求高手指教啊。我做spwm波形,程序不知道哪里出错了?????
[复制链接]
求各位大侠帮我看看需要怎么改,spwm波形就是出不来,用示波器观察只看到了我贴在下面的图片的那种波形
急啊,求各位大侠帮帮忙了。调了好久不知道怎么改了。。。。。。。。
#include<pic.h> #define uchar unsigned char #define uint unsigned int __CONFIG(0x3B31); unsigned char num; unsigned char key_num; unsigned char mode; bit F; const uchar sin_[]={0,12,24,37,49,61,72,84,95,106,117,128, 138,148,158,167,176,184,192,200,207,214,220,225,230,234,238,242,244,246, 248,249,249,249,248,246,244,242,238,234,230,225,220,214,207,200,192,184, 176,167,158,148,138,128,117,106,95,84,72,61,49,37,24,12}; //void delay(uint x); void io_init(); void pwm_init(); void main() { mode=0; F=1; io_init(); pwm_init(); while(1) { if(F==1) { if(TMR2>=CCPR1L) { RC1=0; } } else if(F==0) { if(TMR2>=CCPR2L) { RC2=0; } } } } /*void delay(uint x) { uint a,b; for(a=x;a>0;a--) for(b=110;b>0;b--); }*/
void io_init() { TRISD=0; PORTD=0XFF; TRISC=0XF9;//设置ccp1引脚为输出方式 RC1=1; RC2=0; } void pwm_init() { PR2=0x4B; //20MHZ的用f9 T2CON=0X15;// 打开定时器2设置分频1:1后分频为1:3 CCP1CON=0X3C;//设置为pwm模式 CCP2CON=0x3c; CCPR1L=0X00;//改变ccpr1l和ccp1con(bit4--bit5)的比值就可以改变占空比 CCPR2L=0X00; INTCON=0X00;//禁止总中断和外围中断 } void interrupt time0() { TMR2IF=0; if(mode==63) { F=!F; mode=0; } mode++; if(F==1) { RC1=1; RC2=0; CCPR1L=sin_[mode]; } if(F==0) { RC1=0; RC2=1; CCPR2L=sin_[mode]; } }
|