各位大神,谁能帮我解答一下,为什么我的仿真达不到预期的效果,原代码和图像请参考
附
pwm.rar
(80.14 KB, 下载次数: 50)
,
我所参考的链接:http://blog.163.com/asm_c/blog/static/2482031132012716860918/
我的结果截图:
单片机源程序如下:
- #include<reg52.h>
- unsigned char a, b;
- sbit K1 = P3^2; //定义两个调整占空比的按键
- sbit K2 = P3^3;
- //------------------------------------------
- void delay(unsigned int i)
- {
- unsigned char j;
- while(i--)
- for(j = 0; j < 60; j++);
- }
- //------------------------------------------
- void main()
- {
- TMOD = 0x01; //T0定时方式1
- TH0 = (65536 - 500) / 256; //500us@12MHz
- TL0 = (65536 - 500) % 256;
- EA = 1;
- TR0 = 1;
- ET0 = 1;
- a = 0;
- b = 10;
- P3^2=0;
- while(1) {
- if(!K1)
- {
- delay(20);
- if(!K1)
- {
- b++;
- if(b > 19)
- b = 19;
- while(!K1);
- }
- }
- if(!K2)
- {
- delay(20);
- if(!K2)
- {
- b--;
- if(b < 1)
- b = 1;
- while(!K2);
- }
- }
- //以上两行是检查按键、延时消抖、修改占空比、等待按键释放,b = 1~19
- P0 = ((b / 10) << 4) + b % 10; //显示占空比等级
- }
- }
- //------------------------------------------
- void time0() interrupt 1
- {
- TL0 = (65536 - 493) % 256; //补偿中断进入时间
- TH0 = (65536 - 493) / 256; //500us@12MHz
- a--; //a = 1~20
- if(a == 0)
- {
- a = 20;
- P2 = 0;
- } //周期=20*500us
- if(a <= b)
- P2 = 255; //输出到示波器
- P1 = ~P2; //输出到LED,低电平发光
- }
- //------------------------------------------
复制代码
|