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

Committer:
itbusch
Date:
Sun Nov 29 10:10:28 2020 +0000
Revision:
0:43e25c35d0c1
I2C Library PCF8574 JDS

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef _SOFTWARE_I2C_H_
itbusch 0:43e25c35d0c1 8 #define _SOFTWARE_I2C_H_
itbusch 0:43e25c35d0c1 9
itbusch 0:43e25c35d0c1 10 #include "mbed.h"
itbusch 0:43e25c35d0c1 11
itbusch 0:43e25c35d0c1 12 /**
itbusch 0:43e25c35d0c1 13 * @brief SoftwareI2C class
itbusch 0:43e25c35d0c1 14 */
itbusch 0:43e25c35d0c1 15
itbusch 0:43e25c35d0c1 16 class SoftwareI2C {
itbusch 0:43e25c35d0c1 17 public:
itbusch 0:43e25c35d0c1 18 SoftwareI2C(PinName sda, PinName scl);
itbusch 0:43e25c35d0c1 19 ~SoftwareI2C();
itbusch 0:43e25c35d0c1 20
itbusch 0:43e25c35d0c1 21 void read(uint8_t device_address, uint8_t* data, uint8_t data_bytes);
itbusch 0:43e25c35d0c1 22 void write(uint8_t device_address, uint8_t* data, uint8_t data_bytes);
itbusch 0:43e25c35d0c1 23 void write(uint8_t device_address, uint8_t byte);
itbusch 0:43e25c35d0c1 24 void randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes);
itbusch 0:43e25c35d0c1 25 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes);
itbusch 0:43e25c35d0c1 26 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte);
itbusch 0:43e25c35d0c1 27
itbusch 0:43e25c35d0c1 28 uint8_t read8(uint8_t device_address, uint8_t start_address);
itbusch 0:43e25c35d0c1 29 uint16_t read16(uint8_t device_address, uint8_t start_address);
itbusch 0:43e25c35d0c1 30 uint32_t read24(uint8_t device_address, uint8_t start_address);
itbusch 0:43e25c35d0c1 31 uint32_t read32(uint8_t device_address, uint8_t start_address);
itbusch 0:43e25c35d0c1 32
itbusch 0:43e25c35d0c1 33 void setDeviceAddress(uint8_t address){
itbusch 0:43e25c35d0c1 34 _device_address = address;
itbusch 0:43e25c35d0c1 35 }
itbusch 0:43e25c35d0c1 36
itbusch 0:43e25c35d0c1 37 void setFrequency(uint32_t frequency){
itbusch 0:43e25c35d0c1 38 _frequency_delay = 1000000 / frequency;
itbusch 0:43e25c35d0c1 39 }
itbusch 0:43e25c35d0c1 40
itbusch 0:43e25c35d0c1 41 inline void initialise() {
itbusch 0:43e25c35d0c1 42 _scl.output();
itbusch 0:43e25c35d0c1 43 _sda.output();
itbusch 0:43e25c35d0c1 44
itbusch 0:43e25c35d0c1 45 _sda = 1;
itbusch 0:43e25c35d0c1 46 _scl = 0;
itbusch 0:43e25c35d0c1 47 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 48
itbusch 0:43e25c35d0c1 49 for ( int n = 0; n <= 3; ++n ) {
itbusch 0:43e25c35d0c1 50 stop();
itbusch 0:43e25c35d0c1 51 }
itbusch 0:43e25c35d0c1 52 }
itbusch 0:43e25c35d0c1 53
itbusch 0:43e25c35d0c1 54 private:
itbusch 0:43e25c35d0c1 55 void warte(int zeit)
itbusch 0:43e25c35d0c1 56 {
itbusch 0:43e25c35d0c1 57 for (int i=0;i<zeit*5;i++)
itbusch 0:43e25c35d0c1 58 {
itbusch 0:43e25c35d0c1 59 asm("nop\n\t");
itbusch 0:43e25c35d0c1 60 }
itbusch 0:43e25c35d0c1 61 }
itbusch 0:43e25c35d0c1 62 inline void start() {
itbusch 0:43e25c35d0c1 63 _sda.output();
itbusch 0:43e25c35d0c1 64 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 65 _scl = 1;
itbusch 0:43e25c35d0c1 66 _sda = 1;
itbusch 0:43e25c35d0c1 67 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 68 _sda = 0;
itbusch 0:43e25c35d0c1 69 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 70 _scl = 0;
itbusch 0:43e25c35d0c1 71 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 72 }
itbusch 0:43e25c35d0c1 73
itbusch 0:43e25c35d0c1 74 inline void stop() {
itbusch 0:43e25c35d0c1 75 _sda.output();
itbusch 0:43e25c35d0c1 76 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 77 _sda = 0;
itbusch 0:43e25c35d0c1 78 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 79 _scl = 1;
itbusch 0:43e25c35d0c1 80 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 81 _sda = 1;
itbusch 0:43e25c35d0c1 82 }
itbusch 0:43e25c35d0c1 83
itbusch 0:43e25c35d0c1 84 inline void putByte(uint8_t byte) {
itbusch 0:43e25c35d0c1 85 _sda.output();
itbusch 0:43e25c35d0c1 86 for ( int n = 8; n > 0; --n) {
itbusch 0:43e25c35d0c1 87 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 88 _sda = byte & (1 << (n-1));
itbusch 0:43e25c35d0c1 89 _scl = 1;
itbusch 0:43e25c35d0c1 90 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 91 _scl = 0;
itbusch 0:43e25c35d0c1 92 }
itbusch 0:43e25c35d0c1 93 _sda = 1;
itbusch 0:43e25c35d0c1 94 }
itbusch 0:43e25c35d0c1 95
itbusch 0:43e25c35d0c1 96 inline uint8_t getByte() {
itbusch 0:43e25c35d0c1 97 uint8_t byte = 0;
itbusch 0:43e25c35d0c1 98
itbusch 0:43e25c35d0c1 99 _sda.input(); //release the data line
itbusch 0:43e25c35d0c1 100 _sda.mode(OpenDrain);
itbusch 0:43e25c35d0c1 101
itbusch 0:43e25c35d0c1 102 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 103
itbusch 0:43e25c35d0c1 104 for ( int n = 8; n > 0; --n ) {
itbusch 0:43e25c35d0c1 105 _scl=1; //set clock high
itbusch 0:43e25c35d0c1 106 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 107 byte |= _sda << (n-1); //read the bit
itbusch 0:43e25c35d0c1 108 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 109 _scl=0; //set clock low
itbusch 0:43e25c35d0c1 110 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 111 }
itbusch 0:43e25c35d0c1 112
itbusch 0:43e25c35d0c1 113 _sda.output(); //take data line back
itbusch 0:43e25c35d0c1 114
itbusch 0:43e25c35d0c1 115 return byte;
itbusch 0:43e25c35d0c1 116 }
itbusch 0:43e25c35d0c1 117
itbusch 0:43e25c35d0c1 118 inline void giveAck() {
itbusch 0:43e25c35d0c1 119 _sda.output();
itbusch 0:43e25c35d0c1 120 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 121 _sda = 0;
itbusch 0:43e25c35d0c1 122 _scl = 1;
itbusch 0:43e25c35d0c1 123 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 124 _scl = 0;
itbusch 0:43e25c35d0c1 125 _sda = 1;
itbusch 0:43e25c35d0c1 126
itbusch 0:43e25c35d0c1 127 }
itbusch 0:43e25c35d0c1 128
itbusch 0:43e25c35d0c1 129 inline bool getAck() {
itbusch 0:43e25c35d0c1 130 _sda.output();
itbusch 0:43e25c35d0c1 131 _sda = 1;
itbusch 0:43e25c35d0c1 132 _scl = 1;
itbusch 0:43e25c35d0c1 133 _sda.input();
itbusch 0:43e25c35d0c1 134 _sda.mode(OpenDrain);
itbusch 0:43e25c35d0c1 135 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 136 _scl = 0;
itbusch 0:43e25c35d0c1 137
itbusch 0:43e25c35d0c1 138 if(_sda != 0){return false;}
itbusch 0:43e25c35d0c1 139
itbusch 0:43e25c35d0c1 140 warte(_frequency_delay);
itbusch 0:43e25c35d0c1 141 return true;
itbusch 0:43e25c35d0c1 142 }
itbusch 0:43e25c35d0c1 143
itbusch 0:43e25c35d0c1 144 DigitalInOut _sda;
itbusch 0:43e25c35d0c1 145 DigitalInOut _scl;
itbusch 0:43e25c35d0c1 146
itbusch 0:43e25c35d0c1 147 uint8_t _device_address;
itbusch 0:43e25c35d0c1 148 uint32_t _frequency_delay;
itbusch 0:43e25c35d0c1 149 };
itbusch 0:43e25c35d0c1 150
itbusch 0:43e25c35d0c1 151 #endif