使用8051单片机控制的倒计时器程序,通过连接LCD显示屏和数码管来实现倒计时功能
系统接1个按键用于启动,1个蜂鸣器用于提示计时时间到,两个数码管用于显示时间。
功能:系统上电数码管显示60;当按键按下时,系统开始倒计时,同时数码管显示当前时间;当计时为0时,蜂鸣器响。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #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; // 倒计时初始值
- 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) //倒计时进行中
- {
- if (s> 0)
- {
- cursor(0,10);
- writecount(b[s/10]); //显示定时器T0产生的 分(十位)
- writecount(b[s%10]); //显示定时器T0产生的 分(个位)
- n = 0; //开数码管1
- m = 1;
- P2 = a[s/10]; //显示十位数字
- delay(10); //延时10毫秒
- m = 0; //开数码管2
- n = 1;
- P2 = a[s%10]; //显示个位数字
- delay(10); //延时10毫秒
-
- }
- else
- {
- buzzer = 0;
- delay(250);
- buzzer = 1;
- DJS = 0; //倒计时结束,清零
- cursor(0,10);
- writecount(b[6]); //LCD显示数字为6
- writecount(b[0]); //LCD显示数字为0
- }
- }
- else //倒计时为0,显示60秒
- {
- n = 0; //开数码管1
- m = 1;
- P2 = a[6]; //显示数字为6
- delay(20); // 延时10毫秒
- m = 0; //开数码管2
- n = 1;
- P2 = a[0]; // 显示数字为0
- delay(20); // 延时10毫秒
- s= 60; // 重置倒计时初始值
-
- }
- }
- 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--;
- }
- }
复制代码
Keil代码与Proteus仿真下载:
LCD1602.zip
(31.88 KB, 下载次数: 29)
|