?? wl007.lst
字號:
C51 COMPILER V7.20 WL007 08/19/2005 20:34:44 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE WL007
OBJECT MODULE PLACED IN wl007.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE wl007.C BROWSE DEBUG OBJECTEXTEND
line level source
1 /*******************************************************************************
2 * 標題: ME300系列單片機開發系統演示程序 - 讀寫AT24C02演示程序 *
3 * 硬件: ME300A+ *
4 * 文件: wl007.C *
5 * 日期: 2004-1-5 *
6 * 版本: 1.0 *
7 * 作者: 偉納電子 - Freeman *
8 * 郵箱: freeman@willar.com *
9 * 網站: http://www.willar.com *
10 ********************************************************************************
11 * 描述: *
12 * 讀寫AT24C02演示程序 *
13 * *
14 * *
15 * *
16 ********************************************************************************
17 * 【版權】 Copyright(C)偉納電子 www.willar.com All Rights Reserved *
18 * 【聲明】 此程序僅用于學習與參考,引用請注明版權和作者信息! *
19 *******************************************************************************/
20
21
22 #include <reg51.h>
23 #include <intrins.h>
24
25 #define OP_READ 0xa1 // 器件地址以及讀取操作
26 #define OP_WRITE 0xa0 // 器件地址以及寫入操作
27 #define MAX_ADDR 0x7f // AT24C02最大地址
28
29 unsigned char code dis_code[] = {0x7e,0xbd,0xdb,0xe7,0xdb,0xbd,0x7e,0xff};
30 // 寫入到AT24C01的數據串
31
32 sbit SDA = P1^3;
33 sbit SCL = P3^3;
34
35
36 void start();
37 void stop();
38 unsigned char shin();
39 bit shout(unsigned char write_data);
40 unsigned char read_random(unsigned char random_addr);
41 void write_byte( unsigned char addr, unsigned char write_data);
42 void fill_byte(unsigned char fill_data);
43 void delayms(unsigned char ms);
44
45 main(void)
46 {
47 1 unsigned char i;
48 1 SDA = 1;
49 1 SCL = 1;
50 1 fill_byte(0xff); // 全部填充0xff
51 1
52 1 for(i = 0 ; i < 8; i++) //寫入顯示代碼到AT24Cxx
53 1 {
54 2 write_byte(i, dis_code[i]);
55 2 }
C51 COMPILER V7.20 WL007 08/19/2005 20:34:44 PAGE 2
56 1
57 1 i = 0;
58 1 while(1)
59 1 {
60 2
61 2 P0 = read_random(i); // 循環讀取24Cxx內容,并輸出到P0口
62 2 i++;
63 2 i &= 0x07; // 循環讀取范圍為0x00~0x07
64 2 delayms(250);
65 2 }
66 1 }
67
68 void start()
69 // 開始位
70 {
71 1 SDA = 1;
72 1 SCL = 1;
73 1 _nop_();
74 1 _nop_();
75 1 SDA = 0;
76 1 _nop_();
77 1 _nop_();
78 1 _nop_();
79 1 _nop_();
80 1 SCL = 0;
81 1 }
82
83 void stop()
84 // 停止位
85 {
86 1 SDA = 0;
87 1 _nop_();
88 1 _nop_();
89 1 SCL = 1;
90 1 _nop_();
91 1 _nop_();
92 1 _nop_();
93 1 _nop_();
94 1 SDA = 1;
95 1 }
96
97 unsigned char shin()
98 // 從AT24Cxx移入數據到MCU
99 {
100 1 unsigned char i,read_data;
101 1 for(i = 0; i < 8; i++)
102 1 {
103 2 SCL = 1;
104 2 read_data <<= 1;
105 2 read_data |= (unsigned char)SDA;
106 2 SCL = 0;
107 2 }
108 1 return(read_data);
109 1 }
110 bit shout(unsigned char write_data)
111 // 從MCU移出數據到AT24Cxx
112 {
113 1 unsigned char i;
114 1 bit ack_bit;
115 1 for(i = 0; i < 8; i++) // 循環移入8個位
116 1 {
117 2 SDA = (bit)(write_data & 0x80);
C51 COMPILER V7.20 WL007 08/19/2005 20:34:44 PAGE 3
118 2 _nop_();
119 2 SCL = 1;
120 2 _nop_();
121 2 _nop_();
122 2 SCL = 0;
123 2 write_data <<= 1;
124 2 }
125 1 SDA = 1; // 讀取應答
126 1 _nop_();
127 1 _nop_();
128 1 SCL = 1;
129 1 _nop_();
130 1 _nop_();
131 1 _nop_();
132 1 _nop_();
133 1 ack_bit = SDA;
134 1 SCL = 0;
135 1 return ack_bit; // 返回AT24Cxx應答位
136 1 }
137
138 void write_byte(unsigned char addr, unsigned char write_data)
139 // 在指定地址addr處寫入數據write_data
140 {
141 1 start();
142 1 shout(OP_WRITE);
143 1 shout(addr);
144 1 shout(write_data);
145 1 stop();
146 1 delayms(10); // 寫入周期
147 1 }
148
149 void fill_byte(unsigned char fill_data)
150 // 填充數據fill_data到EEPROM內
151 {
152 1 unsigned char i;
153 1 for(i = 0; i < MAX_ADDR; i++)
154 1 {
155 2 write_byte(i, fill_data);
156 2 }
157 1
158 1 }
159
160 unsigned char read_current()
161 // 在當前地址讀取
162 {
163 1 unsigned char read_data;
164 1 start();
165 1 shout(OP_READ);
166 1 read_data = shin();
167 1 stop();
168 1 return read_data;
169 1 }
170
171 unsigned char read_random(unsigned char random_addr)
172 // 在指定地址讀取
173 {
174 1 start();
175 1 shout(OP_WRITE);
176 1 shout(random_addr);
177 1 return(read_current());
178 1 }
179
C51 COMPILER V7.20 WL007 08/19/2005 20:34:44 PAGE 4
180 void delayms(unsigned char ms)
181 // 延時子程序
182 {
183 1 unsigned char i;
184 1 while(ms--)
185 1 {
186 2 for(i = 0; i < 120; i++);
187 2 }
188 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 246 ----
CONSTANT SIZE = 8 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 3
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -