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.
Dependents: 120_robot_H_Bridge8835_UserButton_DIR_PWM_I2C_LCD 140_robot_H_Bridge8835_DIR_PWM_US_LCD xxx_Uhr_I2C_LCD_Delay_Bua xxx_Uhr_I2C_LCD_Delay_BuaV ... more
SoftwareI2C.h
00001 /* 00002 * mbed Library to use a software master i2c interface on any GPIO pins 00003 * Copyright (c) 2012 Christopher Pepper 00004 * Released under the MIT License: http://mbed.org/license/mit 00005 */ 00006 00007 #ifndef _SOFTWARE_I2C_H_ 00008 #define _SOFTWARE_I2C_H_ 00009 00010 #include "mbed.h" 00011 00012 /** 00013 * @brief SoftwareI2C class 00014 */ 00015 00016 class SoftwareI2C { 00017 public: 00018 SoftwareI2C(PinName sda, PinName scl); 00019 ~SoftwareI2C(); 00020 00021 void read(uint8_t device_address, uint8_t* data, uint8_t data_bytes); 00022 void write(uint8_t device_address, uint8_t* data, uint8_t data_bytes); 00023 void write(uint8_t device_address, uint8_t byte); 00024 void randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes); 00025 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes); 00026 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte); 00027 00028 uint8_t read8(uint8_t device_address, uint8_t start_address); 00029 uint16_t read16(uint8_t device_address, uint8_t start_address); 00030 uint32_t read24(uint8_t device_address, uint8_t start_address); 00031 uint32_t read32(uint8_t device_address, uint8_t start_address); 00032 00033 void setDeviceAddress(uint8_t address){ 00034 _device_address = address; 00035 } 00036 00037 void setFrequency(uint32_t frequency){ 00038 _frequency_delay = 1000000 / frequency; 00039 } 00040 00041 inline void initialise() { 00042 _scl.output(); 00043 _sda.output(); 00044 00045 _sda = 1; 00046 _scl = 0; 00047 warte(_frequency_delay); 00048 00049 for ( int n = 0; n <= 3; ++n ) { 00050 stop(); 00051 } 00052 } 00053 00054 private: 00055 void warte(int zeit) 00056 { 00057 for (int i=0;i<zeit*5;i++) 00058 { 00059 asm("nop\n\t"); 00060 } 00061 } 00062 inline void start() { 00063 _sda.output(); 00064 warte(_frequency_delay); 00065 _scl = 1; 00066 _sda = 1; 00067 warte(_frequency_delay); 00068 _sda = 0; 00069 warte(_frequency_delay); 00070 _scl = 0; 00071 warte(_frequency_delay); 00072 } 00073 00074 inline void stop() { 00075 _sda.output(); 00076 warte(_frequency_delay); 00077 _sda = 0; 00078 warte(_frequency_delay); 00079 _scl = 1; 00080 warte(_frequency_delay); 00081 _sda = 1; 00082 } 00083 00084 inline void putByte(uint8_t byte) { 00085 _sda.output(); 00086 for ( int n = 8; n > 0; --n) { 00087 warte(_frequency_delay); 00088 _sda = byte & (1 << (n-1)); 00089 _scl = 1; 00090 warte(_frequency_delay); 00091 _scl = 0; 00092 } 00093 _sda = 1; 00094 } 00095 00096 inline uint8_t getByte() { 00097 uint8_t byte = 0; 00098 00099 _sda.input(); //release the data line 00100 _sda.mode(OpenDrain); 00101 00102 warte(_frequency_delay); 00103 00104 for ( int n = 8; n > 0; --n ) { 00105 _scl=1; //set clock high 00106 warte(_frequency_delay); 00107 byte |= _sda << (n-1); //read the bit 00108 warte(_frequency_delay); 00109 _scl=0; //set clock low 00110 warte(_frequency_delay); 00111 } 00112 00113 _sda.output(); //take data line back 00114 00115 return byte; 00116 } 00117 00118 inline void giveAck() { 00119 _sda.output(); 00120 warte(_frequency_delay); 00121 _sda = 0; 00122 _scl = 1; 00123 warte(_frequency_delay); 00124 _scl = 0; 00125 _sda = 1; 00126 00127 } 00128 00129 inline bool getAck() { 00130 _sda.output(); 00131 _sda = 1; 00132 _scl = 1; 00133 _sda.input(); 00134 _sda.mode(OpenDrain); 00135 warte(_frequency_delay); 00136 _scl = 0; 00137 00138 if(_sda != 0){return false;} 00139 00140 warte(_frequency_delay); 00141 return true; 00142 } 00143 00144 DigitalInOut _sda; 00145 DigitalInOut _scl; 00146 00147 uint8_t _device_address; 00148 uint32_t _frequency_delay; 00149 }; 00150 00151 #endif
Generated on Tue Jul 26 2022 02:31:18 by
