I have a circuit with a PCA9635 PWM LED driver. I want to turn on and off LEDs one after another, but my code does not work. I think the hardware is OK. My micro is a 16F887.
Can you help me?
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000
#define PCA963X_LED_OFF 0x0 //LED driver off//
#define PCA963X_LED_ON 0x1 //LED driver on//
#define PCA963X_LED_PWM 0x2 //Controlled through PWM//
#define PCA963X_LED_GRP_PWM 0x3 //Controlled through PWM/GRPPWM//
#define PCA963X_MODE2_OUTDRV 0x04 //Open-drain or totem pole//
#define PCA963X_MODE2_INVRT 0x10 //Normal or inverted direction//
#define PCA963X_MODE2_DMBLNK 0x20 //Enable blinking//
#define PCA963X_MODE1 0x00
#define PCA963X_MODE2 0x01
#define PCA963X_LED0_ON 0x02 // LED0
#define PCA963X_LED2_ON 0x04 // LED2
#define PCA963X_LED3_ON 0x05 // LED3
#define PCA963X_LED4_ON 0x06 // LED4
#define PCA963X_LED5_ON 0x07 // LED5
#define PCA963X_LED6_ON 0x08 // LED6
#define PCA963X_LED7_ON 0x09 // LED7
#define PCA963X_LED8_ON 0x0A // LED8
#define PCA963X_LED9_ON 0x0B // LED9
#define PCA963X_LED10_ON 0x0C// LED10
#define PCA963X_LED11_ON 0x0D// LED11
#define PCA963X_LED12_ON 0x0E// LED12
#define PCA963X_LED13_ON 0x0F// LED13
#define PCA963X_LED14_ON 0x10// LED14
#define PCA963X_LED15_ON 0x11// LED15
void main(void)
{
/*
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
PORTB = 0;
TRISB = 0; // Configure PORTB as output
*/
TRISA.f3=0;// make RA3 out------> OE of pca9635
TRISD.f0=0;// make RD0 out for test program is correct
PORTA.f0 = 0; //---> OE low
PORTD.f0 =1; //
Delay_ms(4000); //add delay of 1 second
I2C1_Init(100000);
I2C1_Start();
I2C1_Wr(0b00101010); //-----> slave address 0x2A
I2C1_Wr(0b00000110); //-----> slave software reset
//I2C1_Wr(0x1 );
I2C1_Stop();
PORTD.f0 =0;
Delay_ms(100); //add delay of 1 second
while(1){
PORTA.f0 = 0; //---> OE low
if (I2C1_Is_Idle()) {
PORTD.f0 =1;
Delay_ms(1000);
I2C1_Start();
I2C1_Wr(0b00101010); //-----> slave address
I2C1_Wr(0x1);
I2C1_Wr(0x01);
I2C1_Wr(0x02);
I2C1_Wr(0x03);
I2C1_Wr(0x04);
I2C1_Wr(0x05);
I2C1_Wr(0x06);
I2C1_Wr(0x07);
I2C1_Wr(0x08);
I2C1_Wr(0x09);
I2C1_Wr(0x0A);
I2C1_Wr(0x0B);
I2C1_Stop();
Delay_ms(1000);
I2C1_Start();
I2C1_Wr(0b00101010); //-----> slave address
I2C1_Wr(0xE0);
I2C1_Wr(0xF4);
I2C1_Wr(0x02);
I2C1_Wr(0x02);
I2C1_Wr(0x03);
I2C1_Wr(0x04);
I2C1_Wr(0x05);
I2C1_Wr(0x06);
I2C1_Wr(0x07);
I2C1_Wr(0x08);
I2C1_Wr(0x09);
I2C1_Wr(0x0A);
I2C1_Wr(0x0B);
I2C1_Stop();
Delay_ms(1000);
I2C1_Start();
I2C1_Wr(0b00101010); //-----> slave address
I2C1_Wr(0b10110110); //----->control register
I2C1_Wr(0x00);
I2C1_Wr(1);
I2C1_Stop();
Delay_ms(1000);
PORTD.f0 =0;
Delay_ms(1000); //add delay of 1 second
}
}
}