Gerhard Neumaier / Mbed 2 deprecated Nucleo_IIC_8574a_ana_ledband

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Data send to IIC-board with PCF8574a IC
00002 Data comming from analog-value Pin A0 = PA_0 Nucleo-board
00003 you see a 8bit light strip as a result
00004 Aug. 2015 G. Neumaier Gewerblich-Technische Schule Offenburg Germany
00005 */
00006 
00007 #include "mbed.h"
00008 #define PCF8574a_ADDR  0x72 // PCF8574a address IIC-board Birk 
00009 
00010 I2C i2c(I2C_SDA, I2C_SCL); //IIC Pins SDA and SCL for ARDUINO pinheader Nucleo-board
00011             //SCL and SPA pullup resistors 2k2Ohm to+5Volt
00012 DigitalOut myled(LED1); //LED on Nucleo-board
00013 AnalogIn analog_value0(A0);      //same as Pin PA_0 
00014 Serial pc(SERIAL_TX, SERIAL_RX);
00015  
00016 int main()
00017 {
00018     float mess0;
00019     unsigned char mess1, x, LEDs8;
00020     char data_write[2]; //must be char!!
00021  //   char data_read[2]; //read buffer
00022  
00023     unsigned char speicher[9] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01,0x00}; //data for light strip
00024     data_write[0] = 0xf2; //LEDs low aktiv  dummy
00025     int status = i2c.write(PCF8574a_ADDR , data_write, 1, 0);
00026     if (status != 0) // Error  no acknowledge detected 
00027     { 
00028         while (1) //-> endless loop when error no IIC-IC detected
00029         {   
00030         myled = !myled;
00031         wait(0.7);
00032         }
00033     }
00034  
00035     while (1) //endless loop
00036         {
00037         mess0 = analog_value0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00038         mess0 = mess0 * 255; // Change the value to be in the 0 to 255 range -> 8bit
00039         mess1 = (char) mess0;  //type converting from float to char(8bit)
00040         printf("Analogvalue0 = %.1f \r\n", mess0);  //Rs232 output-> You will see this in terminalprogram on PC
00041         //%.1f -> display 1 digit after comma
00042         // 
00043         x= mess1/29;        //Remainder of the division falls away, only whole numbers
00044         LEDs8=  speicher[x];        //brings accordingly x values aus dem array "speicher"
00045         data_write[0] = ~LEDs8;     //8bit analog-value  ~ inverted  leds lowaktiv!!
00046         i2c.write(PCF8574a_ADDR , data_write, 1, 1); // no stop 
00047         //you see a digital 8bit counter at leds contacted to IC PCF8574
00048     }
00049  
00050 }
00051