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 };//浮點型精度高,換其他會出現(xiàn)尖角
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;//設(shè)置CCP1,0%的脈寬輸出
24 TRISC=0X00;//PORTC are outputs
25 CCP1CON=0x0C;//0000 1100,設(shè)置CCP1為PWM工作方式,且工作循環(huán)位的低兩位為00
26 PR2=199;//設(shè)置PWM周期:(PR2+1)*4*Tosc*(TMR2預(yù)分頻數(shù)1)=200u(5k)
27 T2CON=0x04;//Tc賦值=T*D=(PR2+1)*D=200*D
28 CCP1IE=1;
29 INTCON=0xC0;//GIE=1,開總中斷,開啟第一外設(shè)中斷外圍功能模塊中斷
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 }