初學者,嘗試做一個60S的倒計時。實驗圖如下;
單片機上傳.png (23.31 KB, 下載次數: 56)
下載附件
2019-7-1 09:02 上傳
這個程序是60S倒計時,達到0時,重新賦初值為60,繼續(xù)進行減計時,循環(huán)往復。
單片機源程序如下:
- //60S倒計時程序
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar temp,bai,shi,ge,num;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(uint z);
- void main()
- {
- temp=60;
- num=0;
- P1=0x00;
- TMOD=0x01;
- TH0=(65536-55536)/256;
- TL0=(65536-55536)%256;
- EA=1; ET0=1; TR0=1;
- while(1)
- {
- shi = temp/10;
- ge = temp%10;
- P1 = 0xFD;
- P2 = table[shi];
- P1 = 0xFD;
- delay(5);
- P1 = 0xFE;
- P2 = table[ge];
- delay(5);
- }
- }
- //定時器0外部中斷方式
- void time_0() interrupt 1
- {
- TH0=(65536-55536)/256;
- TL0=(65536-55536)%256;
- num ++;
- if(num == 18)
- {
- temp --;
- num = 0;
- if(temp == -1)
- {
- temp = 60;
- }
- }
- }
- //延時程序
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
復制代碼 全部資料51hei下載地址:
60S程序+仿真.zip
(87.35 KB, 下載次數: 55)
2019-7-1 09:16 上傳
點擊文件名下載附件
60S倒計時 下載積分: 黑幣 -5
|