Data send to IIC-board with PCF8574a IC Data comming from analog-value Pin A0 = PA_0 Nucleo-board you see a 8bit light strip as a result
Testboard IIC with different ICs like PCF8574a PCF8591 etc:
Download Programmbeschreibung:
/media/uploads/schlaumaier54/led_band8bitiic_analog_ein.pdf
Neumaier 2018
main.cpp@0:89b72ed83c96, 2015-08-20 (annotated)
- Committer:
- schlaumaier54
- Date:
- Thu Aug 20 21:48:46 2015 +0000
- Revision:
- 0:89b72ed83c96
Data send to IIC-board with PCF8574a IC; Data comming from analog-value Pin A0 = PA_0 Nucleo-board; you see a 8bit light strip as a result
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
schlaumaier54 | 0:89b72ed83c96 | 1 | /*Data send to IIC-board with PCF8574a IC |
schlaumaier54 | 0:89b72ed83c96 | 2 | Data comming from analog-value Pin A0 = PA_0 Nucleo-board |
schlaumaier54 | 0:89b72ed83c96 | 3 | you see a 8bit light strip as a result |
schlaumaier54 | 0:89b72ed83c96 | 4 | Aug. 2015 G. Neumaier Gewerblich-Technische Schule Offenburg Germany |
schlaumaier54 | 0:89b72ed83c96 | 5 | */ |
schlaumaier54 | 0:89b72ed83c96 | 6 | |
schlaumaier54 | 0:89b72ed83c96 | 7 | #include "mbed.h" |
schlaumaier54 | 0:89b72ed83c96 | 8 | #define PCF8574a_ADDR 0x72 // PCF8574a address IIC-board Birk |
schlaumaier54 | 0:89b72ed83c96 | 9 | |
schlaumaier54 | 0:89b72ed83c96 | 10 | I2C i2c(I2C_SDA, I2C_SCL); //IIC Pins SDA and SCL for ARDUINO pinheader Nucleo-board |
schlaumaier54 | 0:89b72ed83c96 | 11 | //SCL and SPA pullup resistors 2k2Ohm to+5Volt |
schlaumaier54 | 0:89b72ed83c96 | 12 | DigitalOut myled(LED1); //LED on Nucleo-board |
schlaumaier54 | 0:89b72ed83c96 | 13 | AnalogIn analog_value0(A0); //same as Pin PA_0 |
schlaumaier54 | 0:89b72ed83c96 | 14 | Serial pc(SERIAL_TX, SERIAL_RX); |
schlaumaier54 | 0:89b72ed83c96 | 15 | |
schlaumaier54 | 0:89b72ed83c96 | 16 | int main() |
schlaumaier54 | 0:89b72ed83c96 | 17 | { |
schlaumaier54 | 0:89b72ed83c96 | 18 | float mess0; |
schlaumaier54 | 0:89b72ed83c96 | 19 | unsigned char mess1, x, LEDs8; |
schlaumaier54 | 0:89b72ed83c96 | 20 | char data_write[2]; //must be char!! |
schlaumaier54 | 0:89b72ed83c96 | 21 | // char data_read[2]; //read buffer |
schlaumaier54 | 0:89b72ed83c96 | 22 | |
schlaumaier54 | 0:89b72ed83c96 | 23 | unsigned char speicher[9] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01,0x00}; //data for light strip |
schlaumaier54 | 0:89b72ed83c96 | 24 | data_write[0] = 0xf2; //LEDs low aktiv dummy |
schlaumaier54 | 0:89b72ed83c96 | 25 | int status = i2c.write(PCF8574a_ADDR , data_write, 1, 0); |
schlaumaier54 | 0:89b72ed83c96 | 26 | if (status != 0) // Error no acknowledge detected |
schlaumaier54 | 0:89b72ed83c96 | 27 | { |
schlaumaier54 | 0:89b72ed83c96 | 28 | while (1) //-> endless loop when error no IIC-IC detected |
schlaumaier54 | 0:89b72ed83c96 | 29 | { |
schlaumaier54 | 0:89b72ed83c96 | 30 | myled = !myled; |
schlaumaier54 | 0:89b72ed83c96 | 31 | wait(0.7); |
schlaumaier54 | 0:89b72ed83c96 | 32 | } |
schlaumaier54 | 0:89b72ed83c96 | 33 | } |
schlaumaier54 | 0:89b72ed83c96 | 34 | |
schlaumaier54 | 0:89b72ed83c96 | 35 | while (1) //endless loop |
schlaumaier54 | 0:89b72ed83c96 | 36 | { |
schlaumaier54 | 0:89b72ed83c96 | 37 | mess0 = analog_value0.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
schlaumaier54 | 0:89b72ed83c96 | 38 | mess0 = mess0 * 255; // Change the value to be in the 0 to 255 range -> 8bit |
schlaumaier54 | 0:89b72ed83c96 | 39 | mess1 = (char) mess0; //type converting from float to char(8bit) |
schlaumaier54 | 0:89b72ed83c96 | 40 | printf("Analogvalue0 = %.1f \r\n", mess0); //Rs232 output-> You will see this in terminalprogram on PC |
schlaumaier54 | 0:89b72ed83c96 | 41 | //%.1f -> display 1 digit after comma |
schlaumaier54 | 0:89b72ed83c96 | 42 | // |
schlaumaier54 | 0:89b72ed83c96 | 43 | x= mess1/29; //Remainder of the division falls away, only whole numbers |
schlaumaier54 | 0:89b72ed83c96 | 44 | LEDs8= speicher[x]; //brings accordingly x values aus dem array "speicher" |
schlaumaier54 | 0:89b72ed83c96 | 45 | data_write[0] = ~LEDs8; //8bit analog-value ~ inverted leds lowaktiv!! |
schlaumaier54 | 0:89b72ed83c96 | 46 | i2c.write(PCF8574a_ADDR , data_write, 1, 1); // no stop |
schlaumaier54 | 0:89b72ed83c96 | 47 | //you see a digital 8bit counter at leds contacted to IC PCF8574 |
schlaumaier54 | 0:89b72ed83c96 | 48 | } |
schlaumaier54 | 0:89b72ed83c96 | 49 | |
schlaumaier54 | 0:89b72ed83c96 | 50 | } |
schlaumaier54 | 0:89b72ed83c96 | 51 |