基于51单片机+DS1302+ LCD1602显示时间Proteus仿真

基于51单片机+DS1302+ LCD1602显示时间Proteus仿真


在这里插入图片描述

  • AT89C52LCD1602DS1302
  • Proteus仿真器件

在这里插入图片描述

实例代码

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <reg51.h>
#include <string.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define delayNOP() {\_nop\_();\_nop\_();\_nop\_();\_nop\_();}
sbit SDA=P2^6; // DS1302数据线
sbit CLK=P2^5; // DS1302时钟线
sbit RST=P2^4; // DS1302复位线
//DS18B20数据端口定义
sbit DQ=P2^4; //DS18B20数据端口
sbit LCD_RS=P2^0; // LCD寄存器选择
sbit LCD_RW=P2^1; // LCD读写/写控制
sbit LCD_EN=P2^2; // LCD启用

uchar tCount=0;
//一年中每个月的天数,2月的天数由年份决定
uchar MonthsDays[]= {0,31,0,31,30,31,30,31,31,30,31,30,31};
//周日,每周一到周六(0,1-6)【读取DS1302时分别是1-7】
uchar \*WEEK[]= {"SUN","MON","TUS","WEN","THU","FRI","SAT"};

//LCD显示缓冲
uchar LCD_DSY_BUFFER1[]= {"00-00-00 "};
uchar LCD_DSY_BUFFER2[]= {"00-00-00 "};
uchar DateTime[7]; //所读取的日期时间
uchar Adjust_flag=0; //当前调节的时间对像:秒,分,时,日,月,年(1,2,3,4,5,6)
uchar Change_Flag[]=" YMDHM";//(分,时,日,月,年)(不调节秒与周)


void DelayMS(uchar x)//延时
{ uchar i;
while(x--)
for(i=0; i<120; i++);
}


void Write\_A\_Byte\_TO\_DS1302(uchar x)//向DS1302写入一个字节
{ uchar i;
for(i=0; i<8; i++)
{ SDA=x&1;
CLK=1;
CLK=0;
x>>=1;
}
}


uchar Get\_A\_Byte\_FROM\_DS1302()//读取一个字节
{ uchar i,b,t;
for(i=0; i<8; i++)
{ b>>=1;
t=SDA;
b|=t<<7;
CLK=1;
CLK=0;
}

return b/16\*10+b%16;//BCD码转换
}

uchar Read\_Data(uchar addr)//指定位置读数据
{ uchar dat;
RST=0;
CLK=0;
RST=1;
Write\_A\_Byte\_TO\_DS1302(addr);
dat=Get\_A\_Byte\_FROM\_DS1302();
CLK=1;
RST=0;
return dat;
}

void Write\_DS1302(uchar addr,uchar dat)//向某地址写入数据
{ CLK=0;
RST=1;
Write\_A\_Byte\_TO\_DS1302(addr);
Write\_A\_Byte\_TO\_DS1302(dat);
CLK=0;
RST=0;
}

void SET\_DS1302()//设置时间
{ uchar i;
Write\_DS1302(0x8e,0x00);

for(i=0; i<7; i++)
{

Write\_DS1302(0X80+2\*i,(DateTime[i]/10<<4)|(DateTime[i]%10));//日,月,周,年,写入 地址每次增2
}
Write\_DS1302(0x8e,0x80);//加保护
}


void GetTime()//读取本地的时间
{
uchar i;
for(i=0; i<7; i++)
{
DateTime[i]=Read\_Data(0x81+2\*i);
}
}

//LCD驱动代码
bit LCD\_Busy\_Check()//LCD忙
{ bit result;
LCD_RS=0;
LCD_RW=1;
LCD_EN=1;
delayNOP();
result=(bit)(P0&0x80);
LCD_EN=0;
return result;
}


void Write\_LCD\_Command(uchar cmd) //写指令
{ while(LCD\_Busy\_Check());
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
\_nop\_();
\_nop\_();
P0=cmd;
delayNOP();
LCD_EN=1;
delayNOP();
LCD_EN=0;
}

void Write\_LCD\_Data(uchar dat)//写数据
{ while(LCD\_Busy\_Check());
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delayNOP();
LCD_EN=1;
delayNOP();
LCD_EN=0;
}


void Init\_LCD()//LCD初始化
{ Write\_LCD\_Command(0x01);
DelayMS(5);
Write\_LCD\_Command(0x38);
DelayMS(5);
Write\_LCD\_Command(0x0c);
DelayMS(5);
Write\_LCD\_Command(0x06);
DelayMS(5);
}

void Set\_LCD\_POS(uchar pos)//设置显示位置
{ Write\_LCD\_Command(pos|0x80);
}


void Display\_LCD\_String(uchar p,uchar \*s)//在LCD上显示字符串
{ uchar i;
Set\_LCD\_POS(p);
for(i=0; i<16; i++)
{ Write\_LCD\_Data(s[i]);
DelayMS(1);
}
}


void Format\_DateTime(uchar d,uchar \*a)//日期与时间值转换为数字字符
{ a[0]=d/10+'0';
a[1]=d%10+'0';
}


uchar isLeapYear(uint y)//判断是否为闰年
{ return (y%4==0&&y%100!=0)||(y%400==0);
}


void RefreshWeekDay()
{ uint i,d,w=5; //
for(i=2021; i<2021+DateTime[6]; i++)
{ d=isLeapYear(i)?366:365;
w=(w+d)%7;
}
d=0;
for(i=1; i<DateTime[4]; i++)
d+=MonthsDays[i];
d+=DateTime[3];
DateTime[5]=(w+d)%7+1;
}


//定时器0每秒刷新LCD显示
void T0\_INT() interrupt 1
{ uchar i;
if(++tCount!=2) return;
tCount=0;//刷新

Format\_DateTime(DateTime[6],LCD_DSY_BUFFER1);//年
Format\_DateTime(DateTime[4],LCD_DSY_BUFFER1+3);//月
Format\_DateTime(DateTime[3],LCD_DSY_BUFFER1+6);//日

Format\_DateTime(DateTime[2],LCD_DSY_BUFFER2);//时
Format\_DateTime(DateTime[1],LCD_DSY_BUFFER2+3);//分
Format\_DateTime(DateTime[0],LCD_DSY_BUFFER2+6);//秒

Display\_LCD\_String(0x03,LCD_DSY_BUFFER1);//显示年月日,星期,时分秒
Set\_LCD\_POS(0x43);
for(i=0; i<14; i++)
{
Write\_LCD\_Data(LCD_DSY_BUFFER2[i]);
DelayMS(1);
}
}

//主程序
void main()
{
Init\_LCD();//液晶初始化
IE=0X87;//允许INT0,T0中断
TR0=1;
while(1)
{ GetTime();

}
}