This is my i2c program to send one data to slave. i use pcf8574A an i2c expander. Still i am not getting any acknowledgement from slave, even though the address byte mentioned in the program and the address of the PCF8574A are same. :-(
#include<htc.h>
#include"delay.c"
#include"lcd_driver.c"
__PROG_CONFIG(1,0x0200);
__PROG_CONFIG(2,0x000F);
__PROG_CONFIG(3,0x0003);
__PROG_CONFIG(4,0x0005);
void setup_i2c(void); //function proto types
void i2c_master(void);
void start(void);
void stop(void);
void wait(void);
void restart(void);
void nack(void);
void ack(void);
unsigned char i2c_write(unsigned char);
unsigned char i2c_read(void);
int i=0;
void main()
{
setup_lcd_port();
lcd_init();
lcd_puts("I2C COMMUNICATION");
lcd_clrscr();
setup_i2c(); //i2c port setup
DelayMs(1);
i2c_master(); //i2c as a master device
DelayMs(1);
while(1)
{
start();
i2c_write(0x71);
if(ACKSTAT==1)
{
lcd_puts("ACK NOT RECEIVED");
lcd_goto_pos(1);
stop();
}
else
{
while(!SSPIF)
continue;
SSPIF=0;
lcd_puts("ACK WAS RECEIVED");
i2c_write(0x55);
DelayMs(100);
i2c_write(0xAA);
DelayMs(100);
stop();
}
}
}
void setup_i2c(){
TRISC3=TRISC4=1; //use RC3=SCL,RC4=SDA
}
void i2c_master(void){
SSPCON1=0x28; //wcol=0,sspov=0,sspen=1,ckp=1,ssp mode=1000.
SSPCON2=0x50;
SSPSTAT=0xC0; //smp=1 Input data sampled at end of data output time
SSPADD=(20000/((4*100)+1)); //
}
void wait(void){
while(!SSPIF)
continue;
SSPIF=0;
}
void start(void){
SEN=1;
wait();
}
void stop(void){
PEN=1;
while(!SSPIF)
continue;
SSPIF=0;
}
unsigned char i2c_write(unsigned char wbyte){
WCOL=0;
SSPOV=0;
SSPBUF=wbyte;
wait();
return SSPBUF;
}