PIC16F877A的50HZ正弦波
扫描二维码
随时随地手机看文章
1 #include
2 #include
3
4 unsigned char const sin_32[]={104,109,114,119,124,128,133,137,142,146,150,154,157,161,164,//15
5 167,169,172,174,176,177,178,179,180,180,180,180,179,178,176,175,173,171,168,165,//20
6 162,159,156,152,148,144,140,135,131,126,121,116,111,106,101,96,91,86,81,76,72,67,63,//23
7 58,54,50,46,43,39,36,33,31,28,26,24,23,22,21,20,20,20,20,21,22,24,25,27,29,32,35,38,//42
8 41,44,48,52,56,60,65,69,74,79,84,89,94,99
9 };//浮点型精度高,换其他会出现尖角
10
11 void work0();
12 unsigned char set_pwm;
13 void main(void)
14 {
15 work0();
16 while(1)
17 {
18 }
19 }
20
21 void work0()//m=0.8;T=(PR2+1)*4*f*TIMR2分频
22 {
23 //CCPR1L=0X00;//设置CCP1,0%的脉宽输出
24 TRISC=0X00;//PORTC are outputs
25 CCP1CON=0x0C;//0000 1100,设置CCP1为PWM工作方式,且工作循环位的低两位为00
26 PR2=199;//设置PWM周期:(PR2+1)*4*Tosc*(TMR2预分频数1)=200u(5k)
27 T2CON=0x04;//Tc赋值=T*D=(PR2+1)*D=200*D
28 CCP1IE=1;
29 INTCON=0xC0;//GIE=1,开总中断,开启第一外设中断外围功能模块中断
30 TMR2IE=1;TMR2IF=0;
31 }
32
33 //interrupt//
34 void interrupt pic(void)
35 {
36 if(TMR2IF&TMR2IE)
37 {
38 set_pwm++;
39 TMR2IF=0;CCP1IF=0;
40 CCPR1L=sin_32[set_pwm];
41 if(set_pwm==99) set_pwm=0;
42 }
43 }