|
定時器實(shí)現(xiàn)流水燈
單片機(jī)源程序如下:
- //*********************************************************************************
- //功能:查詢方式實(shí)現(xiàn)間隔1s的流水燈程序。8個發(fā)光二極管依次順序點(diǎn)亮,采用循環(huán)移位
- //函數(shù)_crol_(m,1)實(shí)現(xiàn)
- //實(shí)驗(yàn)箱上D1區(qū)J52接P1
- //*********************************************************************************
- #include<reg51.h> /*包含頭文件REG51.H*/
- #include <intrins.h> /*包含函數(shù)_crol_(m,1)的頭文件*/
- #define uchar unsigned char
- #define LED P1 /*定義8個LED接至P1口*/
- #define Count 50000 /* T1方式1定時50ms的計(jì)數(shù)值*/
- uchar Temp1, Temp2;
- void Delay1s(); //1s延時函數(shù)聲明
- void main() //主程序
- {
- TMOD=0x10; //設(shè)置T1為定時器、方式1
- LED=0xff; //8個LED全部熄滅
- Temp1=0xfe; //點(diǎn)亮最上面的LED
- LED=Temp1;
- while(1)
- {
- Delay1s(); //調(diào)用1s延時函數(shù)
- Temp2=_crol_(Temp1,1); //采用_crol_(m,1)實(shí)現(xiàn)單只LED從上到下依次點(diǎn)亮
- Temp1=Temp2;
- LED=Temp2;
- }
- }
- //*********************************************************************************
- //函數(shù)名:Delay1s
- //函數(shù)功能:利用T1定時、方式1實(shí)現(xiàn)1s延時,采用查詢方式實(shí)現(xiàn)
- //*********************************************************************************
- void Delay1s()
- {
- uchar i;
- for(i=0;i<0x14;i++) //設(shè)置20次循環(huán)次數(shù)
- {
- TH1=(65536-Count)/256; //T1的高8位初值
- TL1=(65536-Count)%256; //T1的低8位初值
- TR1=1; //啟動T1
- while(!TF1); //查詢計(jì)數(shù)是否溢出,即定時50ms時間到了嗎?
- TF1=0; //50ms定時時間到,將T1溢出標(biāo)志位TF1清零
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
定時器實(shí)現(xiàn)流水燈.zip
(61.82 KB, 下載次數(shù): 25)
2019-6-14 16:22 上傳
點(diǎn)擊文件名下載附件
|
|