PIC16F877a + I2C Code

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Hai,

Here i am trying to communicate sensor ad7156 thorugh I2C communication in pic.
1. Before doing i am checking the status of SDA and SCL, its is still in ideal stage. there was no change of signal in logic analyzer
2. USing MPLAB IDE for coding it was build successful. still not owrking.
somebody can help on this
uploading code here,
C:
#include<pic.h>

typedef unsigned char int8;
int i;
void delay()
{
    for(i=0;i<10000;i++);
}
void init()
{
    TRISC=0X18;
    SSPADD=26;
    SSPSTAT=0X80;
    SSPCON=0X28;
}  
void start()
{
    SEN=1;
    while(!SSPIF);
    SSPIF=0;
}

void stop()
{
    PEN=1;
    while(!SSPIF);
    SSPIF=0;
}  
void send(int8 b)
{
    static bit s;
    SSPBUF=b;
    while(!SSPIF);
    SSPIF=0;
    s=SSPSTAT;
    delay();
}  
void write(int8 b)
{
    //start();
    //send(0x90);
    send(b);
    stop();
}      
void main()
{
    //ADCON1 = 0x06; // no analog
    init();
    while(1)
    {   
        send('A');  
    }
}
Mod edit: code tags
 
Last edited by a moderator:

Ian Rogers

Joined Dec 12, 2012
901
Writing..

Start();
write( deviceAddr ); .. 0x90..
write( intAddr ); ... which reg are you changing..
write( data ); ... new data
stop();

Reading..
Start();
write( deviceAddr ); .. 0x90..
write( intAddr ); ... which reg are you reading..
ReStart();
write( deviceAddr ); .. 0x91..
read( data ); ...get data ( if more data needed send ack() and read next character )
stop();
 
Top