您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 用AVR单片机Atmega16产生两路互补的PWM波去控制全桥逆变电路的程序
#includeavr/io.hvoidmain(void){PORTB=0x00;DDRB=0x0E;//T/C1初始化TCCR1A=0xA1;TCCR1B=0x09;//匹配时清零,TOP:255,频率:8M/256=31.25KOCR1A=85;//占空比:1/3OCR1B=128;//占空比:1/2//T/C2初始化TCCR2=0x69;//匹配时清零,TOP:255,频率:31.25KOCR2=170;//占空比:2/3while(1);}使用M16产生三路PWM的程序,参考一下基本OK还有个是可调节的PWM程序,我做过仿真了,需要全部留下邮箱传给你;/*****************************************************#defineKEYPINC.0#definePWMAPORTB.3//17号脚#definePWMBPORTB.4//18号脚#includemega8.h#includedelay.h#includemath.hunsignedintm=0;unsignedcharxiangxian=0;bitINIT2=0;//判断是否象限2已经初始化;bitINIT3=0;bitINIT4=0;/*下面为四个象限中处理函数,参数为45度平分为255段角度*/inlinepanduan(){if(m=255){xiangxian=1;}elseif((m255)&&(m511)){xiangxian=2;if(m==256){INIT2=1;PWMA=0;OCR1A=0x00;OCR1B=0xff;PWMB=1;}}elseif((m=511)&&(m767)){xiangxian=3;}elseif((m=767)&&(m1024)){xiangxian=4;}elseif(m1024){m=0;}}voidxiangxian1(unsignedchardegree){PWMA=0;PWMB=0;OCR1BL=m;OCR1AL=255-m;}voidxiangxian2(unsignedchardegree){unsignedchartemp;temp=m-255;OCR1AL=temp;OCR1BL=temp;}voidxiangxian3(unsignedchardegree){unsignedchartemp;temp=m-511;PWMA=1;PWMB=1;OCR1BL=255-temp;OCR1AL=temp;}voidxiangxian4(unsignedchardegree){unsignedchartemp=0;temp=m-767;PWMA=1;PWMB=0;OCR1BL=255-temp;OCR1AL=255-temp;}/*角度计算函数,负责计算在各个象限中角度对应的PWM输出*/voidSET_ANGLE(unsignedchardegree){switch(xiangxian){case1:xiangxian1(degree);break;case2:xiangxian2(degree);break;case3:xiangxian3(degree);break;case4:xiangxian4(degree);break;default:break;}}voidmain(void){unsignedchartemp;unsignedcharxiangxian=0;//DeclareyourlocalvariablesherePORTB=0x187;DDRB=0x1e;//PortCinitialization//Func6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In//State6=TState5=TState4=TState3=TState2=TState1=TState0=PPORTC=0x01;DDRC=0x00;//PortDinitialization//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=OutFunc1=InFunc0=In//State7=TState6=TState5=TState4=TState3=TState2=0State1=TState0=TPORTD=0x00;DDRD=0x04;//Timer/Counter0initialization//Clocksource:SystemClock//Clockvalue:Timer0StoppedTCCR0=0x00;TCNT0=0x00;//Timer/Counter1initialization//Clocksource:SystemClock//Clockvalue:1000.000kHz//Mode:FastPWMtop=01FFh//OC1Aoutput:Inverted//OC1Boutput:Inverted//NoiseCanceler:Off//InputCaptureonFallingEdge//Timer1OverflowInterrupt:Off//InputCaptureInterrupt:Off//CompareAMatchInterrupt:Off//CompareBMatchInterrupt:OffTCCR1A=0xF1;TCCR1B=0x01;TCNT1H=0x00;TCNT1L=0x00;ICR1H=0x00;ICR1L=0x00;OCR1AH=0x00;OCR1AL=0x00;OCR1BH=0x00;OCR1BL=0x00;//Timer/Counter2initialization//Clocksource:SystemClock//Clockvalue:Timer2Stopped//Mode:Normaltop=FFh//OC2output:DisconnectedASSR=0x00;TCCR2=0x00;TCNT2=0x00;OCR2=0x00;//ExternalInterrupt(s)initialization//INT0:Off//INT1:OffMCUCR=0x00;//Timer(s)/Counter(s)Interrupt(s)initializationTIMSK=0x00;//AnalogComparatorinitialization//AnalogComparator:Off//AnalogComparatorInputCapturebyTimer/Counter1:OffACSR=0x80;SFIOR=0x00;PWMA=0;PWMB=0;OCR1AL=0xff;OCR1BL=0x00;while(1){if(KEY==0){delay_ms(20);if(KEY==0){m=m+1;panduan();SET_ANGLE(m);PORTD.2=!PORTD.2;}}};}
本文标题:用AVR单片机Atmega16产生两路互补的PWM波去控制全桥逆变电路的程序
链接地址:https://www.777doc.com/doc-4611655 .html