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.
Fork of Smoothie by
I2CBang.h
00001 /* 00002 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). 00003 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 00004 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00005 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 00006 */ 00007 #ifndef I2CBANG_H 00008 #define I2CBANG_H 00009 #include "mbed.h" // mbed.h lib 00010 00011 void i2cbang_init(I2C i2c){ 00012 const int addr = 0x40; 00013 char cmd[2]; 00014 cmd[0] = 0x1B; 00015 cmd[1] = 0x3F; 00016 i2c.write(addr, cmd, 2); 00017 cmd[0] = 0x0B; 00018 cmd[1] = 0xC0; 00019 i2c.write(addr, cmd, 2); 00020 wait_ms(1); 00021 } 00022 00023 void i2cbang_start(I2C i2c){ 00024 const int addr = 0x40; 00025 char cmd[2]; 00026 cmd[0] = 0x0B; 00027 cmd[1] = 0xBF; 00028 i2c.write(addr, cmd, 2); 00029 wait_ms(1); 00030 cmd[1] = 0x3F; 00031 i2c.write(addr, cmd, 2); 00032 wait_ms(1); 00033 } 00034 00035 void i2cbang_stop(I2C i2c){ 00036 const int addr = 0x40; 00037 char cmd[2]; 00038 cmd[0] = 0x0B; 00039 cmd[1] = 0xBF; 00040 i2c.write(addr, cmd, 2); 00041 wait_ms(1); 00042 cmd[1] = 0xFF; 00043 i2c.write(addr, cmd, 2); 00044 wait_ms(1); 00045 } 00046 00047 void i2cbang_writebit(I2C i2c, bool bit){ 00048 const int addr = 0x40; 00049 char cmd[2]; 00050 cmd[0] = 0x0B; 00051 if(bit){ 00052 cmd[1] = 0x7F; 00053 }else{ 00054 cmd[1] = 0x3F; 00055 } 00056 i2c.write(addr, cmd, 2); 00057 wait_ms(1); 00058 00059 if(bit){ 00060 cmd[1] = 0xFF; 00061 }else{ 00062 cmd[1] = 0xBF; 00063 } 00064 i2c.write(addr, cmd, 2); 00065 wait_ms(1); 00066 00067 if(bit){ 00068 cmd[1] = 0x7F; 00069 }else{ 00070 cmd[1] = 0x3F; 00071 } 00072 i2c.write(addr, cmd, 2); 00073 wait_ms(1); 00074 00075 if(bit){ 00076 cmd[1] = 0x3F; 00077 i2c.write(addr, cmd, 2); 00078 } 00079 wait_ms(1); 00080 } 00081 00082 char i2cbang_readbit(I2C i2c){ 00083 const int addr = 0x40; 00084 char cmd[2]; 00085 char res; 00086 cmd[0] = 0x0B; 00087 cmd[1] = 0x7F; 00088 i2c.write(addr, cmd, 2); 00089 wait_ms(1); 00090 00091 cmd[1] = 0xFF; 00092 i2c.write(addr, cmd, 2); 00093 wait_ms(1); 00094 00095 cmd[0] = 0x03; 00096 i2c.write(addr, cmd, 1, false); 00097 i2c.read(addr, cmd, 1); 00098 res = cmd[0]; 00099 wait_ms(1); 00100 00101 cmd[0] = 0x0B; 00102 cmd[1] = 0x7F; 00103 i2c.write(addr, cmd, 2); 00104 wait_ms(1); 00105 00106 // cmd[1] = 0x3F; 00107 // i2c.write(addr, cmd, 2); 00108 // wait_ms(1); 00109 00110 //res = (~res) & 0x40; 00111 return res; 00112 } 00113 00114 int i2cbang_write(I2C i2c, char c){ 00115 for (int i=0;i<8;i++){ 00116 i2cbang_writebit(i2c, (c&0x80) > 0); 00117 c<<=1; 00118 } 00119 00120 return i2cbang_readbit(i2c); 00121 /* 00122 const int addr = 0x40; 00123 char cmd[2]; 00124 char d = 0x00; 00125 //data 00126 for (int i=7;i>=0;i--){ 00127 i2c.write(0x3F | d); 00128 d = ((c>>i)&1)<<6; 00129 i2c.write(0x3F | d); 00130 i2c.write(0xBF | d); 00131 } 00132 //ack 00133 i2c.write(0x3F | d); 00134 i2c.write(0xBF); 00135 i2c.stop(); 00136 cmd[0] = 0x1B; 00137 cmd[1] = 0x7F; 00138 i2c.write(addr, cmd, 2); 00139 cmd[0] = 0x03; 00140 i2c.write(addr, cmd, 1, false); 00141 i2c.start(); 00142 i2c.write(addr | 0x01); 00143 cmd[1] = i2c.read(false); 00144 // int res = (~cmd[1]) & 0x40; 00145 int res = cmd[1]; 00146 i2c.stop(); 00147 cmd[0] = 0x1B; 00148 cmd[1] = 0x3F; 00149 i2c.write(addr, cmd, 2); 00150 cmd[0] = 0x0B; 00151 cmd[1] = 0xBF; 00152 i2c.write(addr, cmd, 2, false); 00153 return res; 00154 */ 00155 } 00156 00157 char i2cbang_read(I2C i2c, bool ack){ 00158 char res = 0; 00159 for(int i=0;i<8;i++){ 00160 res<<=1; 00161 res |= i2cbang_readbit(i2c); 00162 } 00163 00164 if(ack){ 00165 i2cbang_writebit(i2c, 0); 00166 }else{ 00167 i2cbang_writebit(i2c, 1); 00168 } 00169 00170 wait_ms(1); 00171 00172 return res; 00173 } 00174 00175 #endif // I2CBANG_H 00176
Generated on Tue Jul 12 2022 20:09:02 by
1.7.2
