📄 lcdand24c02.lst
字号:
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE LCDAND24C02
OBJECT MODULE PLACED IN lcdand24c02.OBJ
COMPILER INVOKED BY: D:\Keil\C51\BIN\C51.EXE lcdand24c02.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //AT24C02 EEPROM drive program
2 //for 51 mcu with lcd1602 as display
3 //designed by zhaoliang
4 //2005-6-14 21:02
5
6 #include <reg51.h>
7 #include <intrins.h>
8 /********************************************************************/
9 //lcd part
10 #define LINE1 0
11 #define LINE2 1
12 #define LINE1_HEAD 0x80
13 #define LINE2_HEAD 0xC0
14 #define LCD_DELAY_TIME 40
15 #define DATA_MODE 0x38
16 #define OPEN_SCREEN 0x0C
17 #define DISPLAY_ADDRESS 0x80
18 #define CLEARSCREEN LCD_en_command(0x01)
19 //common part
20 #define HIGH 1
21 #define LOW 0
22 #define TRUE 1
23 #define ZERO 0
24 //at24c02 part
25 #define WRITE24C02 0xA0
26 #define READ24C02 0xA1
27 //I2C part
28 #define ACK 0
29 #define NO_ACK 1
30 #define MSB 0x80
31 /********************************************************************/
32 //change this part at different board
33 #define LCDIO P0
34 sbit LCD1602_RS=P3^5; //data command select 1 data 0 command pin 4
35 sbit LCD1602_RW=P3^6; //read write select 1 read 0 write pin 5
36 sbit LCD1602_EN=P3^7; //LCD enable signal pin 6
37
38 sbit SDA=P2^1; //AT24C02 serial data pin 5
39 sbit SCLK=P2^0; //AT24C02 serial clock pin 6
40 /********************************************************************/
41 void LCD_delay(void);//lcd delay function
42 void LCD_en_command(unsigned char command);//write command function
43 void LCD_en_dat(unsigned char temp);//write data function
44 void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
45 void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
46 void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
47 void LCD_init(void);//lcd initize function
48 /********************************************************************/
49 void I2C_delay(void);//I2C delay function
50 void I2C_start(void);//I2C start function
51 void I2C_stop(void);//I2C stop function
52 void I2C_send_ack(bit k);//I2C send responsion function
53 void I2C_write_byte(unsigned char dat);//I2C bus write byte function
54 unsigned char I2C_read_byte(void);//I2C bus read byte function
55 /********************************************************************/
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 2
56 void AT24C02_write(unsigned char address,unsigned char dat);//write 24c02 information function
57 unsigned char AT24C02_read(unsigned char address);//read 24c02 information function
58 /********************************************************************/
59 void Mcu_init(void);//system initize funcition
60 void delay_nms(unsigned int n);//delay functinon
61 unsigned char count[2];
62 /********************************************************************/
63 void main()
64 {
65 1 unsigned char temp;
66 1 Mcu_init();
67 1 while(1)
68 1 {
69 2 AT24C02_write(0x00,count[1]);
70 2 temp=AT24C02_read(0x00);
71 2 LCD_write_string(0x00,LINE1,"AT24C02 TEST");
72 2 LCD_write_char(0x0e,LINE1,(temp/10)|0x30);
73 2 LCD_write_char(0x0f,LINE1,(temp%10)|0x30);
74 2 LCD_write_string(0x00,LINE2,"TIMER0 JISHU");
75 2 LCD_write_char(0x0e,1,(count[1]/10)|0x30);
76 2 LCD_write_char(0x0f,1,(count[1]%10)|0x30);
77 2 AT24C02_write(0x00,count[1]);
78 2 }
79 1 }
80 /***********************************************************************/
81 void timer0(void) interrupt 1 using 1
82 {
83 1 TH0=-(12000/256);
84 1 TL0=-(12000%256);
85 1 count[0]=count[0]+1;
86 1 if(count[0]==100)
87 1 {
88 2 count[0]=0;
89 2 count[1]=count[1]+1;
90 2 if(count[1]==99)
91 2 count[1]=0;
92 2 }
93 1 }
94 /***********************************************************************/
95 void Mcu_init(void)
96 {
97 1 TMOD=0x11;
98 1 TH0=-(12000/256);
99 1 TL0=-(12000%256);
100 1 EA=HIGH;
101 1 ET0=HIGH;
102 1 TR0=HIGH;
103 1 LCD_init();
104 1 }
105 /***********************************************************************/
106 /******************************** I2C PART **************************/
107 void I2C_delay(void)
108 {
109 1 _nop_();_nop_();_nop_();_nop_();
110 1 }
111 /***********************************************************************/
112 void I2C_start(void)
113 {
114 1 SDA=HIGH;
115 1 _nop_();
116 1 SCLK=HIGH;
117 1 _nop_();
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 3
118 1 SDA=LOW;
119 1 _nop_();
120 1 SCLK=LOW;
121 1 _nop_();
122 1 }
123 /***********************************************************************/
124 void I2C_stop(void)
125 {
126 1 SDA=LOW;
127 1 _nop_();
128 1 SCLK=HIGH;
129 1 _nop_();
130 1 SDA=HIGH;
131 1 _nop_();
132 1 SCLK=LOW;
133 1 _nop_();
134 1 }
135 /***********************************************************************/
136 void I2C_send_ack(bit k)
137 {
138 1 SDA=k;
139 1 I2C_delay();
140 1 SCLK=HIGH;
141 1 I2C_delay();
142 1 SCLK=LOW;
143 1 }
144 /***********************************************************************/
145 void I2C_write_byte(unsigned char dat)
146 {
147 1 unsigned char i;
148 1 for (i=8;i>0;i--)
149 1 {
150 2 SCLK=LOW;
151 2 I2C_delay();
152 2 SDA=(bit)(dat&MSB);
153 2 dat<<=1;
154 2 I2C_delay();
155 2 SCLK=HIGH;
156 2 I2C_delay();
157 2 }
158 1 SCLK=LOW;
159 1 }
160 /***********************************************************************/
161 unsigned char I2C_read_byte(void)
162 {
163 1 unsigned char i,dat;
164 1 for (i=0;i<8;i++)
165 1 {
166 2 SCLK=LOW;
167 2 I2C_delay();
168 2 SDA=HIGH;
169 2 I2C_delay();
170 2 SCLK=HIGH;
171 2 dat<<=1;
172 2 I2C_delay();
173 2 if(SDA)
174 2 dat++;
175 2 }
176 1 SCLK=LOW;
177 1
178 1 return (dat);
179 1 }
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 4
180 /***********************************************************************/
181 /************************ 24C02 PART **********************************/
182 void AT24C02_write(unsigned char address,unsigned char dat)
183 {
184 1 unsigned char temp;
185 1 temp=dat/10;
186 1 temp<<=4;
187 1 temp=dat%10+temp;
188 1
189 1 I2C_start();
190 1 I2C_write_byte(WRITE24C02);
191 1 I2C_send_ack(ACK);
192 1 I2C_write_byte(address);
193 1 I2C_send_ack(ACK);
194 1 I2C_write_byte(temp);
195 1 I2C_send_ack(NO_ACK);
196 1 I2C_stop();
197 1 }
198 /***********************************************************************/
199 unsigned char AT24C02_read(unsigned char address)
200 {
201 1 unsigned char temp,dat;
202 1 I2C_start();
203 1 I2C_write_byte(WRITE24C02);
204 1 I2C_send_ack(ACK);
205 1 I2C_write_byte(address);
206 1 I2C_send_ack(NO_ACK);
207 1 I2C_stop();
208 1
209 1 I2C_start();
210 1 I2C_write_byte(READ24C02);
211 1 I2C_send_ack(ACK);
212 1 dat=I2C_read_byte();
213 1 I2C_send_ack(NO_ACK);
214 1 I2C_stop();
215 1
216 1 temp=dat/16;
217 1 dat=dat%16;
218 1 dat=dat+temp*10;
219 1
220 1 return (dat);
221 1 }
222 /******************** LCD PART *************************************/
223 void LCD_delay(void)
224 {
225 1 unsigned char i;
226 1 for(i=LCD_DELAY_TIME;i>ZERO;i--)//be sure lcd reset
227 1 ;
228 1 }
229 /********************************************************************/
230 void LCD_en_command(unsigned char command)
231 {
232 1 LCDIO=command;
233 1 LCD1602_RS=LOW;
234 1 LCD1602_RW=LOW;
235 1 LCD1602_EN=LOW;
236 1 LCD_delay();
237 1 LCD1602_EN=HIGH;
238 1 }
239 /********************************************************************/
240 void LCD_en_dat(unsigned char dat)
241 {
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 5
242 1 LCDIO=dat;
243 1 LCD1602_RS=HIGH;
244 1 LCD1602_RW=LOW;
245 1 LCD1602_EN=LOW;
246 1 LCD_delay();
247 1 LCD1602_EN=HIGH;
248 1 }
249 /********************************************************************/
250 void LCD_set_xy( unsigned char x, unsigned char y )
251 {
252 1 unsigned char address;
253 1 if (y == LINE1)
254 1 address = LINE1_HEAD + x;
255 1 else
256 1 address = LINE2_HEAD + x;
257 1 LCD_en_command(address);
258 1 }
259 /********************************************************************/
260 void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
261 {
262 1 LCD_set_xy( x, y );
263 1 LCD_en_dat(dat);
264 1 }
265 /********************************************************************/
266 void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
267 {
268 1 LCD_set_xy( X, Y ); //set address
269 1 while (*s) // write character
270 1 {
271 2 LCDIO=*s;
272 2 LCD_en_dat(*s);
273 2 s ++;
274 2 }
275 1 }
276 /********************************************************************/
277 void LCD_init(void)
278 {
279 1 CLEARSCREEN;//clear screen
280 1 LCD_en_command(DATA_MODE);//set 8 bit data transmission mode
281 1 LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
282 1 LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address
283 1 CLEARSCREEN;//clear screen
284 1 }
285 /********************************************************************/
286 /******************* OTHER PART ***********************************/
287 void delay_nms(unsigned int n)
288 {
289 1 unsigned int i=0,j=0;
290 1 for (i=n;i>0;i--)
291 1 for (j=0;j<1140;j++)
292 1 ;
293 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 526 ----
CONSTANT SIZE = 26 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 2 1
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
C51 COMPILER V8.02 LCDAND24C02 04/24/2009 22:29:52 PAGE 6
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -