|
一個定時器可以輸出N個定時時間,方法有多種。舉個比較簡單的示例:取所有定時時間的共倍數(shù)30秒做計數(shù)周期。3個LED分別以5、10、15秒周期閃爍。
- #include <STC8.H>
- #define uint unsigned int
- #define uchar unsigned char
- sbit led1 = P3^0;
- sbit led2 = P3^1;
- sbit led3 = P3^2;
- uchar num,t1s;
- bit t5s,t10s,t15s;
- void InitTimer0()//@11.0592MHz
- {
- TMOD = 0x01;
- TH0 = 0x4C;
- TL0 = 0x00;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void main(void)
- {
- InitTimer0();
- while(1)
- {
- if(t5s==1)
- {
- t5s=0;
- led1=~led1;
- }
- if(t10s==1)
- {
- t10s=0;
- led2=~led2;
- }
- if(t15s==1)
- {
- t15s=0;
- led3=~led3;
- }
- }
- }
- void Timer0Interrupt(void) interrupt 1
- {
- TH0 = 0x4C;
- TL0 = 0x00;
- num++;
- if(num>=20)//1秒
- {
- num=0;
- t1s++;
- if(t1s%5==0)//5秒
- {
- t5s=1;
- }
- if(t1s%10==0)//10秒
- {
- t10s=1;
- }
- if(t1s%15==0)//15秒
- {
- t15s=1;
- }
- if(t1s==30)//30秒
- {
- t1s=0;
- }
- }
- }
復(fù)制代碼 |
|