正弦波逆變器SPWM設(shè)計(jì)參考
//最近在搞SPWM逆變,貢獻(xiàn)一個小程序,F(xiàn)YI
本文引用地址:http://m.butianyuan.cn/article/227526.htm//用18F452調(diào)的,只有SPWM波形部分,反饋沒加進(jìn)來,如果需要改幅度,該R_rate的值就好了,范圍(1~195)
//晶震10M+PLL鎖到40M,RC2輸出SPWM波形,RC0為50HZ方波,作為半橋驅(qū)動時的交越信號。
void main()
{ asm("NOP");
TRISC=0X00; //設(shè)置I/O口方向
TRISD=0X00; //設(shè)置I/O口方向
PORTC=0X00;
PORTD=0X00;
//=============LCD init======================
// lcd_init();
//=============timer0 init===================
T0CON=0x82; //8分頻 ;0xCx is 8bit timer
TMR0L=0x79;
TMR0H=0XFE;
GIE=0X1; //開放全局中斷
TMR0IE=1; //使能timer0 interrupt
//==============PWM init=====================
PR2=124; //設(shè)置PWM頻率20K
CCPR1L=0; //設(shè)置占空比高8位,init時為0
CCP1CON=0x0F; //CCP select PWM mode
T2CON=0X05; //設(shè)置TIMER2預(yù)分頻比并使能T2
//==============MAIN LOOP====================
while(1)
{ }
}
//===============中斷函數(shù)=============================
void interrupt ISR(void)
{
if((TMR0IF)(TMR0IE))
{
TMR0L=0x79;
TMR0H=0XFE;
TMR0IF=0;//清除中斷標(biāo)志
update_duty();//用3.2K的定時頻率按照正弦規(guī)律改變脈寬,改64次正好為50HZ的調(diào)制正弦波
}
}
//下面這部分摘自另一個文件,
unsigned char R_sin=0; //這個變量從0~31變化。
unsigned char R_rate=190;//幅度
void update_duty(void)
{ unsigned int i=0;
if(R_sin==0)
RC0=!RC0; //創(chuàng)造交越信號
i=((float)(sin_tab[R_sin]))*R_rate/100;
R_sin++;
if(R_sin==32)
R_sin=0;
CCPR1L=i/4; //設(shè)置占空比高8位,init時為0
CCP1CON=0x0F+(i3)*16;
}
const unsigned char sin_tab[32]={
0,25,50,74,98,120,142,162,180,
197,212,225,235,244,250,254,
255,254,250,244,235,225,212,
197,180,162,142,120,98,74,50,25};
評論