📄 i2c.lst
字号:
227 3 ResSDA2;
228 3 }
229 2
230 2 SetSCL2;
231 2 while(!GetSCL2);
232 2 ResSCL2;
233 2 if(!noack)
234 2 {
235 3 SetSDA2;
236 3 }
237 2 }
238 1
239 1 return readdata;
240 1
C51 COMPILER V8.05a I2C 07/03/2008 18:27:38 PAGE 5
241 1 }
242
243 // lun hahaha
244 //---------------------------------------------------------------------------
245 BYTE IIC_ReadByte(BIT ch, BIT noack)
246 {
247 1 BYTE i,readdata=0;
248 1
249 1 if (!ch)
250 1 {
251 2 for(i = 0; i < 8; i++)
252 2 {
253 3 readdata <<= 1;
254 3
255 3 SetSDA1;
256 3 SetSCL1;
257 3 while(!GetSCL1);
258 3 Delay1us(1);
259 3
260 3 if(GetSDA1)
261 3 readdata |= 0x01;
262 3 ResSCL1;
263 3 }
264 2 if(noack)
265 2 {
266 3 SetSDA1;
267 3 }
268 2 else
269 2 {
270 3 ResSDA1;
271 3 }
272 2
273 2 SetSCL1;
274 2 }
275 1
276 1 return readdata;
277 1
278 1 }
279
280 /****************************************************************************/
281 // IIC write address
282 /****************************************************************************/
283 void IICSW_Write(BIT ch, BYTE id,BOOL boolSubAddr,BYTE addr,BYTE *databuf,WORD number)
284 {
285 1 int i;
286 1 BYTE count = 0;
287 1
288 1 do
289 1 {
290 2 count++;
291 2 // Stop
292 2 IIC_Stop(ch);
293 2 Delay1us(20);
294 2 // Start
295 2 IIC_Start(ch);
296 2 // Slave ID
297 2 IIC_Send1Byte(ch,id);
298 2 if (bNoAck)
299 2 {
300 3 continue;
301 3 }
302 2 // Address
C51 COMPILER V8.05a I2C 07/03/2008 18:27:38 PAGE 6
303 2 if(boolSubAddr)
304 2 {
305 3 IIC_Send1Byte(ch,addr);
306 3 if (bNoAck)
307 3 continue;
308 3 }
309 2 // data
310 2 for(i = 0; i < number; i++)
311 2 {
312 3 IIC_Send1Byte(ch,databuf[i]);
313 3 if (bNoAck)
314 3 break;
315 3 }
316 2 if (bNoAck)
317 2 continue;
318 2 // Stop
319 2 IIC_Stop(ch);
320 2 }
321 1 while(bNoAck && (count<10));
322 1 }
323
324 /****************************************************************************/
325 // IIC Read address
326 /****************************************************************************/
327 void IICSW_Read(BIT ch, BYTE id,BOOL boolSubAddr,BYTE addr,BYTE *databuf,WORD number)
328 {
329 1 int i;
330 1 BYTE count = 0;
331 1
332 1 do
333 1 {
334 2 count++;
335 2 // Stop
336 2 IIC_Stop(ch);
337 2 Delay1us(20);
338 2 // Start
339 2 if(boolSubAddr)
340 2 {
341 3 IIC_Start(ch);
342 3 // Slave ID
343 3 IIC_Send1Byte(ch,id);
344 3 if (bNoAck)
345 3 continue;
346 3 // Address
347 3 IIC_Send1Byte(ch,addr);
348 3 if (bNoAck)
349 3 continue;
350 3 if (!ch)
351 3 {
352 4 SetSDA1;
353 4 SetSCL1;
354 4 while(!GetSCL1);
355 4 }
356 3 else
357 3 {
358 4 SetSDA2;
359 4 SetSCL2;
360 4 while(!GetSCL2);
361 4 }
362 3 }
363 2 IIC_Start(ch);
364 2 // Slave ID
C51 COMPILER V8.05a I2C 07/03/2008 18:27:38 PAGE 7
365 2 IIC_Send1Byte(ch, (id | 0x01));
366 2 if (bNoAck)
367 2 continue;
368 2 // data
369 2 if(boolSubAddr)
370 2 {
371 3 for(i = 0; i < number-1; i++)
372 3 {
373 4 databuf[i] = IIC_Read1Byte(ch,0);
374 4 }
375 3 databuf[i] = IIC_Read1Byte(ch,1);
376 3 }
377 2 else
378 2 databuf[0] = IIC_Read1Byte(ch,1);
379 2
380 2 // Stop
381 2 IIC_Stop(ch);
382 2 }
383 1 while(bNoAck && count < 10);
384 1 }
385
386 void IICSW_WriteTest(void) //test martin 28May08
387 {
388 1 /*
389 1 mSetDvdIr();
390 1 Delay1ms(100);
391 1 mClrDvdIr();
392 1 Delay1ms(100);
393 1 */
394 1 SetSCL1;
395 1 SetSDA1;
396 1
397 1 mSetDvdIr();
398 1
399 1 // Delay1ms(100);
400 1 // Delay1ms(100);
401 1 // Delay1ms(20);
402 1
403 1 ResSDA1;
404 1 ResSCL1;
405 1 mClrDvdIr();
406 1 // Delay1ms(100);
407 1 // Delay1ms(100);
408 1 // Delay1ms(20);
409 1
410 1 SetSCL1;
411 1 SetSDA1;
412 1
413 1 mSetDvdIr();
414 1
415 1 // Delay1ms(100);
416 1 // Delay1ms(100);
417 1 // Delay1ms(20);
418 1 ResSDA1;
419 1 ResSCL1;
420 1 mClrDvdIr();
421 1 // Delay1ms(100);
422 1 // Delay1ms(100);
423 1 // Delay1ms(20);
424 1
425 1
426 1 SetSCL1;
C51 COMPILER V8.05a I2C 07/03/2008 18:27:38 PAGE 8
427 1 SetSDA1;
428 1
429 1 mSetDvdIr();
430 1
431 1 // Delay1ms(100);
432 1 // Delay1ms(100);
433 1 // Delay1ms(20);
434 1 ResSDA1;
435 1 ResSCL1;
436 1 mClrDvdIr();
437 1 // Delay1ms(100);
438 1 // Delay1ms(100);
439 1 // Delay1ms(20);
440 1
441 1
442 1 }
443
444
445
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 824 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- 21
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 11
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -