51单片机 1602液晶显示的DS1302实时时钟+Proteus仿真

51单片机 1602液晶显示的DS1302实时时钟+Proteus仿真


  • 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
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char

sbit IO = P1^0;
sbit SCLK = P1^1;
sbit RST = P1^2;
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;

uchar \*WEEK[]=
{
"SUN","\*\*\*","MON","TUS","WEN","THU","FRI","SAT"
};
uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00 "};
uchar LCD_DSY_BUFFER2[]={"TIME 00:00:00 "};
uchar DateTime[7];

void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}

void Write\_A\_Byte\_TO\_DS1302(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
IO=x&0x01;SCLK=1;SCLK=0;x>>=1;
}
}

uchar Get\_A\_Byte\_FROM\_DS1302()
{
uchar i,b=0x00;
for(i=0;i<8;i++)
{
b |= \_crol\_((uchar)IO,i);
SCLK=1;SCLK=0;
}
return b/16\*10+b%16;
}

uchar Read\_Data(uchar addr)
{
uchar dat;
RST = 0;SCLK=0;RST=1;
Write\_A\_Byte\_TO\_DS1302(addr);
dat = Get\_A\_Byte\_FROM\_DS1302();
SCLK=1;RST=0;
return dat;
}

void GetTime()
{
uchar i,addr = 0x81;
for(i=0;i<7;i++)
{
DateTime[i]=Read\_Data(addr);addr+=2;
}
}

uchar Read\_LCD\_State()
{
uchar state;
RS=0;RW=1;EN=1;DelayMS(1);
state=P0;
EN = 0;DelayMS(1);
return state;
}


void LCD\_Busy\_Wait()
{
while((Read\_LCD\_State()&0x80)==0x80);
DelayMS(5);
}

void Write\_LCD\_Data(uchar dat)
{
LCD\_Busy\_Wait();
RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0;
}

void Write\_LCD\_Command(uchar cmd)
{
LCD\_Busy\_Wait();
RS=0;RW=0;EN=0;P0=cmd;EN=1;DelayMS(1);EN=0;
}

void Init\_LCD()
{
Write\_LCD\_Command(0x38);
DelayMS(1);
Write\_LCD\_Command(0x01);
DelayMS(1);
Write\_LCD\_Command(0x06);
DelayMS(1);
Write\_LCD\_Command(0x0c);
DelayMS(1);
}

void Set\_LCD\_POS(uchar p)
{
Write\_LCD\_Command(p|0x80);
}

void Display\_LCD\_String(uchar p,uchar \*s)
{
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';
}

void main()
{
Init\_LCD();
while(1)
{
GetTime();
Format\_DateTime(DateTime[6],LCD_DSY_BUFFER1+5);
Format\_DateTime(DateTime[4],LCD_DSY_BUFFER1+8);
Format\_DateTime(DateTime[3],LCD_DSY_BUFFER1+11);

strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]);

Format\_DateTime(DateTime[2],LCD_DSY_BUFFER1+5);
Format\_DateTime(DateTime[1],LCD_DSY_BUFFER1+8);
Format\_DateTime(DateTime[0],LCD_DSY_BUFFER1+11);

Display\_LCD\_String(0x00,LCD_DSY_BUFFER1);
Display\_LCD\_String(0x40,LCD_DSY_BUFFER2);
}
}

仿真资源和程序源码

1
2
3
链接:https://pan.baidu.com/s/11FNookywaY0cSdEOj22-0w 
提取码:3280