Hello,
I wanted to make a PCF8574P to control (output) 8 different LED;
But I still do not understand how to select each LED ...
leaving out the links to the LED, assuming that the address of the PCF is x, what code would you use you?
(p.s. 0x0 should set the i-th output "high" and then turn off the LED, and 0x80 should set "low" and then turn on, isn't it? please include this in your code, especially if it's wrong this supposition! XD)
/* I2C Interface - Mike Cook December 2008
*
* -----------------
* Makes the Arduino simulate a
* Robot-electronics USB to I2C interface (www.robot-electronics.co.uk)
* as used in the Transistot Tester
* Pin 13 LED will flash during an I2C command
###############################################################################################
HARDWARE NOTE:
* Arduino analog input 5 - I2C SCL - requires a pull up resistor of about 3K
* Arduino analog input 4 - I2C SDA - requires a pull up resistor of about 3K
* Also connect 5V and Gnd to the I2C lead
################################################################################################
*/
// Includes
#include <Wire.h>
#define LEDpin 13 // Indicator LED:
// Variables setup
int incomingByte, iicAddress, iicData;
// Start of code
void setup() {
pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin, LOW); // turn of command indicator
Wire.begin(); // start the I2C interface
Serial.begin(19200); // Start serial port speed
}
//********************* MAIN LOOP ***********************************
void loop() {
if(Serial.available() > 0){
incomingByte = Serial.read();
if(incomingByte == 0x53){ // got the start command
digitalWrite(LEDpin, HIGH); // indicate an I2C command
incomingByte = getNextByte(); // get the next byte in the command, the address
iicAddress = incomingByte >> 1; // change the address to the Arduino form without LSB R/W
if((incomingByte & 1) == 0) { // LSB is a zero therefore it's a write
iicData = getNextByte(); // get the data to write
gpio_write(iicAddress, iicData); // write it
Serial.print(0x01, BYTE); // acknowlage the write, back to the serial port
}
else { // LSB of the incomingByte is a one therefore it's a read request
iicData = gpio_read(iicAddress);
Serial.print(iicData, BYTE); // send the data back, through the serial port
}
}
}
digitalWrite(LEDpin, LOW); // indicate the end of an I2C command
} // end loop function
//********************* Functions ***********************************
byte getNextByte() {
while(Serial.available() == 0) { } // do nothing until a byte is avaliable
return(Serial.read());
}
void gpio_write(int address, int data) {
// Send device address
Wire.beginTransmission(address);
Wire.send(data ); // send byte
Wire.endTransmission();
}
int gpio_read(int address) {
int data = 0;
// Connect to device and request one bytes
Wire.requestFrom(address, 1);
while(Wire.available()) // slave may send less than requested
{
data = Wire.receive(); // receive a byte as character
}
return data;
}
I can not see how it controls the i-th output of the PCF… I have 8 leds connected to the PCF, I want to turn a (given) led at a time …
simply do:
Wire.beginTransmission (x);
Wire.send (i) / / to tourn on/off the i-th led???
Wire.endTransmission ();
I can not understand … Forgive, but my head is taken by many things …
Where x is the address of the chip.
If you have the LEDs connected correctly, sending 0 to the chip should light no LEDs. Sending 0b00000001 (or 0x01 or 1) should light the 1st LED. Sending 0b00000010 (or 0x02 or 2) should light the 2nd LED.
In general:
byte b = 0;
bitSet(b, n);
and sending b to the chip should light the nth LED.