2 \author Travis Goodspeed
7 //Higher level left to client application.
9 //Based upon a neighborly example at
10 //http://codinglab.blogspot.com/2008/10/i2c-on-avr-using-bit-banging.html
25 #define I2CDELAY(x) delay(x<<4)
28 //2xx only, need 1xx compat code
29 #define CLRSDA P5OUT&=~SDA
30 #define SETSDA P5OUT|=SDA
31 #define CLRSCL P5OUT&=~SCL
32 #define SETSCL P5OUT|=SCL
34 #define READSDA (P5IN&SDA?1:0)
35 #define SETBOTH P5OUT|=(SDA|SCL)
37 #define I2C_DATA_HI() SETSDA
38 #define I2C_DATA_LO() CLRSDA
40 #define I2C_CLOCK_HI() SETSCL
41 #define I2C_CLOCK_LO() CLRSCL
43 //#warning "Using internal resistors. Won't work on 161x devices."
45 //! Inits bitbanging port, must be called before using the functions below
50 //Direction, not value, is used to set the value.
64 void I2C_WriteBit( unsigned char c )
84 unsigned char I2C_ReadBit()
91 unsigned char c = READSDA;
100 //! Send a START Condition
103 // set both to high at the same time
114 //! Send a STOP Condition
124 //! write a byte to the I2C slave device
125 unsigned char I2C_Write( unsigned char c )
129 I2C_WriteBit( c & 0x80 );
133 return I2C_ReadBit();
137 //! read a byte from the I2C slave device
138 unsigned char I2C_Read( unsigned char ack )
140 unsigned char res = 0;
145 res |= I2C_ReadBit();
159 //! Handles a monitor command.
160 void i2chandle(unsigned char app,
172 if(len>0) //optional parameter of length
174 if(!len) //default value of 1
177 cmddata[i]=I2C_Read(1); //Always acknowledge
178 txdata(app,verb,len);
183 cmddata[0]+=I2C_Write(cmddata[i]);