54 void i2c_enable(
void);
55 void i2c_disable(
void);
57 unsigned i2c_read(
int send_ack);
58 int i2c_write(
unsigned);
61 #define I2C_PxDIR P3DIR
63 #define I2C_PxOUT P3OUT
64 #define I2C_PxSEL P3SEL
72 #define SDA_0() (I2C_PxDIR |= BV(SDA))
73 #define SDA_1() (I2C_PxDIR &= ~BV(SDA))
74 #define SDA_IS_1 (I2C_PxIN & BV(SDA))
76 #define SCL_0() (I2C_PxDIR |= BV(SCL))
77 #define SCL_1() (I2C_PxDIR &= ~BV(SCL))
78 #define SCL_IS_1 (I2C_PxIN & BV(SCL))
83 #define SCL_WAIT_FOR_1() do{}while (!SCL_IS_1)
85 #define delay_4_7us() do{ _NOP(); _NOP(); _NOP(); _NOP(); \
86 _NOP(); _NOP(); _NOP(); _NOP(); \
87 _NOP(); _NOP(); _NOP(); _NOP(); }while(0)
89 #define delay_4us() do{ _NOP(); _NOP(); _NOP(); _NOP(); \
90 _NOP(); _NOP(); _NOP(); _NOP(); \
91 _NOP(); _NOP(); }while(0)
93 static unsigned char old_pxsel, old_pxout, old_pxdir;
102 unsigned char sda_scl = BV(SDA)|BV(SCL);
104 old_pxsel = I2C_PxSEL & sda_scl;
105 old_pxout = I2C_PxOUT & sda_scl;
106 old_pxdir = I2C_PxDIR & sda_scl;
110 I2C_PxSEL &= ~sda_scl;
112 I2C_PxOUT &= ~sda_scl;
114 I2C_PxDIR |= BV(SCL);
115 I2C_PxDIR &= ~BV(SDA);
125 unsigned char not_sda_scl = ~(BV(SDA)|BV(SCL));
127 I2C_PxDIR = (I2C_PxDIR & not_sda_scl) | old_pxdir;
128 I2C_PxOUT = (I2C_PxOUT & not_sda_scl) | old_pxout;
129 I2C_PxSEL = (I2C_PxSEL & not_sda_scl) | old_pxsel;
144 for (n = 0; n < 100000 && !SCL_IS_1; n++)
171 i2c_write(
unsigned _c)
173 unsigned char c = _c;
178 for (i = 0; i < 8; i++, c <<= 1) {
191 for (n = 0; n < 250000; n++) {
204 i2c_read(
int send_ack)
207 unsigned char c = 0x00;
210 for (i = 0; i < 8; i++) {