?? i2c.lst
字號(hào):
127
128 //*************receive a byte *********//
129 unsigned char I2C_Receive_byte(void)
130 {
131 1 unsigned char i,da=0;
132 1 bit a;
133 1 for(i=0;i<8;i++)
134 1 {
135 2 SDA=1;
136 2 SCL=1;
137 2 delay(3);
138 2 a=SDA;
139 2 delay(3);
140 2 SCL=0;
141 2 if(a==1)
142 2 {
143 3 da=da<<1;
144 3 da=da|0x01;
145 3 }
146 2 else
147 2 {
148 3 da=da<<1;
149 3 }
150 2 }
151 1 return da;
152 1 }
153
154 //**********Send n bety ,n<16,page[0-7],add[0-255].error return 0********//
155 bit I2C_Send_Nbyte(unsigned char page,unsigned char add,unsigned char *buffer,unsigned char n)
156 {
157 1 unsigned char i;
158 1
159 1 I2C_start(); //send start//
160 1
161 1 I2C_Send_byte(0xa0|(page<<1)); //send device address//
162 1 if(I2C_Cack()==1)
163 1 {
164 2 I2C_stop();
165 2 return 0;
166 2 }
167 1
168 1 I2C_Send_byte(add); //send data address//
169 1 if(I2C_Cack()==1)
170 1 {
171 2 I2C_stop();
172 2 return 0;
173 2 }
174 1
175 1 for(i=0;i<n;i++) //send data//
176 1 {
177 2 I2C_Send_byte(buffer[i]);
178 2 if(I2C_Cack()==1)
179 2 {
C51 COMPILER V7.01 I2C 11/28/2003 11:13:40 PAGE 4
180 3 I2C_stop();
181 3 return 0;
182 3 }
183 2 }
184 1
185 1 I2C_stop(); //send stop//
186 1 return 1;
187 1 }
188
189
190 //***********receive n byte n<16,page[0-7],add[0-255],error return 0*********//
191 bit I2C_Receive_Nbyte(unsigned char page,unsigned char add,unsigned char *buffer,unsigned char n)
192 {
193 1 unsigned char i;
194 1
195 1 I2C_start(); //send start//
196 1
197 1 I2C_Send_byte(0xa0|(page<<1)); //send device address//
198 1 if(I2C_Cack()==1)
199 1 {
200 2 I2C_stop();
201 2 return 0;
202 2 }
203 1
204 1 I2C_Send_byte(add); //send data address//
205 1 if(I2C_Cack()==1)
206 1 {
207 2 I2C_stop();
208 2 return 0;
209 2 }
210 1
211 1 I2C_stop(); //send stop//
212 1
213 1 I2C_start(); //send start//
214 1
215 1 I2C_Send_byte(0xa1|(page<<1)); //send device address read//
216 1 if(I2C_Cack()==1)
217 1 {
218 2 I2C_stop();
219 2 return 0;
220 2 }
221 1
222 1 for(i=0;i<n;i++) //send data//
223 1 {
224 2 buffer[i]=I2C_Receive_byte();
225 2 if(i!=n)
226 2 {
227 3 I2C_ack();
228 3 }
229 2 else
230 2 {
231 3 I2C_nack();
232 3 }
233 2 }
234 1
235 1 I2C_stop(); //send stop//
236 1
237 1 return 1;
238 1 }
239
240
241 //********main************//
C51 COMPILER V7.01 I2C 11/28/2003 11:13:40 PAGE 5
242 void main(void)
243 {
244 1 unsigned buffer[8],i;
245 1 SCON = 0x52;
246 1 TMOD=0x29;
247 1 TH1=0xfd;
248 1 TL1=0xfd; //** 9600 baud **//
249 1 TR1=1; //** start timer 1 **//
250 1 TI=1;
251 1 EA=1; //** Allow UART Int **//
252 1
253 1 printf("I2Ctimer Test\n");
254 1 delay_ms(100);
255 1
256 1 /* I2C_start();
257 1
258 1 I2C_Send_byte(0x64);
259 1 if(I2C_Cack()==1)
260 1 {
261 1 I2C_stop();
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -