I2C-LC-Diaplsay mit PCF8574
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.cpp@1:68d6c4c092fb, 2022-02-23 (annotated)
- Committer:
- itbusch
- Date:
- Wed Feb 23 18:52:50 2022 +0000
- Revision:
- 1:68d6c4c092fb
- Parent:
- 0:43e25c35d0c1
140_robot_H_Bridge8835_DIR_PWM_US_LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
itbusch | 0:43e25c35d0c1 | 1 | /* |
itbusch | 0:43e25c35d0c1 | 2 | * mbed Library to use a software master i2c interface on any GPIO pins |
itbusch | 0:43e25c35d0c1 | 3 | * Copyright (c) 2012 Christopher Pepper |
itbusch | 0:43e25c35d0c1 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
itbusch | 0:43e25c35d0c1 | 5 | */ |
itbusch | 0:43e25c35d0c1 | 6 | |
itbusch | 0:43e25c35d0c1 | 7 | #include "SoftwareI2C.h" |
itbusch | 0:43e25c35d0c1 | 8 | |
itbusch | 0:43e25c35d0c1 | 9 | /** |
itbusch | 0:43e25c35d0c1 | 10 | * @brief Initializes interface |
itbusch | 0:43e25c35d0c1 | 11 | * @param sda GPIO pin to use as I2C SDA |
itbusch | 0:43e25c35d0c1 | 12 | * @param scl GPIO pin to use as I2C SCL |
itbusch | 0:43e25c35d0c1 | 13 | */ |
itbusch | 0:43e25c35d0c1 | 14 | |
itbusch | 0:43e25c35d0c1 | 15 | SoftwareI2C::SoftwareI2C(PinName sda, PinName scl) : _sda(sda) , _scl(scl) { |
itbusch | 0:43e25c35d0c1 | 16 | _scl.output(); |
itbusch | 0:43e25c35d0c1 | 17 | _scl.mode(OpenDrain); |
itbusch | 0:43e25c35d0c1 | 18 | _sda.output(); |
itbusch | 0:43e25c35d0c1 | 19 | _sda.mode(OpenDrain); |
itbusch | 0:43e25c35d0c1 | 20 | |
itbusch | 0:43e25c35d0c1 | 21 | _device_address = 0; |
itbusch | 0:43e25c35d0c1 | 22 | _frequency_delay = 20;//3; |
itbusch | 0:43e25c35d0c1 | 23 | |
itbusch | 0:43e25c35d0c1 | 24 | initialise(); |
itbusch | 0:43e25c35d0c1 | 25 | } |
itbusch | 0:43e25c35d0c1 | 26 | |
itbusch | 0:43e25c35d0c1 | 27 | SoftwareI2C::~SoftwareI2C() { |
itbusch | 0:43e25c35d0c1 | 28 | |
itbusch | 0:43e25c35d0c1 | 29 | } |
itbusch | 0:43e25c35d0c1 | 30 | |
itbusch | 0:43e25c35d0c1 | 31 | /** |
itbusch | 0:43e25c35d0c1 | 32 | * @brief Read 1 or more bytes from the I2C slave |
itbusch | 0:43e25c35d0c1 | 33 | * @param device_address The address of the device to read from |
itbusch | 0:43e25c35d0c1 | 34 | * @param data An allocated array to read the data into |
itbusch | 0:43e25c35d0c1 | 35 | * @param data_bytes Number of bytes to read (must be equal to or less then the allocated memory in data) |
itbusch | 0:43e25c35d0c1 | 36 | */ |
itbusch | 0:43e25c35d0c1 | 37 | void SoftwareI2C::read(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { |
itbusch | 0:43e25c35d0c1 | 38 | if (data == 0 || data_bytes == 0) return; |
itbusch | 0:43e25c35d0c1 | 39 | __disable_irq(); |
itbusch | 0:43e25c35d0c1 | 40 | device_address = device_address | 0x01; |
itbusch | 0:43e25c35d0c1 | 41 | start(); |
itbusch | 0:43e25c35d0c1 | 42 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 43 | getAck(); |
itbusch | 0:43e25c35d0c1 | 44 | for (int x = 0; x < data_bytes; ++x) { |
itbusch | 0:43e25c35d0c1 | 45 | data[x] = getByte(); |
itbusch | 0:43e25c35d0c1 | 46 | if ( x < (data_bytes -1)) { //ack all but the final byte |
itbusch | 0:43e25c35d0c1 | 47 | giveAck(); |
itbusch | 0:43e25c35d0c1 | 48 | } |
itbusch | 0:43e25c35d0c1 | 49 | } |
itbusch | 0:43e25c35d0c1 | 50 | stop(); |
itbusch | 0:43e25c35d0c1 | 51 | __enable_irq(); |
itbusch | 0:43e25c35d0c1 | 52 | } |
itbusch | 0:43e25c35d0c1 | 53 | |
itbusch | 0:43e25c35d0c1 | 54 | /** |
itbusch | 0:43e25c35d0c1 | 55 | * @brief Write 1 or more bytes to the I2C slave |
itbusch | 0:43e25c35d0c1 | 56 | * @param device_address The address of the device to write to |
itbusch | 0:43e25c35d0c1 | 57 | * @param data An array to write the data from |
itbusch | 0:43e25c35d0c1 | 58 | * @param data_bytes Number of bytes to write from array |
itbusch | 0:43e25c35d0c1 | 59 | */ |
itbusch | 0:43e25c35d0c1 | 60 | void SoftwareI2C::write(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { |
itbusch | 0:43e25c35d0c1 | 61 | if (data == 0 || data_bytes == 0) return; |
itbusch | 0:43e25c35d0c1 | 62 | __disable_irq(); |
itbusch | 0:43e25c35d0c1 | 63 | device_address = device_address & 0xFE; |
itbusch | 0:43e25c35d0c1 | 64 | start(); |
itbusch | 0:43e25c35d0c1 | 65 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 66 | getAck(); |
itbusch | 0:43e25c35d0c1 | 67 | for ( int x = 0; x < data_bytes; ++x ) { |
itbusch | 0:43e25c35d0c1 | 68 | putByte(data[x]); |
itbusch | 0:43e25c35d0c1 | 69 | getAck(); |
itbusch | 0:43e25c35d0c1 | 70 | } |
itbusch | 0:43e25c35d0c1 | 71 | stop(); |
itbusch | 0:43e25c35d0c1 | 72 | __enable_irq(); |
itbusch | 0:43e25c35d0c1 | 73 | } |
itbusch | 0:43e25c35d0c1 | 74 | |
itbusch | 0:43e25c35d0c1 | 75 | /** |
itbusch | 0:43e25c35d0c1 | 76 | * @brief Write 1 byte to the I2C slave |
itbusch | 0:43e25c35d0c1 | 77 | * @param device_address The address of the device to write to |
itbusch | 0:43e25c35d0c1 | 78 | * @param byte The data to write |
itbusch | 0:43e25c35d0c1 | 79 | */ |
itbusch | 0:43e25c35d0c1 | 80 | void SoftwareI2C::write(uint8_t device_address, uint8_t byte) { |
itbusch | 0:43e25c35d0c1 | 81 | device_address = device_address & 0xFE; |
itbusch | 0:43e25c35d0c1 | 82 | start(); |
itbusch | 0:43e25c35d0c1 | 83 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 84 | getAck(); |
itbusch | 0:43e25c35d0c1 | 85 | putByte(byte); |
itbusch | 0:43e25c35d0c1 | 86 | getAck(); |
itbusch | 0:43e25c35d0c1 | 87 | stop(); |
itbusch | 0:43e25c35d0c1 | 88 | } |
itbusch | 0:43e25c35d0c1 | 89 | |
itbusch | 0:43e25c35d0c1 | 90 | /** |
itbusch | 0:43e25c35d0c1 | 91 | * @brief Read 1 or more bytes from the I2C slave at the specified memory address |
itbusch | 0:43e25c35d0c1 | 92 | * @param device_address The address of the device to read from |
itbusch | 0:43e25c35d0c1 | 93 | * @param start_address The memory address to read from |
itbusch | 0:43e25c35d0c1 | 94 | * @param data The allocated array to read into |
itbusch | 0:43e25c35d0c1 | 95 | * @param data_bytes The number of bytes to read |
itbusch | 0:43e25c35d0c1 | 96 | */ |
itbusch | 0:43e25c35d0c1 | 97 | void SoftwareI2C::randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { |
itbusch | 0:43e25c35d0c1 | 98 | if (data == 0 || data_bytes == 0) return; |
itbusch | 0:43e25c35d0c1 | 99 | |
itbusch | 0:43e25c35d0c1 | 100 | device_address = device_address & 0xFE; |
itbusch | 0:43e25c35d0c1 | 101 | start(); |
itbusch | 0:43e25c35d0c1 | 102 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 103 | if (!getAck()) { |
itbusch | 0:43e25c35d0c1 | 104 | return; |
itbusch | 0:43e25c35d0c1 | 105 | } |
itbusch | 0:43e25c35d0c1 | 106 | putByte(start_address); |
itbusch | 0:43e25c35d0c1 | 107 | if (!getAck()) { |
itbusch | 0:43e25c35d0c1 | 108 | return; |
itbusch | 0:43e25c35d0c1 | 109 | } |
itbusch | 0:43e25c35d0c1 | 110 | |
itbusch | 0:43e25c35d0c1 | 111 | device_address=device_address | 0x01; |
itbusch | 0:43e25c35d0c1 | 112 | start(); |
itbusch | 0:43e25c35d0c1 | 113 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 114 | if (!getAck()) { |
itbusch | 0:43e25c35d0c1 | 115 | return; |
itbusch | 0:43e25c35d0c1 | 116 | } |
itbusch | 0:43e25c35d0c1 | 117 | for ( int x = 0; x < data_bytes; ++x) { |
itbusch | 0:43e25c35d0c1 | 118 | data[x] = getByte(); |
itbusch | 0:43e25c35d0c1 | 119 | if (x != (data_bytes - 1)) giveAck(); |
itbusch | 0:43e25c35d0c1 | 120 | } |
itbusch | 0:43e25c35d0c1 | 121 | stop(); |
itbusch | 0:43e25c35d0c1 | 122 | } |
itbusch | 0:43e25c35d0c1 | 123 | |
itbusch | 0:43e25c35d0c1 | 124 | /** |
itbusch | 0:43e25c35d0c1 | 125 | * @brief Write 1 byte to the I2C slave at the specified memory address |
itbusch | 0:43e25c35d0c1 | 126 | * @param device_address The address of the device to write to |
itbusch | 0:43e25c35d0c1 | 127 | * @param start_address The memory address to write to |
itbusch | 0:43e25c35d0c1 | 128 | * @param byte The data to write |
itbusch | 0:43e25c35d0c1 | 129 | */ |
itbusch | 0:43e25c35d0c1 | 130 | void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte) { |
itbusch | 0:43e25c35d0c1 | 131 | device_address = device_address & 0xFE; |
itbusch | 0:43e25c35d0c1 | 132 | start(); |
itbusch | 0:43e25c35d0c1 | 133 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 134 | getAck(); |
itbusch | 0:43e25c35d0c1 | 135 | putByte(start_address); |
itbusch | 0:43e25c35d0c1 | 136 | getAck(); |
itbusch | 0:43e25c35d0c1 | 137 | putByte(byte); |
itbusch | 0:43e25c35d0c1 | 138 | getAck(); |
itbusch | 0:43e25c35d0c1 | 139 | stop(); |
itbusch | 0:43e25c35d0c1 | 140 | } |
itbusch | 0:43e25c35d0c1 | 141 | |
itbusch | 0:43e25c35d0c1 | 142 | /** |
itbusch | 0:43e25c35d0c1 | 143 | * @brief Write 1 or more bytes to the I2C slave at the specified memory address |
itbusch | 0:43e25c35d0c1 | 144 | * @param device_address The address of the device to write to |
itbusch | 0:43e25c35d0c1 | 145 | * @param start_address The memory address to write to |
itbusch | 0:43e25c35d0c1 | 146 | * @param data The data to write |
itbusch | 0:43e25c35d0c1 | 147 | * @param data_bytes The number of bytes to write |
itbusch | 0:43e25c35d0c1 | 148 | */ |
itbusch | 0:43e25c35d0c1 | 149 | void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { |
itbusch | 0:43e25c35d0c1 | 150 | if (data == 0 || data_bytes == 0) return; |
itbusch | 0:43e25c35d0c1 | 151 | |
itbusch | 0:43e25c35d0c1 | 152 | device_address = device_address & 0xFE; |
itbusch | 0:43e25c35d0c1 | 153 | start(); |
itbusch | 0:43e25c35d0c1 | 154 | putByte(device_address); |
itbusch | 0:43e25c35d0c1 | 155 | getAck(); |
itbusch | 0:43e25c35d0c1 | 156 | putByte(start_address); |
itbusch | 0:43e25c35d0c1 | 157 | getAck(); |
itbusch | 0:43e25c35d0c1 | 158 | for ( int x = 0; x <= data_bytes; ++x ) { |
itbusch | 0:43e25c35d0c1 | 159 | putByte(data[x]); |
itbusch | 0:43e25c35d0c1 | 160 | getAck(); |
itbusch | 0:43e25c35d0c1 | 161 | } |
itbusch | 0:43e25c35d0c1 | 162 | stop(); |
itbusch | 0:43e25c35d0c1 | 163 | } |
itbusch | 0:43e25c35d0c1 | 164 | |
itbusch | 0:43e25c35d0c1 | 165 | /** |
itbusch | 0:43e25c35d0c1 | 166 | * @brief Read 2 bytes from the I2C slave at the specified memory address and return them as an 16bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 167 | * @param device_address The address of the device to read from |
itbusch | 0:43e25c35d0c1 | 168 | * @param start_address The memory address to read from |
itbusch | 0:43e25c35d0c1 | 169 | * @return MSB 16bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 170 | */ |
itbusch | 0:43e25c35d0c1 | 171 | uint16_t SoftwareI2C::read16(uint8_t device_address, uint8_t start_address) { |
itbusch | 0:43e25c35d0c1 | 172 | uint8_t short_array[2] = {0, 0}; |
itbusch | 0:43e25c35d0c1 | 173 | randomRead(device_address, start_address, short_array, 2 ); |
itbusch | 0:43e25c35d0c1 | 174 | uint16_t value = 0; |
itbusch | 0:43e25c35d0c1 | 175 | value = short_array[0] << 8; |
itbusch | 0:43e25c35d0c1 | 176 | value |= short_array[1]; |
itbusch | 0:43e25c35d0c1 | 177 | |
itbusch | 0:43e25c35d0c1 | 178 | return value; |
itbusch | 0:43e25c35d0c1 | 179 | } |
itbusch | 0:43e25c35d0c1 | 180 | |
itbusch | 0:43e25c35d0c1 | 181 | /** |
itbusch | 0:43e25c35d0c1 | 182 | * @brief Read 3 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 183 | * @param device_address The address of the device to read from |
itbusch | 0:43e25c35d0c1 | 184 | * @param start_address The memory address to read from |
itbusch | 0:43e25c35d0c1 | 185 | * @return MSB 32bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 186 | */ |
itbusch | 0:43e25c35d0c1 | 187 | uint32_t SoftwareI2C::read24(uint8_t device_address, uint8_t start_address) { |
itbusch | 0:43e25c35d0c1 | 188 | uint8_t value_array[4] = {0, 0, 0}; |
itbusch | 0:43e25c35d0c1 | 189 | randomRead(device_address, start_address, value_array, 3 ); |
itbusch | 0:43e25c35d0c1 | 190 | uint32_t value = 0; |
itbusch | 0:43e25c35d0c1 | 191 | value = value_array[0] << 16; |
itbusch | 0:43e25c35d0c1 | 192 | value |= value_array[1] << 8; |
itbusch | 0:43e25c35d0c1 | 193 | value |= value_array[2]; |
itbusch | 0:43e25c35d0c1 | 194 | |
itbusch | 0:43e25c35d0c1 | 195 | return value; |
itbusch | 0:43e25c35d0c1 | 196 | } |
itbusch | 0:43e25c35d0c1 | 197 | |
itbusch | 0:43e25c35d0c1 | 198 | /** |
itbusch | 0:43e25c35d0c1 | 199 | * @brief Read 4 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 200 | * @param device_address The address of the device to read from |
itbusch | 0:43e25c35d0c1 | 201 | * @param start_address The memory address to read from |
itbusch | 0:43e25c35d0c1 | 202 | * @return MSB 32bit unsigned integer |
itbusch | 0:43e25c35d0c1 | 203 | */ |
itbusch | 0:43e25c35d0c1 | 204 | uint32_t SoftwareI2C::read32(uint8_t device_address, uint8_t start_address) { |
itbusch | 0:43e25c35d0c1 | 205 | uint8_t value_array[4] = {0, 0, 0, 0}; |
itbusch | 0:43e25c35d0c1 | 206 | randomRead(device_address, start_address, value_array, 4 ); |
itbusch | 0:43e25c35d0c1 | 207 | uint32_t value = 0; |
itbusch | 0:43e25c35d0c1 | 208 | value = value_array[0] << 24; |
itbusch | 0:43e25c35d0c1 | 209 | value |= value_array[1] << 16; |
itbusch | 0:43e25c35d0c1 | 210 | value |= value_array[2] << 8; |
itbusch | 0:43e25c35d0c1 | 211 | value |= value_array[3]; |
itbusch | 0:43e25c35d0c1 | 212 | |
itbusch | 0:43e25c35d0c1 | 213 | return value; |
itbusch | 0:43e25c35d0c1 | 214 | } |