|
定時(shí)器方式2是8位自動(dòng)重裝初值模式,12M晶振最大256us。并且TH0=TL0,如果預(yù)裝值6,可以定時(shí)250us,定時(shí)1秒需要累加中斷4000次。
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- unsigned int count=0;//無(wú)符號(hào)整型數(shù)據(jù)(16位)
- uchar led;
- void time0(void)interrupt 1
- {
- //無(wú)需再對(duì)TH0 TL0 賦值
- count++;
- if(count==4000)
- {
- count=0;
- led=_crol_(led,1);
- P1=led;
- }
- }
- void main(void)
- {
- led=0xfe; //賦初值
- P1=led;
- TMOD= 0x02; //設(shè)置定時(shí)器自動(dòng)重裝模式
- TL0 = 0x06; //設(shè)置定時(shí)初值
- TH0 = 0x06; //設(shè)置定時(shí)重載值
- TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
- ET0=1;
- EA=1;
- while(1);
- }
復(fù)制代碼 |
|