00001 /****************************************************************************/ 00018 /***************************************************************************** 00019 * * 00020 * This program is free software; you can redistribute it and/or modify * 00021 * it under the terms of the GNU General Public License as published by * 00022 * the Free Software Foundation; either version 2 of the License, or * 00023 * any later version. * 00024 * * 00025 *****************************************************************************/ 00026 #include "asuro.h" 00027 #include "i2c.h" 00028 00029 00030 00031 /****************************************************************************/ 00048 void InitI2C (void) 00049 { 00050 SDA_DDR |= (1 << SDA); 00051 SCL_DDR |= (1 << SCL); 00052 SDA_HI; 00053 SCL_HI; 00054 } 00055 00056 00057 00058 /****************************************************************************/ 00072 unsigned char WriteI2C (unsigned char byte) 00073 { 00074 unsigned char i; 00075 00076 for (i=8; i>0; i--) 00077 { 00078 if ( byte & (1<<(i-1)) ) 00079 SDA_HI; 00080 else 00081 SDA_LO; 00082 SCL_TOGGLE; 00083 } 00084 SDA_HI; 00085 00086 SDA_DDR &= ~(1 << SDA); 00087 HDEL; 00088 SCL_HI; 00089 byte = SDA_PIN & (1 << SDA); 00090 00091 HDEL; 00092 SCL_LO; 00093 SDA_DDR |= (1 << SDA); 00094 HDEL; 00095 00096 return (byte == 0); 00097 } 00098 00099 00100 00101 /****************************************************************************/ 00115 unsigned char ReadI2C(unsigned char ack) 00116 { 00117 unsigned char i, byte = 0; 00118 00119 SDA_HI; 00120 SDA_DDR &= ~(1 << SDA); 00121 00122 for (i=0; i<8; i++) 00123 { 00124 HDEL; 00125 SCL_HI; 00126 byte <<= 1; 00127 byte |= (SDA_PIN & (1 << SDA)) >> SDA; 00128 HDEL; 00129 SCL_LO; 00130 } 00131 00132 SDA_DDR |= (1 << SDA); 00133 00134 if (ack) 00135 SDA_LO; // ack 00136 else 00137 SDA_HI; // nak 00138 00139 SCL_TOGGLE; 00140 SDA_HI; 00141 00142 return byte; 00143 } 00144 00145 00146 00147 /****************************************************************************/ 00159 unsigned char StartI2C (unsigned char device) 00160 { 00161 I2C_START; 00162 return WriteI2C (device); 00163 } 00164 00165 00166 00167 /****************************************************************************/ 00179 void StopI2C (void) 00180 { 00181 SDA_LO; 00182 I2C_STOP; 00183 }