📄 lcd_1602.lst
字号:
C51 COMPILER V7.07 LCD_1602 05/14/2009 21:13:16 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE LCD_1602
OBJECT MODULE PLACED IN LCD_1602.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE LCD_1602.C BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /* DS12C887时钟C语言编程 作者:赖楚君 时间:从2009-5-12至2009-
2
3 程序流程说明
4
5 一、LCD_1602驱动程序
6 1、1ms延时函数
7 2、读LCD函数
8 3、检测忙碌标志位函数
9 4、写LCD函数
10 5、字符显示定位函数
11 6、输出并显示字符函数
12 7、初始化LCD函数
13
14 二、DS12C887驱动程序
15 1、地址替换函数
16 2、读DS12C887函数
17 3、写DS12C887函数
18 4、初始化DS12C887函数
19 */
20 #include<reg51.h>
21 #include<intrins.h>
22 #define uchar unsigned char
23 #define uint unsigned int
24
25 /* 一、LCD_1602驱动程序
26 端口引脚定义 */
27 sfr LCD1602_DATA_PORT = 0x80;//P0口
28 sbit RS = P2^5;//命令/数据选择端(H/L)
29 sbit RW = P2^6;//读/写选择端(H/L)
30 sbit EN = P2^7;//下降沿触发
31
32
33 /*1ms延时函数*/
34 void Delay_ms(uint i)
35 {
36 1 uint j;
37 1 for(;i>0;i--)
38 1 for(j=125;j>0;j++);
39 1 }
40
41 /*读LCD函数*/
42 uchar Read_LCD(bit Style)
43 {
44 1 uchar Port;
45 1 RS = Style;
46 1 RW = 1;//读时高电平有效
47 1 EN = 1;
48 1 Port = LCD1602_DATA_PORT;
49 1 EN = 0;
50 1 RS = ~Style;
51 1 RW = 0;
52 1 return Port;
53 1 }
54
55 /*检测忙碌标志位函数*/
C51 COMPILER V7.07 LCD_1602 05/14/2009 21:13:16 PAGE 2
56 void Check_BusyFlag()
57 {
58 1 uint Retry;
59 1 for(Retry=1000;Retry>0;Retry--)
60 1 {
61 2 if(Read_LCD(0)&0x80==0) break;//BusyFlag=0表示不忙碌
62 2 }
63 1 }
64
65 /*写LCD函数*/
66 #define LCD_Command 0 //写指令宏定义
67 #define LCD_Data 1 //写数据宏定义
68 void Write_LCD(bit Style,uchar Data )
69 {
70 1 Check_BusyFlag();//写LCD前要先检测忙碌标志位,而读LCD前不用检测该位
71 1 RS = Style;
72 1 RW = 0;//写时低电平有效
73 1 EN = 1;
74 1 LCD1602_DATA_PORT = Data;
75 1 EN = 0;
76 1 RS = ~Style;
77 1 RW = 1;
78 1 }
79
80
81
82 /*字符显示定位函数*/
83 void Goto_XY(uchar X,uchar Y)
84 {
85 1 if(Y==0) Write_LCD(LCD_Command,0x80|X);//第一行显示
86 1 if(Y==1) Write_LCD(LCD_Command,0xC0|X);//第二行显示
87 1 }
88
89 /*输出并显示字符函数*/
90 void Output_String(uchar *Str)
91 {
92 1 while(*Str!='\0')//不为空字符串时输出
93 1 {
94 2 Write_LCD(LCD_Data,*Str);
95 2 Str++;
96 2 //Delay_ms(1);//加延时可以实现打字效果
97 2 }
98 1 }
99
100 /*初始化LCD函数*/
101 void Init_LCD()
102 {
103 1
104 1 Write_LCD(LCD_Command,0x38);//写指令0x38h(16x2显示,5x7点阵,8位数据接口)
105 1 Write_LCD(LCD_Command,0x38);
106 1 Write_LCD(LCD_Command,0x08);//关闭显示
107 1 Write_LCD(LCD_Command,0x01);//清屏
108 1 Write_LCD(LCD_Command,0x0C);//开启显示且显示光标
109 1 }
110
111 /* 二、DS12C887驱动程序
112 端口引脚定义(因特尔模式)
113 sfr DS12C887_DATA_PORT = 0x90;//P1口
114 sbit ALE = P2^4;//AS Pin
115 sbit RD_ = P2^3;//DS Pin
116 sbit WR_ = P2^2;//R/W pin
117 sbit CS = P2^1;//CS Pin
C51 COMPILER V7.07 LCD_1602 05/14/2009 21:13:16 PAGE 3
118
119 地址替换函数
120 因为DS12C887的基地址为7F00H
121 uint Replay_Address(uchar Address)
122 {
123 uint Replay;
124 Replay = 0x7F00 + Address;
125 return Replay;
126 }
127
128 读DS12C887函数
129 uchar Read_DS12C887(uint Address)
130 {
131 uchar Port;
132 ALE = 1;
133 RD_ = 1;
134 WR_ = 1;
135 CS = 0;
136 DS12C887_DATA_PORT = Address;//读取地址
137
138 ALE = 0;
139 RD_ = 0;
140 Port = DS12C887_DATA_PORT;//读取数据
141 RD_ = 1;
142 CS = 1;
143 ALE = 1;
144 return Port;
145 }
146
147 写DS12C887函数
148 void Write_DS12C887(uint Address,uchar Data)
149 {
150 ALE = 1;
151 RD_ = 1;
152 WR_ = 1;
153 CS = 0;
154 DS12C887_DATA_PORT = Address;
155
156 ALE = 0;
157 WR_ = 0;
158 DS12C887_DATA_PORT = Data;
159 WR_ = 1;
160 CS = 1;
161 ALE = 1;
162 }
163
164 初始化DS12C887函数
165 void Init_DS12C887()
166 {
167 Write_DS12C887(Replay_Address(0x0A),0x20);//对寄存器A进行设置:打开振荡器并使RTC计时;SWQ禁止
168 Write_DS12C887(Replay_Address(0x0B),0x06);//对寄存器B进行设置:时钟、日历格式为二进制;24小时模式
169 //Write_DS12C887(Replay_Address(0x0C),0x06);//对寄存器C进行设置:
170 if(Read_DS12C887(Replay_Address(0x0D)) == 0)//读寄存器D-bit7,如为0则DS12C887内部锂电池电能耗尽,并在LCD_1
-602显示"Warning:Battery Few"
171 {
172 Init_LCD();
173 Goto_XY(4,0);
174 Output_String("Warning:");
175 Goto_XY(2,1);
176 Output_String("Battery Few");
177 while(1);
178 }
C51 COMPILER V7.07 LCD_1602 05/14/2009 21:13:16 PAGE 4
179 }
180
181 时间处理函数
182
183 DS12C887时间地址宏定义
184 #define Second 0x00//秒
185 #define Second_Alarm 0x01//秒闹钟
186 #define Minute 0x02//分
187 #define Minute_Alarm 0x03//分闹钟
188 #define Hour 0x04//时
189 #define Hour_Alarm 0x05//时闹钟
190 #define Week 0x06//星期
191 #define Data 0x07//日
192 #define Month 0x08//月
193 #define Year 0x09//年
194 #define Century 0x32//世纪
195
196 void Time_Process()*/
197
198 /*DS12C887+LCD_1602时钟主函数*/
199 void main()
200 {
201 1 Init_LCD();
202 1 Goto_XY(0,0);
203 1 Output_String("2009-05-14");
204 1 Goto_XY(0,1);
205 1 Output_String("09:05:20");
206 1 while(1);
207 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 213 ----
CONSTANT SIZE = 20 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 1
IDATA SIZE = ---- ----
BIT SIZE = ---- 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -