|
使用8051單片機(jī)控制的倒計(jì)時(shí)器程序,通過(guò)連接LCD顯示屏和數(shù)碼管來(lái)實(shí)現(xiàn)倒計(jì)時(shí)功能
系統(tǒng)接1個(gè)按鍵用于啟動(dòng),1個(gè)蜂鳴器用于提示計(jì)時(shí)時(shí)間到,兩個(gè)數(shù)碼管用于顯示時(shí)間。
功能:系統(tǒng)上電數(shù)碼管顯示60;當(dāng)按鍵按下時(shí),系統(tǒng)開始倒計(jì)時(shí),同時(shí)數(shù)碼管顯示當(dāng)前時(shí)間;當(dāng)計(jì)時(shí)為0時(shí),蜂鳴器響。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.gif (98.98 KB, 下載次數(shù): 50)
下載附件
2023-6-2 20:04 上傳
單片機(jī)源程序如下:
- #include<reg52.h>
- sbit buzzer=P3^3;
- sbit key=P3^2;
- sbit m=P3^0;
- sbit n=P3^1;
- sbit RS=P3^4;
- sbit RW=P3^5;
- sbit E=P3^6;
- unsigned char a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- unsigned char b[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
- unsigned char DJS=0;
- unsigned int count;
- unsigned char s = 60; // 倒計(jì)時(shí)初始值
- void delay(unsigned char t)
- {
- unsigned char i,j;
- for (i = 0; i < t; i++)
- for (j = 0; j < 250; j++);
- }
- void writecommand(unsigned char command)
- {
- RS=0;
- RW=0;
- P1=command;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void writecount(unsigned char count)
- {
- RS=1;
- RW=0;
- P1=count;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- void cursor(unsigned char H,m)
- {
- if(H==0)
- {
- writecommand(0x80+m);
- }
- else if(H==1)
- {
- writecommand(0x80+0x40+m);
- }
- }
- void LCDstring(unsigned char *string)
- {
- while(*string!='\0')
- writecount(*string++);
- }
- void LCDInit()
- {
- E=0;
- writecommand(0x38);
- writecommand(0x0c);
- writecommand(0x06);
- writecommand(0x01);
- }
- //void on()
- //{
- // writecommand(0x0c);
- //}
- //void off()
- //{
- // writecommand(0x08);
- //}
- void ShowInit()
- {
- cursor(0,0);
- LCDstring("LCD");
- cursor(0,5);
- writecount(0x31);
- cursor(0,6);
- writecount(0x36);
- cursor(0,7);
- writecount(0x30);
- cursor(0,8);
- writecount(0x32);
- cursor(0,10);
- writecount(b[6]);
- cursor(0,11);
- writecount(b[0]);
- cursor(1,0);
- LCDstring("Test--Program--");
- }
- void display()
- {
- if (DJS == 1) //倒計(jì)時(shí)進(jìn)行中
- {
- if (s> 0)
- {
- cursor(0,10);
- writecount(b[s/10]); //顯示定時(shí)器T0產(chǎn)生的 分(十位)
- writecount(b[s%10]); //顯示定時(shí)器T0產(chǎn)生的 分(個(gè)位)
- n = 0; //開數(shù)碼管1
- m = 1;
- P2 = a[s/10]; //顯示十位數(shù)字
- delay(10); //延時(shí)10毫秒
- m = 0; //開數(shù)碼管2
- n = 1;
- P2 = a[s%10]; //顯示個(gè)位數(shù)字
- delay(10); //延時(shí)10毫秒
-
- }
- else
- {
- buzzer = 0;
- delay(250);
- buzzer = 1;
- DJS = 0; //倒計(jì)時(shí)結(jié)束,清零
- cursor(0,10);
- writecount(b[6]); //LCD顯示數(shù)字為6
- writecount(b[0]); //LCD顯示數(shù)字為0
- }
- }
- else //倒計(jì)時(shí)為0,顯示60秒
- {
- n = 0; //開數(shù)碼管1
- m = 1;
- P2 = a[6]; //顯示數(shù)字為6
- delay(20); // 延時(shí)10毫秒
- m = 0; //開數(shù)碼管2
- n = 1;
- P2 = a[0]; // 顯示數(shù)字為0
- delay(20); // 延時(shí)10毫秒
- s= 60; // 重置倒計(jì)時(shí)初始值
-
- }
- }
- void main()
- {
- TMOD= 0x01;
- TL0 = (65536 - 1000)%256;
- TH0 = (65536 - 1000)/256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- EX0=1;
- IT0=1;
- LCDInit();
- ShowInit();
- while(1)
- {
- display();
- }
- }
- void Int0() interrupt 0
- {
- delay(10);
- if(key==0)
- {
- DJS=1;
- }
- }
- void Int1() interrupt 1
- {
- TL0 = (65536-1000)%256;
- TH0 = (65536-1000)/256;
- count++;
- if(count == 500)
- {
- count = 0;
- s--;
- }
- }
復(fù)制代碼
Keil代碼與Proteus仿真下載:
LCD1602.zip
(31.88 KB, 下載次數(shù): 29)
2023-6-2 19:00 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|