这是我编写的PWM呼吸灯程序,但是只有在中断里面加上while(1)循环他才能呼吸。没有这个while直接就不亮了,但我看别人的呼吸灯函数中断内没有while循环也能呼吸,这是为什么?
还有一个问题,加上while循环后的呼吸灯在逐渐变暗的时候“灯亮灭的抖动”明显,怎么能让他看起来流畅一点。
单片机源程序如下:
- #include "reg52.h"
- typedef unsigned char u8;
- typedef unsigned int u16;
- sbit LED = P2^0;
- u16 timer1, count, value;
- bit flag;
- void TimeInit()//¶¨ê±1us
- {
- TMOD |= 0x10;
- TH1 = 0xFF;
- TL1 = 0xFF;
- TR1 = 1;
- EA = 1;
- ET1 = 1;
- }
- void main()
- {
- LED = 1;
- timer1 = 0;
- count = 0;
- value = 0;
- flag = 0;
- TimeInit();
- while(1);
- }
- void Time() interrupt 2
- {
- TH1 = 0xFF;
- TL1 = 0xFF;
- //while(1)
- // {
- timer1++;
- count++;
- if(timer1 == 1000)
- {
- timer1 = 0;
- LED = 1;
- }
- if(count == 100)
- {
- count = 0;
- if(flag == 0)
- {
- value++;
- }
- if(flag == 1)
- {
- value--;
- }
- }
- if(value == 1000)
- {
- flag = 1;
- }
- if(value == 0)
- {
- flag = 0;
- }
- if(value > timer1)
- {
- LED = 0;
- }
- else
- {
- LED = 1;
- }
- // }
- }
复制代码
|