Hi all, I usually write in the italian forum, but now i've a problem that my friends can't solve so I'll try to ask here.
I've got some PCA9555n GPIO port expander and an arduino diecimila, i would like to blink a led with that IC first. Than i'll try to control some stepper.
Ok, some technical documentation that you can find in the datasheet!
PCA9555n has 16bit, that are configurable in 8 ways writing a byte in the command register
The command byte is the first byte to follow the address byte during a write transmission. It is used as a pointer to determine which of the following registers will be written or read
command = 0 port0 input
command = 1 port1 input
command = 2 port0 Output
command = 3 port1 Output
command = 4 port0 Polarity inversion
command = 5 port1 " " " " "
command = 6 port0 Configuration (that configure the direction of the pins, 1 is input 0 is output)
command = 7 port1 " " " " " " " " "
The pca9555n has 3 programmable pins for address that allows you to put in the same i2c bus 8 i2c with different address.
So the first byte that i've to send to the IC is the address (0100 A0 A1 A2), than the command byte and than data's byte.
I found something really cool in this website
http://www.neufeld.newton.ks.us/electronics/?p=241 ,i've studied the code but no gratifying results.
I know that i've pullup resistors on the outputs pins, but i don't think that is the problem.
so can someone help me to understand this code and modify it to for blink a led.
/******************************************************************************
* i2c_gpio
* Keith Neufeld
* May 26, 2008
*
* Prototype I2C interface to TI 9535 and 9555 GPIO expanders.
*
* Arduino analog input 5 - I2C SCL
* Arduino analog input 4 - I2C SDA
*
******************************************************************************/
#include <Wire.h>
//#define DEBUG_GPIO
// I2C device address is 0 1 0 0 A2 A1 A0
#define DIP_ADDRESS (0x4 << 3 | 0x0)
#define LED_ADDRESS (0x4 << 3 | 0x7)
void setup() {
#ifdef DEBUG_GPIO
Serial.begin(9600);
//Serial.println(DIP_ADDRESS); delay(2000);
#endif
Wire.begin();
gpio_write(LED_ADDRESS, 0xffff);
gpio_dir(LED_ADDRESS, 0x0000);
}
void loop() {
int bits;
#ifdef DEBUG_GPIO
Serial.println(0xff & gpio_read(DIP_ADDRESS));
#endif
bits = gpio_read(DIP_ADDRESS) & 0x0f;
// mirror direction of bits for output display
gpio_write(LED_ADDRESS,
(
((bits & 1) << 3) | ((bits & 2) << 1) |
((bits & 4) >> 1) | ((bits & 8) >> 3)
) << 12);
#ifdef DEBUG_GPIO
delay(200);
#endif
}
#define REGISTER_INPUT (0)
#define REGISTER_OUTPUT (2)
#define REGISTER_CONFIG (6)
int gpio_read(int address) {
int data = 0;
// Send input register address
Wire.beginTransmission(address);
Wire.send(REGISTER_INPUT);
Wire.endTransmission();
// Connect to device and request two bytes
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
if (Wire.available()) {
data = Wire.receive();
}
if (Wire.available()) {
data |= Wire.receive() << 8;
}
Wire.endTransmission();
return data;
}
void gpio_dir(int address, int dir) {
// Send config register address
Wire.beginTransmission(address);
Wire.send(REGISTER_CONFIG);
// Connect to device and send two bytes
Wire.send(0xff & dir); // low byte
Wire.send(dir >> 8); // high byte
Wire.endTransmission();
}
void gpio_write(int address, int data) {
// Send output register address
Wire.beginTransmission(address);
Wire.send(REGISTER_OUTPUT);
// Connect to device and send two bytes
Wire.send(0xff & data); // low byte
Wire.send(data >> 8); // high byte
Wire.endTransmission();
}
maybe it's simple but i need help to understand becouse i'm a simple electronics student.
Sorry for my english :S
Best Regards!