产生SPWM用于逆变器控制H桥,从而产生频率和电压幅值可变的正弦波输出 思路是:用MEGA16的定时器1产生SPWM输出,每个采样点时刻的占空比存入这个数组sin_temp[j]=0.5+0.5*M*sin(j*2*PI/N);如果采用pwm输出模式8的话,则ICR1的值为TOP值,那么OCR1A,OCR1B中的寄存器值就为 (uint)(TOP*sin_temp[i]),我想在进入比较匹配中断的时候(或者溢出中断的时候)改变OCR1A,OCR1B的值,这样输出就能产生两路互补的SPWM波,其中ICR1的值改变载波频率,M为调制度(用于改变电压幅值),N为载波比(改变输出电压频率) 但是我调试了好久都是只输出一个固定占空比的PWM 这是我的调试程序: #include<iom16v.h> #include<macros.h> #include<math.h> #define uint unsigned int #define uchar unsigned char #define PI 3.1415926 #define N 60 //载波比 #define FS 50 //基波频率 #define M 0.5 //调制比 #define FCLK 8000000//晶振 #define DeadTime 16 //死区时间控制系数 float sin_temp[N];//占空比 uint TOP1; uint temp[N]; uchar count=1; void port_init(void) { DDRD|=BIT(4)|BIT(5);// OC1A,OC1B互补输出 } void timer1_init(void) { TCCR1A=0XB0;//1011 0000 OC1A,OC1B互补输出 //TCCR1A=0XB3; TCCR1B=0X10;//0001 0000,PWM模式8,关定时器 //TIMSK|=BIT(TOIE1); //开输入捕捉中断 TIMSK|=BIT(OCIE1A)|BIT(OCIE1B);//比较匹配中断 //TIMSK|=BIT(TICIE1);//输入捕获中断开 ICR1=(uint)(FCLK/(2*N*FS)); OCR1A=temp[0]-DeadTime; OCR1B=temp[0]+DeadTime; } /*#pragma interrupt_handler t1_capt: 6 // T/C1的输入捕捉中断 void t1_capt(void) { OCR1A=(uint)(TOP1*sin_temp[count++])-DeadTime; OCR1B=(uint)(TOP1*sin_temp[count++])+DeadTime; PORTB^=BIT(3); if(count>N) count=0; } #pragma interrupt_handler timer1_ovf:9 //T/C1的溢出中断 void timer1_ovf(void) { count++; if(count<=N) { //OCR1A=(uint)(TOP1*sin_temp[i])-DeadTime; //OCR1B=(uint)(TOP1*sin_temp[i])+DeadTime; OCR1A=temp[count]-DeadTime; OCR1B=temp[count]+DeadTime; PORTB^=BIT(3); } else count=0; }*/ #pragma interrupt_handler timer1_compa:7//比较匹配A中断 void timer1_compa(void) { if(count<=N) { OCR1A=temp[count++]-DeadTime; } else count=0; } #pragma interrupt_handler timer1_compb:8//比较匹配B中断 void timer1_compb(void) { if(count<=N) { OCR1B=temp[count++]+DeadTime; } else count=0; } void main(void) { uchar j; CLI(); TOP1=(uint)(FCLK/(2*N*FS)); port_init(); for(j=0;j<N;j++) { sin_temp[j]=0.5+0.5*M*sin(j*2*PI/N);//各采样点的占空比 temp[j]=(uint)(TOP1*sin_temp[j]); } timer1_init(); TIFR=0; TCNT1=0; SEI();//开总中断 TCCR1B=0X11;//PWM模式8,开定时器 DDRB|=BIT(3); PORTB&=~BIT(3); while(1); } |
|