starguard2527
Newbie level 4
Hello,
Guys i am doing a project in which i need to communicate between two 16f877a controllers. and the restriction is to use i2c. i have developed a hardware in which one pic is the master and other is the slave. there is an lcd connected to the slave. not the task is to transmit data from master to slave and display it on lcd.
diagramatically:
Master PIC >> Slave PIC>> 16x2 LCD
i am using mikro c for this and the code which i have written is as follow:
MASTER CODE:
void main() {
while(1){
i2c1_init(100000);
i2c1_start();
i2c1_wr(0x02);
i2c1_wr(0x69);
i2c1_stop();
delay_ms(250);
}
}
SLAVE CODE:
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
unsigned short get_rid_of_addr(void);
unsigned short get_i2c_data(void);
void main(){
char fb_val_txt[4];
trisc=0b00011000;
trisb.F0=1;
SSPCON=0b00010110;
SSPCON2.F0=0;
GCEN_bit=0;
SSPSTAT.F6=0;
SSPSTAT.F7=1;
SSPADD=0x01;
CKE_bit=0;
SSPEN_bit=1;
lcd_init();
lcd_out(1,1,"Executing");
while(portb.F0==0);
while(1){
get_rid_of_addr();
ByteToStr(get_i2c_data(),fb_val_txt);
CKP_bit=1;
Lcd_Cmd(_LCD_CLEAR);
lcd_out(1,1,fb_val_txt);
delay_ms(250);
}
lcd_out(2,1,"Exiting");
}
unsigned short get_rid_of_addr(void){
short dummy,i=0;
GIE_bit=1;
PEIE_bit=1;
SSPIE_bit=1;
lcd_out(2,1,"getting rid");
while( SSPIF_bit==0)
{
i=2;
if (SSPOV_bit==1)
lcd_out(1,1,"Overflow");
}
lcd_cmd(_LCD_CLEAR);
lcd_out(i,1,"got it");
delay_ms(500);
dummy=SSPBUF;
SSPIF_bit=0;
CKP_bit=1;
return dummy;
}
unsigned short get_i2c_data(void){
unsigned short dat;
while(SSPIF_bit==0)
{
dat=0;
}
dat=SSPBUF;
SSPIF_bit=0;
CKP_bit=1;
return dat;
}
I have also heard that pic16f877a has some hardware problems with i2c interface. is it a truth or myth....
PLEASE PLEASE PLEASE HELP ME OUT... Thanks in advance.
Guys i am doing a project in which i need to communicate between two 16f877a controllers. and the restriction is to use i2c. i have developed a hardware in which one pic is the master and other is the slave. there is an lcd connected to the slave. not the task is to transmit data from master to slave and display it on lcd.
diagramatically:
Master PIC >> Slave PIC>> 16x2 LCD
i am using mikro c for this and the code which i have written is as follow:
MASTER CODE:
void main() {
while(1){
i2c1_init(100000);
i2c1_start();
i2c1_wr(0x02);
i2c1_wr(0x69);
i2c1_stop();
delay_ms(250);
}
}
SLAVE CODE:
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
unsigned short get_rid_of_addr(void);
unsigned short get_i2c_data(void);
void main(){
char fb_val_txt[4];
trisc=0b00011000;
trisb.F0=1;
SSPCON=0b00010110;
SSPCON2.F0=0;
GCEN_bit=0;
SSPSTAT.F6=0;
SSPSTAT.F7=1;
SSPADD=0x01;
CKE_bit=0;
SSPEN_bit=1;
lcd_init();
lcd_out(1,1,"Executing");
while(portb.F0==0);
while(1){
get_rid_of_addr();
ByteToStr(get_i2c_data(),fb_val_txt);
CKP_bit=1;
Lcd_Cmd(_LCD_CLEAR);
lcd_out(1,1,fb_val_txt);
delay_ms(250);
}
lcd_out(2,1,"Exiting");
}
unsigned short get_rid_of_addr(void){
short dummy,i=0;
GIE_bit=1;
PEIE_bit=1;
SSPIE_bit=1;
lcd_out(2,1,"getting rid");
while( SSPIF_bit==0)
{
i=2;
if (SSPOV_bit==1)
lcd_out(1,1,"Overflow");
}
lcd_cmd(_LCD_CLEAR);
lcd_out(i,1,"got it");
delay_ms(500);
dummy=SSPBUF;
SSPIF_bit=0;
CKP_bit=1;
return dummy;
}
unsigned short get_i2c_data(void){
unsigned short dat;
while(SSPIF_bit==0)
{
dat=0;
}
dat=SSPBUF;
SSPIF_bit=0;
CKP_bit=1;
return dat;
}
I have also heard that pic16f877a has some hardware problems with i2c interface. is it a truth or myth....
PLEASE PLEASE PLEASE HELP ME OUT... Thanks in advance.