www.pudn.com > HT1621Control.rar > i2c_drv.i



#line 1 "Driver\i2c_drv.c" /0









 
  
#line 1 "system\reg51.h" /0






 
 
 
 
 
 
 sfr P0   = 0x80;
 sbit P0_0 = P0^0;
 sbit P0_1 = P0^1;
 sbit P0_2 = P0^2;
 sbit P0_3 = P0^3;
 sbit P0_4 = P0^4;
 sbit P0_5 = P0^5;
 sbit P0_6 = P0^6;
 sbit P0_7 = P0^7;
 
 sfr P1   = 0x90;
 sbit P1_0 = P1^0;
 sbit P1_1 = P1^1;
 sbit P1_2 = P1^2;
 sbit P1_3 = P1^3;
 sbit P1_4 = P1^4;
 sbit P1_5 = P1^5;
 sbit P1_6 = P1^6;
 sbit P1_7 = P1^7;
 
 sfr P2   = 0xA0;
 sbit P2_0 = P2^0;
 sbit P2_1 = P2^1;
 sbit P2_2 = P2^2;
 sbit P2_3 = P2^3;
 sbit P2_4 = P2^4;
 sbit P2_5 = P2^5;
 sbit P2_6 = P2^6;
 sbit P2_7 = P2^7;
 
 sfr P3   = 0xB0;
 sbit P3_0 = P3^0;
 sbit P3_1 = P3^1;
 sbit P3_2 = P3^2;
 sbit P3_3 = P3^3;
 sbit P3_4 = P3^4;
 sbit P3_5 = P3^5;
 sbit P3_6 = P3^6;
 sbit P3_7 = P3^7;
 
 sfr PSW  = 0xD0;
 sfr ACC  = 0xE0;
 sfr B    = 0xF0;
 sfr SP   = 0x81;
 sfr DPL  = 0x82;
 sfr DPH  = 0x83;
 sfr PCON = 0x87;
 sfr TCON = 0x88;
 sfr TMOD = 0x89;
 sfr TL0  = 0x8A;
 sfr TL1  = 0x8B;
 sfr TH0  = 0x8C;
 sfr TH1  = 0x8D;
 sfr IE   = 0xA8;
 sfr IP   = 0xB8;
 sfr SCON = 0x98;
 sfr SBUF = 0x99;
 
 
 
 
 sbit CY   = 0xD7;
 sbit AC   = 0xD6;
 sbit F0   = 0xD5;
 sbit RS1  = 0xD4;
 sbit RS0  = 0xD3;
 sbit OV   = 0xD2;
 sbit P    = 0xD0;
 
 
 sbit TF1  = 0x8F;
 sbit TR1  = 0x8E;
 sbit TF0  = 0x8D;
 sbit TR0  = 0x8C;
 sbit IE1  = 0x8B;
 sbit IT1  = 0x8A;
 sbit IE0  = 0x89;
 sbit IT0  = 0x88;
 
 
 sbit EA   = 0xAF;
 sbit ES   = 0xAC;
 sbit ET1  = 0xAB;
 sbit EX1  = 0xAA;
 sbit ET0  = 0xA9;
 sbit EX0  = 0xA8;
 
 
 sbit PS   = 0xBC;
 sbit PT1  = 0xBB;
 sbit PX1  = 0xBA;
 sbit PT0  = 0xB9;
 sbit PX0  = 0xB8;
 
 
 sbit RD   = 0xB7;
 sbit WR   = 0xB6;
 sbit T1   = 0xB5;
 sbit T0   = 0xB4;
 sbit INT1 = 0xB3;
 sbit INT0 = 0xB2;
 sbit TXD  = 0xB1;
 sbit RXD  = 0xB0;
 
 
 sbit SM0  = 0x9F;
 sbit SM1  = 0x9E;
 sbit SM2  = 0x9D;
 sbit REN  = 0x9C;
 sbit TB8  = 0x9B;
 sbit RB8  = 0x9A;
 sbit TI   = 0x99;
 sbit RI   = 0x98;
 
 
#line 11 "Driver\i2c_drv.c" /0
 
  
#line 1 "system\compiler.h" /0









 
 
 typedef unsigned char      Byte;
 typedef unsigned char      Uchar;
 typedef unsigned int       Uint16;
 typedef int                Int16;
 typedef float              Float16;
 typedef unsigned long int  Uint32;
 typedef long int           Int32; 
 
 
#line 12 "Driver\i2c_drv.c" /0
 
  
#line 1 "system\config.h" /0









 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 171 "system\config.h" /1
  
  
  
  
  
 
#line 177 "system\config.h" /0
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 13 "Driver\i2c_drv.c" /0
 
  
#line 1 "Driver\io_def.h" /0









 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 14 "Driver\i2c_drv.c" /0
 
  
#line 1 "Driver\i2c_drv.h" /0









 
 
 
 
 Byte I2c_ReadByte (Byte address);
 void I2c_WriteByte (Byte address, Byte bByte);
 Byte I2c_scanLSB (void);
 void I2c_getLSB (Byte bByte);
 void I2c_start (void);
 void I2c_stop (void);
 void I2c_ack (void);
 void I2c_init (void);
 void I2c_wait (void);
#line 15 "Driver\i2c_drv.c" /0
 
 














 
 Byte I2c_ReadByte (Byte address)
 {
 Byte bTemp = 0x00;
 
 I2c_start();
 I2c_getLSB(0xa0);
 I2c_getLSB(address);
 I2c_start();
 I2c_getLSB(0xa1);
 bTemp = I2c_scanLSB();
 I2c_ack();
 I2c_stop();
 return(bTemp);
 }
 














 
 void I2c_WriteByte (Byte address, Byte bByte)
 {
 I2c_start();
 I2c_getLSB(address);
 I2c_getLSB(bByte);
 I2c_stop();
 }
 














 
 Byte I2c_scanLSB (void)
 {
 Byte i;
 char bByte = 0x00,temp;
 
 for (i=0; i<8; i++){
  P2_0 = 1;
 I2c_wait();
 if (P3_4){
 temp = 0x01;
 temp <<= i;
 bByte |= temp;
 }
  P2_0 = 0;
 I2c_wait();
 }
 return (bByte);
 
 }
 














 
 void I2c_getLSB (Byte bByte)
 {
 Byte i;
 
 for (i=0; i<8; i++) 
 {
 if (bByte & 0x80) P2_1 = 1;
 else P2_1 = 0;
 bByte <<= 1;
 I2c_wait();
  P2_0 = 1;
 I2c_wait();
  P2_0 = 0;
 I2c_wait();
 }
  P2_1 = 1;
 I2c_wait();
  P2_0 = 1;
 I2c_wait();
 I2c_wait();
  P2_0 = 0;
 I2c_wait();
 }
 














 
 void I2c_start (void)
 {
  P2_1 = 1;
  P2_0 = 1;
 I2c_wait();
  P2_1 = 0;
 I2c_wait();
  P2_0 = 0;
 }
 














 
 void I2c_stop (void)
 {
  P2_1 = 0;
 I2c_wait();
  P2_0 = 1;
 I2c_wait();
  P2_1 = 1;
 I2c_wait();
 }
 














 
 void I2c_ack (void)
 {
  P2_1 = 0;
 I2c_wait();
  P2_1 = 1;
 I2c_wait();
 }
 














 
 void I2c_init (void)
 {
  P2_1 = 1;
 I2c_wait();
  P2_0 = 1;
 I2c_wait();
 }
 














 
 void I2c_wait (void)
 {
 char i;
 for(i=0; i<2; i++){
 i = i;
 }
 }