Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
my_i2c.cpp
00001 #include "mbed.h" 00002 #include "my_i2c.h" 00003 00004 DigitalInOut sda(p9); 00005 DigitalInOut scl(p10); 00006 00007 00008 #define SDA(state) if (state) { \ 00009 sda.input (); \ 00010 sda = 1; \ 00011 } else { \ 00012 sda = 0; \ 00013 sda.output (); \ 00014 } 00015 00016 #define SCL(state) if (state) { \ 00017 scl.input (); \ 00018 scl = 1; \ 00019 } else { \ 00020 scl = 0; \ 00021 scl.output (); \ 00022 } 00023 00024 #define IO_DELAY() wait_us (1) 00025 00026 00027 void my_i2cWriteByte (unsigned char byte); 00028 bool my_i2cGetAck (); 00029 unsigned char my_i2cReadByte (); 00030 00031 00032 bool 00033 my_i2cStart (unsigned char slaveAddress) 00034 { 00035 SDA(1); 00036 SCL(1); 00037 IO_DELAY(); 00038 SDA(0); 00039 IO_DELAY(); 00040 00041 my_i2cWriteByte (slaveAddress); 00042 00043 return my_i2cGetAck (); 00044 } 00045 00046 00047 bool 00048 my_i2cStop () 00049 { 00050 SDA(0); 00051 SCL(0); 00052 IO_DELAY(); 00053 00054 SCL(1); 00055 IO_DELAY(); 00056 00057 SDA(1); 00058 IO_DELAY(); 00059 00060 return true; 00061 } 00062 00063 00064 bool 00065 my_i2cRepeatedStart (unsigned char slaveAddress) 00066 { 00067 SCL(0); 00068 IO_DELAY(); 00069 00070 SDA(1); 00071 00072 IO_DELAY(); 00073 00074 SCL(1); 00075 00076 IO_DELAY(); 00077 00078 SDA(0); 00079 my_i2cWriteByte (slaveAddress); 00080 00081 return my_i2cGetAck (); 00082 } 00083 00084 00085 bool 00086 my_i2cWrite (unsigned char data) 00087 { 00088 my_i2cWriteByte (data); 00089 return my_i2cGetAck (); 00090 } 00091 00092 00093 unsigned char 00094 my_i2cReadByte () 00095 { 00096 unsigned char data = 0; 00097 00098 for (int i = 0x80; i > 0x00; i >>= 1) 00099 { 00100 SCL(0); 00101 SDA(1); 00102 IO_DELAY(); 00103 00104 SCL(1); 00105 IO_DELAY(); 00106 00107 if (sda) 00108 { // turn bit to 1... 00109 data |= i; 00110 } 00111 } 00112 return data; 00113 } 00114 00115 00116 unsigned char 00117 my_i2cReadAck () 00118 { 00119 unsigned char data = my_i2cReadByte (); 00120 00121 SCL(0); 00122 SDA(0); 00123 IO_DELAY(); 00124 00125 SCL(1); 00126 00127 // wait till scl is high 00128 IO_DELAY(); 00129 00130 return data; 00131 } 00132 00133 00134 unsigned char 00135 my_i2cReadNak () 00136 { 00137 unsigned char data = my_i2cReadByte (); 00138 00139 SCL(0); 00140 SDA(1); 00141 IO_DELAY(); 00142 00143 SCL(1); 00144 // wait till scl is high 00145 IO_DELAY(); 00146 00147 return data; 00148 } 00149 00150 00151 unsigned char 00152 my_i2cRead (unsigned char ack) 00153 { 00154 if (ack) 00155 { 00156 return my_i2cReadAck (); 00157 } 00158 return my_i2cReadNak (); 00159 } 00160 00161 00162 void 00163 my_i2cWriteByte (unsigned char byte) 00164 { 00165 for (int i = 0x80; i > 0x00; i >>= 1) 00166 { 00167 SCL(0); 00168 SDA((byte & i) ? true : false); 00169 IO_DELAY(); 00170 00171 SCL(1); 00172 IO_DELAY(); 00173 } 00174 } 00175 00176 00177 bool 00178 my_i2cGetAck () 00179 { 00180 SCL(0); 00181 SDA(1); 00182 IO_DELAY(); 00183 00184 SCL(1); 00185 // wait till clock = 1... 00186 IO_DELAY(); 00187 00188 bool ack = false; 00189 ack = sda; 00190 00191 return !ack; 00192 }
Generated on Fri Jul 15 2022 13:24:00 by
1.7.2