【Proteus仿真】51单片机+LCD1602显示+按键扫描功能实现模板

【Proteus仿真】51单片机+LCD1602显示+按键扫描功能实现模板


  • Proteus仿真演示
    在这里插入图片描述
  • 说明:

演示简易时钟驱动显示,按键P31启动/停止,(无源蜂鸣器响应),注意在Proteus搭建电路上调用的是无源蜂鸣器,P30复位。定时器没过一秒中P10上的Led灯状态改变一次。

主程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
实验名称:LCD1602显示
接线说明:
实验现象:下载程序后,按下P31键开始计时,再次按下P31键停止计时,按下P30键清零
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include "public.h"
#include "key.h"
#include "beep.h"
#include "lcd1602.h"
#include "time.h"

void main()
{

u8 key=0;
u8 time_buf[9];
u8 time_flag=0;
led = 0;
lcd1602\_init();
time0\_init();
while(1)
{
key=key\_scan(0);//按键扫描
if(key==KEY1_PRESS)//开始和停止计时
{
time_flag=!time_flag;
beep\_alarm(1000,10);//短暂提示音
}
else if(key==KEY2_PRESS)//清除计时
{
time0\_stop();
time_flag=0;
gmin=0;
gsec=0;
gmsec=0;
beep\_alarm(1000,10);//短暂提示音
}
if(time_flag)
time0\_start();//开始计时
else
time0\_stop();//停止计时

time_buf[0]=ghour/10+0x30;
time_buf[1]=ghour%10+0x30;//毫秒
time_buf[2]='-';//-
time_buf[3]=gmin/10+0x30;
time_buf[4]=gmin%10+0x30;//分
time_buf[5]='-';//-
time_buf[6]=gsec/10+0x30;
time_buf[7]=gsec%10+0x30;//秒
time_buf[8]='\0';
lcd1602\_show\_string(0,0,time_buf);//显示
}
}


程序源码和仿真资源

1
2
3
链接:https://pan.baidu.com/s/1G8lTPsnnRtP9CkDtlWQxbQ 
提取码:pkvj