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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Data send-receive by IIC-bus Hardware: IIC-board with 4 x PCF8574 IC
00002 you see a 8bit leds this leds show the value from an other 8574 who has 8 switches as input
00003 The 8xswitches from IIC you can see also at the pins PC_0 to PC_7
00004 Tasks: 1. Receive a analog value and show this value as a 8xled-strip at the third PCF8574 IC
00005         2. Send the 8xswitches from IIC-bus to the virtuell COM-port. You can visualize the switches
00006            with the Software "Profilab" or "LABview"
00007 Aug. 2015 G. Neumaier Gewerblich-Technische Schule Offenburg Germany
00008 */
00009 
00010 #include "mbed.h"
00011 #define PCF8574_ADDRoutput  0x46 // PCF8574 address 0x46 lowest bit (R/W)=0 IICboard P3
00012 #define PCF8574_ADDRinput   0x45 // PCF8574 address 0x45 lowest bit (R/W)=1 IICboard P2
00013         //ADR-Jumper on IIC4xPCF8574board jumped to 0: Basis ADR:P0=0x40;P1=0x42;P2=0x44;P3=0x46;
00014        //ADR-Jumper on IIC4xPCF8574board jumped to 1: Basis ADR:P0=0x48;P1=0x4A;P2=0x4C;P3=0x4E; 
00015 #define LED_MASK  0xffff    //Portc has 16bit, therefore 4digit mask
00016                             // 1=output    0=input
00017               
00018 I2C i2c(I2C_SDA, I2C_SCL); //IIC Pins SDA and SCL for ARDUINO pinheader Nucleo-board
00019             //SCL and SPA pullup resistors 2k2Ohm to+5Volt
00020             
00021 PortOut leds(PortC, LED_MASK); //Important: Port_B not possible -> PB_8 PB_9 Pins for IIC-bus
00022             
00023 DigitalOut myled(LED1); //LED on Nucleo-board
00024 //for the task1****:  AnalogIn analog_value0(A0);      //same as Pin PA_0 
00025 //for the task2****:  Serial pc(SERIAL_TX, SERIAL_RX);
00026  
00027 int main()
00028 {
00029     char input8bit;
00030     char data_write[2]; //must be char!! a array with 2storage places
00031     char data_read[2]; //read buffer   a array with 2storage places
00032  
00033     data_write[0] = 0xf2; //LEDs low aktiv  dummy
00034     int status = i2c.write(PCF8574_ADDRoutput , data_write, 1, 0);
00035     if (status != 0) // Error -> no acknowledge detected 
00036     { 
00037         while (1) //-> endless loop when error IIC detected
00038         {   
00039         myled = !myled;
00040         wait(0.7);
00041         }
00042     }
00043  
00044     while (1) //endless loop
00045         {
00046  
00047         // i2c.read will start the IIC-bus with the last 0 also stop the same by 12c.read
00048         i2c.read(PCF8574_ADDRinput, data_read, 1, 0);  // read to array  1byte  stop
00049         input8bit = data_read[0];   //array value to variable
00050         leds = input8bit; //Output PortC low-byte 
00051         data_write[0] = input8bit;     //8bit analog-value to array
00052         i2c.write(PCF8574_ADDRoutput , data_write, 1, 0); //  output array 1byte  stop 
00053         //you see a digital 8bit PCF8574 output (8xleds), received from PCF8574 input (8xswitsches)
00054         
00055         //Here your solutions from task1 and 2:
00056     }
00057  
00058 }
00059