[KeilC51]软件简单PWM波生成
应用在某些硬件资源不够用的情况下,不是很精确,频率的话也不是很好调节;
原理就是“J满则溢”。
1.中断1服务程序不改变占空比:
2.终端1服务程序改变占空比:(明显可以看到波形随时间变化推移)
例程代码如下:
1 #include <reg52.h> 2 // CPU STC89C52RC 12T 11.0592M的晶振 3 #define LED_PORT P0 4 sbit P0_0 = LED_PORT^0; 5 sbit P0_1 = LED_PORT^1; 6 sbit P0_2 = LED_PORT^2; 7 sbit P0_3 = LED_PORT^3; 8 sbit P0_4 = LED_PORT^4; 9 sbit P0_5 = LED_PORT^5; 10 sbit P0_6 = LED_PORT^6; 11 sbit P0_7 = LED_PORT^7; 12 13 unsigned char sum_temp0,sum_temp1,sum_temp2,sum_temp3,sum_temp4,sum_temp5,sum_temp6,sum_temp7; 14 unsigned char incre_step0,incre_step1,incre_step2,incre_step3,incre_step4,incre_step5,incre_step6,incre_step7; 15 void main(void){ 16 TMOD = 0x21; 17 TCON = 0x00; 18 19 TR1=0; 20 TH1=(65536-50000)/256; 21 TL1=(65536-50000)%256; 22 TF1=0; 23 TR1=1; 24 ET1=1; 25 EA = 1; 26 sum_temp0 = sum_temp1 = sum_temp2 = sum_temp3 = sum_temp4 = sum_temp5 = sum_temp6 = sum_temp7 = 0; 27 incre_step0 = 0x10; 28 incre_step1 = 0x30; 29 incre_step2 = 0x50; 30 incre_step3 = 0x70; 31 incre_step4 = 0x90; 32 incre_step5 = 0xb0; 33 incre_step6 = 0xd0; 34 incre_step7 = 0xf0; 35 while(1){ 36 sum_temp0 += incre_step0; 37 P0_0 = CY; 38 sum_temp1 += incre_step1; 39 P0_1 = CY; 40 sum_temp2 += incre_step2; 41 P0_2 = CY; 42 sum_temp3 += incre_step3; 43 P0_3 = CY; 44 sum_temp4 += incre_step4; 45 P0_4 = CY; 46 sum_temp5 += incre_step5; 47 P0_5 = CY; 48 sum_temp6 += incre_step6; 49 P0_6 = CY; 50 sum_temp7 += incre_step7; 51 P0_7 = CY; 52 } 53 } 54 55 56 57 void Timer1_ISR(void) interrupt 3 58 { 59 static unsigned short int count = 0; 60 unsigned char temp; 61 TR1=0; 62 TH1=(65536-50000)/256; 63 TL1=(65536-50000)%256; 64 TF1=0; 65 TR1=1; 66 67 count++; 68 69 #ifdef __NOTCHANGE__ 70 return ; 71 #endif //如果不管变就直接返回 72 73 if(count == 500){ 74 temp = incre_step0; 75 incre_step0 = incre_step1; 76 incre_step1 = incre_step2; 77 incre_step2 = incre_step3; 78 incre_step3 = incre_step4; 79 incre_step4 = incre_step5; 80 incre_step5 = incre_step6; 81 incre_step6 = incre_step7; 82 incre_step7 = temp; 83 count = 0; 84 85 } 86 }
逻辑分析仪数据如下:
https://files.cnblogs.com/alimy/softPWM_logicdata.zip
~不再更新,都不让我写公式,博客园太拉胯了