?? iic.lst
字號:
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE IIC
OBJECT MODULE PLACED IN iic.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE iic.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg52.h>
2 #include "delay.h"
3 #include "iic.h"
4
5 /*啟動IIC設備*/
6 void start(void)
7 {
8 1 DelayMs(5);
9 1 SDA = 1 ;
10 1 Delay(5);
11 1 SCL = 1 ;
12 1 Delay(5);
13 1 SDA = 0 ;
14 1 Delay(5);
15 1 SCL = 0 ;
16 1 Delay(5);
17 1 }
18
19
20 /*停止IIC設備*/
21 void stop(void)
22 {
23 1 Delay(5);
24 1 SCL = 0 ;
25 1 Delay(5);
26 1 SDA = 0 ;
27 1 Delay(5);
28 1 SCL = 1 ;
29 1 Delay(5);
30 1 SDA = 1 ;
31 1 Delay(5);
32 1 DelayMs(15);
33 1 }
34
35 /*檢測ack*/
36 bit testack(void)
37 {
38 1 bit AckFlag;
39 1 Delay(5);
40 1 SDA = 1;
41 1 Delay(5);
42 1 SCL = 1;
43 1 Delay(5);
44 1 AckFlag = SDA;
45 1 SCL = 0;
46 1 Delay(5);
47 1 return(AckFlag);
48 1 }
49
50 /*發送應答信號*/
51 void sendack(void)
52 {
53 1 Delay(5);
54 1 SDA = 0;
55 1 Delay(5);
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 2
56 1 SCL = 1;
57 1 Delay(5);
58 1 SCL = 0;
59 1 Delay(5);
60 1 SDA = 1;
61 1 Delay(5);
62 1 }
63 /*發送非應答信號*/
64 void sendNack(void)
65 {
66 1 Delay(5);
67 1 SDA = 1 ;
68 1 Delay(5);
69 1 SCL = 1 ;
70 1 Delay(5);
71 1 SCL = 0 ;
72 1 Delay(5);
73 1 }
74
75 /*寫1個字節的數據*/
76 void write8bit(unsigned char T_Data)
77 {
78 1 unsigned char i ;
79 1 for(i = 8; i != 0; i--)
80 1 {
81 2 SDA = (bit)(T_Data & 0x80);
82 2 Delay(5);
83 2 SCL = 1 ;
84 2 Delay(5);
85 2 SCL = 0 ;
86 2 Delay(5);
87 2 T_Data = T_Data << 1 ;
88 2 Delay(5);
89 2 }
90 1 }
91
92 /*讀1個字節的數據*/
93 unsigned char read8bit(void)
94 {
95 1 unsigned char i = 0 ;
96 1 unsigned char R_Data = 0x00 ;
97 1
98 1 for(i = 8; i != 0; i--)
99 1 {
100 2 SDA = 1;
101 2 Delay(5);
102 2 SCL = 0 ;
103 2 Delay(5);
104 2 Delay(5);
105 2 SCL = 1 ;
106 2 R_Data = R_Data << 1 ;
107 2 R_Data = R_Data |((unsigned char)(SDA)) ;
108 2 SCL = 0 ;
109 2 Delay(5);
110 2 }
111 1 return(R_Data);
112 1 }
113
114 /*寫1個字節的數據到指定的地址*/
115 void write_I2C(unsigned char RomAddress,unsigned char T_Data)
116 {
117 1 start() ;
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 3
118 1 write8bit(WrDevAddr) ;
119 1 testack() ;
120 1 write8bit(RomAddress) ;
121 1 testack() ;
122 1 write8bit(T_Data) ;
123 1 testack() ;
124 1 stop() ;
125 1 }
126
127
128 /*讀1個字節的數據*/
129 unsigned char read_I2C(unsigned char RomAddress)
130 {
131 1 unsigned char R_Data = 0x00 ;
132 1 start() ;
133 1 write8bit(WrDevAddr) ;
134 1 testack() ;
135 1 write8bit(RomAddress) ;
136 1 testack() ;
137 1 start() ;
138 1 write8bit(RdDevAddr) ;
139 1 testack() ;
140 1 R_Data = read8bit() ;
141 1 sendNack() ;
142 1 stop() ;
143 1 return(R_Data) ;
144 1 }
145
146 /*指定的地址寫頁-8字節數據*/
147 void WriteOnePage (unsigned char addr,unsigned char *thedata) //寫入一頁8個字節到指定開始地址,可以自動
-翻頁
148 {
149 1 unsigned char i; //計數器
150 1 start(); //開始總線
151 1 write8bit(WrDevAddr); //發送控制數據
152 1 testack(); //檢查應答信息
153 1 write8bit(addr); //寫入地址
154 1 testack();
155 1 for(i=0;i<8;i++) //循環寫入第一頁的數據
156 1 {
157 2 write8bit(*thedata); //寫入數據
158 2 testack();
159 2 thedata++; //數據指針加1
160 2 }
161 1 stop(); //停止總線
162 1 }
163
164 /*指定的地址寫N(N<=8)字節數據*/
165 void WriteNByte (unsigned char addr,unsigned char *thedata,unsigned char n) //寫入N個字到指定開始地址
166 {
167 1 unsigned char i; //計數器
168 1 start(); //開始總線
169 1 write8bit(WrDevAddr); //發送控制數據
170 1 testack(); //檢查應答信息
171 1 write8bit(addr); //寫入地址
172 1 testack();
173 1 for(i=0;i<n;i++) //循環寫入第一頁的數據
174 1 {
175 2 write8bit(*thedata); //寫入數據
176 2 testack();
177 2 thedata++; //數據指針加1
178 2 }
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 4
179 1 stop(); //停止總線
180 1 }
181
182 /*指定地址,頁寫n個數據*/
183 void WritePages(unsigned char addr,unsigned char *thedata,unsigned char n)
184 {
185 1 unsigned char page=n/8;
186 1 unsigned char leave=n%8;
187 1 unsigned char i;
188 1 for(i=0;i<page;i++)
189 1 {
190 2 WriteOnePage((addr+8*i),thedata);
191 2 thedata=thedata+8;
192 2 }
193 1 WriteNByte((addr+8*page),thedata,leave);
194 1 }
195
196 /*指定地址,讀取8字節,頁讀*/
197 void ReadoOnePage(unsigned char addr,unsigned char *thedata) //連續讀取8個字節 頁
198 {
199 1 unsigned char i;
200 1 start();
201 1 write8bit(WrDevAddr);
202 1 testack();
203 1 write8bit(addr);
204 1 testack();
205 1 start();
206 1 write8bit(RdDevAddr) ;
207 1 testack();
208 1 for(i=0;i<7;i++) //8字節
209 1 {
210 2 *thedata=read8bit(); //讀取數據
211 2 sendack(); //發送應答,表示繼續讀取
212 2 thedata++; //數據指針加1
213 2 }
214 1 *thedata=read8bit(); //讀取最后一個數據
215 1 sendNack(); //發送非應答,結束讀取
216 1 stop(); //停止總線
217 1 }
218
219 /*連續讀取N個字節*/
220 void ReadNByte (unsigned char addr,unsigned char *thedata,unsigned char n)
221 {
222 1 unsigned char i;
223 1 start();
224 1 write8bit(WrDevAddr);
225 1 testack();
226 1 write8bit(addr);
227 1 testack();
228 1 start();
229 1 write8bit(RdDevAddr) ;
230 1 testack();
231 1 for(i=0;i<n-1;i++) //n字節
232 1 {
233 2 *thedata=read8bit(); //讀取數據
234 2 sendack(); //發送應答,表示繼續讀取
235 2 thedata++; //數據指針加1
236 2 }
237 1 *thedata=read8bit(); //讀取最后一個數據
238 1 sendNack(); //發送非應答,結束讀取
239 1 stop();
240 1 }
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 5
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 768 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 38
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 + -