• 回复
  • 收藏
  • 点赞
  • 分享
  • 发新帖

软件实现双极性SPWM

PWM正弦波发生器:

#include //config1__CONFIG (FOSC_HS & WDTE_OFF & MCLRE_OFF & CP_OFF & BOREN_OFF);//confing2//__CONFIG (PLLEN_ON);            //OSC*4 = 32 MHz__CONFIG (PLLEN_OFF);            //OSC = 8 MHzconst unsigned int  pwmtab[]={0x80,0x86,0x8C,0x92,0x98,0x9E,0xA4,0xA9,0xAF,0xB4,0xBA,0xBF,0xC4,0xC9,0xCE,0xD3,0xD7,0xDB,0xDF,0xE3,0xE7,0xEA,0xED,0xF0,0xF3,0xF5,0xF8,0xFA,0xFB,0xFC,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFC,0xFA,0xF9,0xF7,0xF4,0xF2,0xEF,0xEC,0xE9,0xE5,0xE1,0xDD,0xD9,0xD5,0xD0,0xCC,0xC7,0xC2,0xBC,0xB7,0xB2,0xAC,0xA6,0xA1,0x9B,0x95,0x8F,0x89,0x83,0x7D,0x77,0x71,0x6B,0x65,0x5F,0x5A,0x54,0x4E,0x49,0x44,0x3E,0x39,0x34,0x30,0x2B,0x27,0x23,0x1F,0x1B,0x17,0x14,0x11,0x0E,0x0C,0x09,0x07,0x06,0x04,0x03,0x02,0x01,0x01,0x01,0x01,0x02,0x02,0x04,0x05,0x06,0x08,0x0B,0x0D,0x10,0x13,0x16,0x19,0x1D,0x21,0x25,0x29,0x2D,0x32,0x37,0x3C,0x41,0x46,0x4C,0x51,0x57,0x5C,0x62,0x68,0x6E,0x74,0x7A};

void PWM1(void){    #define FOSC        8000000.0                                      //系统时间频率8M,    #define DIV         1.0                                            //timer2预分频    //#define DIV        4.0                                           //timer2预分频    //#define DIV       16.0                                           //timer2预分频    #define FPWM        20000.0                                        //PWM频率  20k(spwm)    #define PWM_PERIOD  1000000.0/FPWM                                 //PWM周期 50us    #define TOSC        1000000.0/(FOSC)                               //机器周期 0.125US                                                                        //4TOSC = 0.125US*4 = 0.5us    #define PR2_LOADER  (PWM_PERIOD-(4*TOSC*DIV)) / (4*TOSC*DIV)    RB3 = 0;    TRISB3 = 0;    RB5 = 0;    TRISB5 = 0;    CCPTMRS = 0;    CCP1CON = 0x8c;    CCP1CON &= ~(0x03<<4);    CCPR1L=0x00;      TMR2 = 0;    T2CON = 0x00;//预分频1:1    PR2 = 99;    PWM1CON = 0x02;//死区时间1us (FOSC/4(4 * TOSC))    TMR2IF = 0; //Timer2 与PR2 匹配中断标志位    TMR2IE = 1; // TMR2 与PR2 匹配中断允许位    T2CON |= (1<<2)|(0x02<<3);   }

void main(){    for (i=0; i<255; i++);    PWM1();    INTCONbits.GIE = 1;    INTCONbits.PEIE = 1;    TRISBbits.TRISB0 = 0;    PORTBbits.RB0 = 0;    while(1)    {            }}void interrupt isr(void){    static unsigned char index;    unsigned int reference;    if (TMR2IE && TMR2IF)    {        TMR2IF = 0;        PORTBbits.RB0 = !PORTBbits.RB0;         reference = pwmtab[index];                  CCPR1L = (reference>>2)&0xff;        //CCP1CON &= ~(0x03<<4);        //CCP1CON |= ((reference&0xff)<<4);        if (++index > 66*2)        {            index = 0;           }    }}

看了N天理论,不如动手一试!

呵呵, 结果!

全部回复(5)
正序查看
倒序查看
remon
LV.4
2
2013-10-31 15:05

单极性(是否是合成的就称为单极性):

0
回复
remon
LV.4
3
2013-10-31 15:12
@remon
单极性(是否是合成的就称为单极性):[图片]
0
回复
心泪
LV.1
4
2013-11-01 19:02
@remon
[图片]

用的哪款单片机?能提供个硬件图跟源代码吗?

0
回复
remon
LV.4
5
2013-11-01 19:56
@心泪
用的哪款单片机?能提供个硬件图跟源代码吗?

PIC单片机,带CCP功能的就可以了!

软件在1L,有一个CCP引脚加RC就可以得到正弦波!

0
回复
小螃蟹
LV.1
6
2015-02-12 18:52
@remon
单极性(是否是合成的就称为单极性):[图片]
你这个是双极性的吗?我滤出来的效果没有你的好,求指教,我的QQ是330670304《很急,兄弟,
0
回复