?? readwriteat24c08.lst
字號:
C51 COMPILER V7.50 READWRITEAT24C08 09/19/2006 14:43:25 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE READWRITEAT24C08
OBJECT MODULE PLACED IN ReadWriteAT24C08.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ReadWriteAT24C08.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg51.h>
2
3 // 對AT24C08的讀、寫
4 // extern void DelayMs(unsigned int);
5 // extern void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
6 // extern void WriteI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
7
8 /***************************************************************************/
9 #define WriteDeviceAddress 0xa0 //寫器件地址
10 #define ReadDviceAddress 0xa1 //讀器件地址
11
12 /***************************************************************************/
13 sbit SCL=P1^0; //I2C時鐘線SCL
14 sbit SDA=P1^1; //I2C數(shù)據(jù)線SDA
15 sbit DOG=P3^7; //程序運(yùn)行標(biāo)志及數(shù)據(jù)讀寫正確標(biāo)志
16
17 /***************************************************************************/
18 void DelayMs(unsigned int number) //延時子程序,延時大小取決于工作頻率和number值
19 {
20 1 unsigned char temp;
21 1 for(;number!=0;number--,DOG=!DOG) //循環(huán),DOG用于輸出狀態(tài)信號
22 1 {
23 2 for(temp=112;temp!=0;temp--) //空循環(huán)
24 2 {
25 3 }
26 2 }
27 1 }
28
29 /***************************************************************************/
30 void Start() //起始信號子程序
31 {
32 1 SDA=1;
33 1 DelayMs(1); //延時
34 1 SCL=1;
35 1 DelayMs(1);
36 1 SDA=0;
37 1 DelayMs(1);
38 1 SCL=0;
39 1 DelayMs(1);
40 1 }
41
42 /***************************************************************************/
43 void Stop() //終止信號子程序
44 {
45 1 SCL=0;
46 1 DelayMs(1);
47 1 SDA=0;
48 1 DelayMs(1);
49 1 SCL=1;
50 1 DelayMs(1);
51 1 SDA=1;
52 1 DelayMs(1);
53 1 }
54
55 /***************************************************************************/
C51 COMPILER V7.50 READWRITEAT24C08 09/19/2006 14:43:25 PAGE 2
56 void Ack() //發(fā)送應(yīng)答位子程序
57 {
58 1 SDA=0;
59 1 DelayMs(1);
60 1 SCL=1;
61 1 DelayMs(1);
62 1 SCL=0;
63 1 DelayMs(1);
64 1 SDA=1;
65 1 DelayMs(1);
66 1 }
67
68 /***************************************************************************/
69 void NoAck() //發(fā)送非應(yīng)答位子程序
70 {
71 1 SDA=1;
72 1 DelayMs(1);
73 1 SCL=1;
74 1 DelayMs(1);
75 1 SCL=0;
76 1 DelayMs(1);
77 1 }
78
79 /***************************************************************************/
80 bit TestAck() //應(yīng)答位檢查子程序
81 {
82 1 bit ErrorBit;
83 1 SDA=1;
84 1 DelayMs(1);
85 1 SCL=1;
86 1 DelayMs(1);
87 1 ErrorBit=SDA; //讀入數(shù)據(jù)線上的應(yīng)答狀態(tài)
88 1 DelayMs(1);
89 1 SCL=0;
90 1 DelayMs(1);
91 1 return(ErrorBit); //返回應(yīng)答狀態(tài),0為正常應(yīng)答信號,1為非應(yīng)答信號
92 1 }
93
94 /***************************************************************************/
95 bit Write8Bit(unsigned char input) //寫一個字節(jié)數(shù)據(jù)子程序
96 { //input為待發(fā)送的數(shù)據(jù)
97 1 unsigned char temp;
98 1 for(temp=8;temp!=0;temp--) //循環(huán)移位,逐位發(fā)送數(shù)據(jù)
99 1 {
100 2 SDA=(bit)(input&0x80); //取數(shù)據(jù)的最高位
101 2 DelayMs(1);
102 2 SCL=1;
103 2 DelayMs(1);
104 2 SCL=0;
105 2 DelayMs(1);
106 2 input=input<<1; //左移一位
107 2 }
108 1 return 1;
109 1 }
110
111 /***************************************************************************/
112 void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
113 { //寫n個字節(jié)數(shù)據(jù)子程序
114 1 Start(); //啟動
115 1 Write8Bit(WriteDeviceAddress); //寫寫器件的尋址地址
116 1 TestAck(); //應(yīng)答檢查
117 1 Write8Bit(RomAddress); //寫入I2C器件內(nèi)部的數(shù)據(jù)存儲首地址
C51 COMPILER V7.50 READWRITEAT24C08 09/19/2006 14:43:25 PAGE 3
118 1 TestAck(); //應(yīng)答檢查
119 1 for(;number!=0;number--) //循環(huán),逐個字節(jié)發(fā)送
120 1 {
121 2 Write8Bit(*Wdata); //寫一個字節(jié)
122 2 TestAck(); //應(yīng)答檢查
123 2 Wdata++; //指針增加,指向下一個數(shù)據(jù)
124 2 }
125 1 Stop(); //停止
126 1 DelayMs(10);
127 1 }
128
129 /***************************************************************************/
130 unsigned char Read8Bit() //讀一個字節(jié)數(shù)據(jù)子程序
131 {
132 1 unsigned char temp,rbyte=0;
133 1 for(temp=8;temp!=0;temp--) //循環(huán),逐位讀入字節(jié)數(shù)據(jù)
134 1 {
135 2 SCL=1;
136 2 DelayMs(1);
137 2 rbyte=rbyte<<1; //左移一位
138 2 DelayMs(1);
139 2 rbyte=rbyte|((unsigned char)(SDA)); //數(shù)據(jù)線SDA上的數(shù)據(jù)存入rbyte的最低位
140 2 SCL=0;
141 2 DelayMs(1);
142 2 }
143 1 return(rbyte); //返回讀入的字節(jié)數(shù)據(jù)
144 1 }
145
146 /***************************************************************************/
147 void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)
148 { //讀n個字節(jié)數(shù)據(jù)子程序
149 1 Start(); //啟動
150 1 Write8Bit(WriteDeviceAddress); //寫寫器件的尋址地址
151 1 TestAck(); //應(yīng)答檢查
152 1 Write8Bit(RomAddress); //寫I2C器件內(nèi)部數(shù)據(jù)的讀取首地址
153 1 TestAck(); //應(yīng)答檢查
154 1 Start(); //重新啟動
155 1 Write8Bit(ReadDviceAddress); //寫讀器件的尋址地址
156 1 TestAck(); //應(yīng)答檢查
157 1 while(bytes!=1) //循環(huán)讀入字節(jié)數(shù)據(jù)
158 1 {
159 2 *RamAddress=Read8Bit(); //讀入一個字節(jié)
160 2 Ack(); //應(yīng)答
161 2 RamAddress++; //地址指針遞增
162 2 bytes--; //待讀入數(shù)據(jù)個數(shù)遞減
163 2 }
164 1 *RamAddress=Read8Bit(); //讀入最后一個字節(jié)數(shù)據(jù)
165 1 NoAck(); //非應(yīng)答
166 1 Stop(); //停止
167 1 }
168
169 void main()
170 {
171 1 unsigned char writeByte[8]={0xC0,0x34,0x12,0x11,0x22,0x01,0x00,0x00};
172 1 //需要寫的8個字節(jié)USB數(shù)據(jù)ID
173 1 unsigned char readByte[8]; //用于存讀入的8個字節(jié)數(shù)據(jù)
174 1 unsigned char *addw; //寫數(shù)據(jù)指針操作
175 1 unsigned char *addr; //讀數(shù)據(jù)指針操作
176 1 unsigned char i;
177 1 unsigned char ok=0;
178 1 bit write=1; //讀寫標(biāo)志
179 1 DOG=0;
C51 COMPILER V7.50 READWRITEAT24C08 09/19/2006 14:43:25 PAGE 4
180 1 while(1)
181 1 {
182 2 if(write==1) //當(dāng)write==1時,執(zhí)行寫和讀操作
183 2 {
184 3 addw=writeByte; //寫地址映射
185 3 addr=readByte; //讀地址映射
186 3 WriteI2C(addw,0x00,8); //寫數(shù)據(jù)
187 3 ReadI2C(addr,0x00,8); //讀數(shù)據(jù)
188 3 for(i=0;i<8;i++) //判斷每個字節(jié)讀寫是否一致
189 3 {
190 4 if(writeByte[i]==readByte[i])
191 4 {
192 5 ok++;
193 5 }
194 4 }
195 3 if(ok==8)
196 3 {
197 4 DOG=1; //當(dāng)讀寫一致時,P3.7輸出高電平
198 4 }
199 3 else
200 3 {
201 4 DOG=0; //當(dāng)讀寫不一致時,P3.7輸出低電平
202 4 }
203 3 write=0; //置write==0,讀寫完畢
204 3 }
205 2 }
206 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 440 ----
CONSTANT SIZE = 8 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 33
IDATA SIZE = ---- ----
BIT SIZE = ---- 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -