Data send-receive by IIC-bus Hardware: IIC-board with 4 x PCF8574 IC you see a 8bit leds this leds show the value from an other 8574 who has 8 switches as input The 8xswitches from IIC you can see also at the pins PC_0 to PC_7

Dependencies:   mbed

Fork of Nucleo_IIC_2x8574in_out by Gerhard Neumaier

Gerhard Neumaier will show you : /media/uploads/schlaumaier54/funkturm.jpg

You can see my hardware: Nucleo F103RB with a IIC-bus application

In the background a printed board "IIC-Trainer" with different IIC ICs like PCF8574a, PCF8591, EEPROM; Real-time-clock In the foreground you see right a own-made printed circuit-board with 4x IC PCF8574

/media/uploads/schlaumaier54/nucleo2xmitiicund_servo6.jpg

Eine Seite auf os_mbed ARM von Gerhard Neumaier Offenburg Feb 2018

Committer:
schlaumaier54
Date:
Fri Dec 15 11:25:59 2017 +0000
Revision:
1:cb8a26c1d6e9
Parent:
0:b183e63b31f8
Version 15.Dez.17

Who changed what in which revision?

UserRevisionLine numberNew contents of line
schlaumaier54 0:b183e63b31f8 1 /*Data send-receive by IIC-bus Hardware: IIC-board with 4 x PCF8574 IC
schlaumaier54 0:b183e63b31f8 2 you see a 8bit leds this leds show the value from an other 8574 who has 8 switches as input
schlaumaier54 0:b183e63b31f8 3 The 8xswitches from IIC you can see also at the pins PC_0 to PC_7
schlaumaier54 0:b183e63b31f8 4 Tasks: 1. Receive a analog value and show this value as a 8xled-strip at the third PCF8574 IC
schlaumaier54 0:b183e63b31f8 5 2. Send the 8xswitches from IIC-bus to the virtuell COM-port. You can visualize the switches
schlaumaier54 0:b183e63b31f8 6 with the Software "Profilab" or "LABview"
schlaumaier54 0:b183e63b31f8 7 Aug. 2015 G. Neumaier Gewerblich-Technische Schule Offenburg Germany
schlaumaier54 0:b183e63b31f8 8 */
schlaumaier54 0:b183e63b31f8 9
schlaumaier54 0:b183e63b31f8 10 #include "mbed.h"
schlaumaier54 0:b183e63b31f8 11 #define PCF8574_ADDRoutput 0x46 // PCF8574 address 0x46 lowest bit (R/W)=0 IICboard P3
schlaumaier54 1:cb8a26c1d6e9 12 #define PCF8574_ADDRinput 0x45 // PCF8574 address 0x45 lowest bit (R/W)=1 IICboard P2
schlaumaier54 0:b183e63b31f8 13 //ADR-Jumper on IIC4xPCF8574board jumped to 0: Basis ADR:P0=0x40;P1=0x42;P2=0x44;P3=0x46;
schlaumaier54 0:b183e63b31f8 14 //ADR-Jumper on IIC4xPCF8574board jumped to 1: Basis ADR:P0=0x48;P1=0x4A;P2=0x4C;P3=0x4E;
schlaumaier54 0:b183e63b31f8 15 #define LED_MASK 0xffff //Portc has 16bit, therefore 4digit mask
schlaumaier54 0:b183e63b31f8 16 // 1=output 0=input
schlaumaier54 0:b183e63b31f8 17
schlaumaier54 0:b183e63b31f8 18 I2C i2c(I2C_SDA, I2C_SCL); //IIC Pins SDA and SCL for ARDUINO pinheader Nucleo-board
schlaumaier54 0:b183e63b31f8 19 //SCL and SPA pullup resistors 2k2Ohm to+5Volt
schlaumaier54 0:b183e63b31f8 20
schlaumaier54 0:b183e63b31f8 21 PortOut leds(PortC, LED_MASK); //Important: Port_B not possible -> PB_8 PB_9 Pins for IIC-bus
schlaumaier54 0:b183e63b31f8 22
schlaumaier54 0:b183e63b31f8 23 DigitalOut myled(LED1); //LED on Nucleo-board
schlaumaier54 0:b183e63b31f8 24 //for the task1****: AnalogIn analog_value0(A0); //same as Pin PA_0
schlaumaier54 0:b183e63b31f8 25 //for the task2****: Serial pc(SERIAL_TX, SERIAL_RX);
schlaumaier54 0:b183e63b31f8 26
schlaumaier54 0:b183e63b31f8 27 int main()
schlaumaier54 0:b183e63b31f8 28 {
schlaumaier54 0:b183e63b31f8 29 char input8bit;
schlaumaier54 1:cb8a26c1d6e9 30 char data_write[2]; //must be char!! a array with 2storage places
schlaumaier54 1:cb8a26c1d6e9 31 char data_read[2]; //read buffer a array with 2storage places
schlaumaier54 0:b183e63b31f8 32
schlaumaier54 0:b183e63b31f8 33 data_write[0] = 0xf2; //LEDs low aktiv dummy
schlaumaier54 0:b183e63b31f8 34 int status = i2c.write(PCF8574_ADDRoutput , data_write, 1, 0);
schlaumaier54 1:cb8a26c1d6e9 35 if (status != 0) // Error -> no acknowledge detected
schlaumaier54 0:b183e63b31f8 36 {
schlaumaier54 1:cb8a26c1d6e9 37 while (1) //-> endless loop when error IIC detected
schlaumaier54 0:b183e63b31f8 38 {
schlaumaier54 0:b183e63b31f8 39 myled = !myled;
schlaumaier54 0:b183e63b31f8 40 wait(0.7);
schlaumaier54 0:b183e63b31f8 41 }
schlaumaier54 0:b183e63b31f8 42 }
schlaumaier54 0:b183e63b31f8 43
schlaumaier54 0:b183e63b31f8 44 while (1) //endless loop
schlaumaier54 0:b183e63b31f8 45 {
schlaumaier54 0:b183e63b31f8 46
schlaumaier54 0:b183e63b31f8 47 // i2c.read will start the IIC-bus with the last 0 also stop the same by 12c.read
schlaumaier54 0:b183e63b31f8 48 i2c.read(PCF8574_ADDRinput, data_read, 1, 0); // read to array 1byte stop
schlaumaier54 0:b183e63b31f8 49 input8bit = data_read[0]; //array value to variable
schlaumaier54 0:b183e63b31f8 50 leds = input8bit; //Output PortC low-byte
schlaumaier54 0:b183e63b31f8 51 data_write[0] = input8bit; //8bit analog-value to array
schlaumaier54 0:b183e63b31f8 52 i2c.write(PCF8574_ADDRoutput , data_write, 1, 0); // output array 1byte stop
schlaumaier54 0:b183e63b31f8 53 //you see a digital 8bit PCF8574 output (8xleds), received from PCF8574 input (8xswitsches)
schlaumaier54 0:b183e63b31f8 54
schlaumaier54 0:b183e63b31f8 55 //Here your solutions from task1 and 2:
schlaumaier54 0:b183e63b31f8 56 }
schlaumaier54 0:b183e63b31f8 57
schlaumaier54 0:b183e63b31f8 58 }
schlaumaier54 0:b183e63b31f8 59