PIC16F724 communication with PCA9622 using I2C
Hi all.
I have recieved a circuitboard that i have to program to do some simple stuff. The only problem is that i have very little experience in C. I am used to programming PLC's in ladder.
The board has 30 led's that are connected with two PCA9622 chips controlled by the PIC. There are 17 inputs and two outputs on the board. The first 15 led's are green and the last 15 are yellow.
LED1 and LED16 are combined in one, LED2 and 17 are combined and so on..
The function should be this:
If input 1 gets high led1 or led16 goes on, if in2 gets high led2 or led17 goes on and so on. So i should be able to chose if it is the green or the yellow led that is lit whenever input x is high.
If input 16 goes high all yellow led's should be lit to see if they work.
The two outputs should be set high if certain led's are on. I should be able to write which led's would set one of the outputs high.
That is basically it.
I have read through the datasheet of the PCA9622 and also the PIC. I have programmed this type of PIC before to drive som ledīs, but not through i2c.
I am very confused on how to deal with this.
I have some code, but i am not sure how to do this. The first thing is to be able to turn on the led's individually.
So far i have understood this is how it goes:
Start
1. adress of slave (PCA9622)
2. go to mode 1 register (don't know why, but that is what i get from the datasheet)
3. ReStart
4. adress of slave again
5. and know i am lost?!? :S
Code:
#include <htc.h>
#define _XTAL_FREQ 16000000
#define SCL RC3 // I2C bus
#define SDA RC4 //
#define green 0x14;
#define red 0x15;
void i2c_dly(void)
{
}
void i2c_start(void)
{
SDA = 1; // i2c start bit sequence
i2c_dly();
SCL = 1;
i2c_dly();
SDA = 0;
i2c_dly();
SCL = 0;
i2c_dly();
}
void i2c_stop(void)
{
SDA = 0; // i2c stop bit sequence
i2c_dly();
SCL = 1;
i2c_dly();
SDA = 1;
i2c_dly();
}
main(void){
i2c_start(); // send start sequence
SDA = green; //adress for the green led driver
SDA = 0x00; //Mode 1 register
_delay_us(600);
i2c_start(); //restart
SDA = green; //adress for the green led driver
SDA = 0x14;
i2c_stop(); // send stop sequence
}
I have a schematic for the board, but the pdf is to large for upload to the forum, so i will get it resized later.
But the datasheet for PCA9622 is here http://www.nxp.com/documents/data_sheet/PCA9622.pdf
Any help for the part with the i2c and PCA9622 would be much appreciated!
Regards Nic