52单片机lcd1602显示倒计时的程序
时间: 2023-10-16 12:10:05 浏览: 169
以下是基于51单片机的LCD1602显示倒计时的程序,需要使用定时器和LCD1602驱动库:
```c
#include<reg52.h>
#include<intrins.h>
#include"lcd1602.h"
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
}; // 0~F的16进制编码
void delay(uint i) // 延时函数
{
while(i--);
}
void main()
{
uint i,j;
uchar m=10,s=0;
lcd_init(); // 初始化LCD1602
while(1)
{
for(i=0;i<60000;i++) // 延时1分钟
{
for(j=0;j<1500;j++)
{
TH0=0xfc; // 定时1ms
TL0=0x66;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
}
if(s==0) // 秒数为0时,减去1分钟
{
m--;
if(m==0) // 时间到,停止计时
{
lcd_show_string(0,0,"Time's up!");
while(1);
}
s=59;
}
else
{
s--;
}
lcd_show_char(0,0,table[m/10]); // 显示分钟的十位数
lcd_show_char(0,1,table[m%10]); // 显示分钟的个位数
lcd_show_char(0,3,table[s/10]); // 显示秒钟的十位数
lcd_show_char(0,4,table[s%10]); // 显示秒钟的个位数
}
}
```
在此程序中,使用了定时器来控制时间的计时和显示,每隔1分钟减去1分钟,并在倒计时结束时停止计时并显示“Time's up!”。请注意,此程序仅供参考,请根据自己的具体需求进行修改。