Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2C NOT WORKING

Status
Not open for further replies.
hi ,
recently i switched to pic 18f
i cant get the program for i2c correct can any body help


C:
#include<htc.h>

__CONFIG(2, PWRTDIS & WDTPS1 & WDTEN &WDTDIS);
__CONFIG(4, STVRDIS);
#define _XTAL_FREQ 16000000



void _delay(unsigned long cycles);
void i2c_init(void);
bit i2c_write_byte(unsigned char byte);
unsigned char i2c_read_byte(void);
void i2c_send_ack(void);
void i2c_send_nack(void);
void i2c_start(void);
void i2c_stop(void);
void i2c_repstart(void);

void main()
{
    unsigned char byte;
    byte = 0xff;
    TRISB =0XFF;
    i2c_init();

  
    while(1)
    {
  
        i2c_start();
      
        i2c_write_byte(0xD0);
        i2c_repstart();
        i2c_write_byte(0x00);
        i2c_write_byte(0x55);
        i2c_write_byte(0x02);
        i2c_stop();
      
        _delay(10000);
        
        
    }
}
void i2c_init()
{
    SMP = 1;
    SSPCON1 = 0X3B;
}  
void i2c_start(void)
{
    SEN = 1;
    while(!SSPIF);
    SSPIF=0;
}
void i2c_repstart(void)
{
    RSEN = 1;
    while(!SSPIF);
    SSPIF = 0;
    return;
}
void i2c_stop(void)
{
    PEN = 1;
    while(!SSPIF);
    SSPIF =0 ;
}
void i2c_send_ack(void)
{
    ACKDT = 0;
    ACKEN = 1;
    while(!SSPIF);
    SSPIF =0;
}
void i2c_send_nack(void)
{
    ACKDT =1;//Not Acknowledged
    ACKEN = 1;
    while(!SSPIF);
    SSPIF =0;
}
bit i2c_write_byte(unsigned char byte)
{
    SSPBUF = byte;
    while(!SSPIF);
    SSPIF = 0;
    return ACKSTAT;
}
unsigned char i2c_read_byte(void)
{
    RCEN =1;
    while(!SSPIF);
    SSPIF = 0;
    return SSPBUF;
}


when i simulate in proteus i get the following outputs.
it doesnt make any sense .
 

Attachments

  • circuit.JPG
    circuit.JPG
    117.5 KB · Views: 210
  • i2c debugger.JPG
    i2c debugger.JPG
    21.3 KB · Views: 201
  • oscilloscope.JPG
    oscilloscope.JPG
    19.4 KB · Views: 212
Last edited by a moderator:
You do not need the repeat start if you are writing only.... A repeat start is for the read cycle when you switch from write to read mode.
writing...
C:
        i2c_start();     
        i2c_write_byte(0xD0);   // RTC write address
        i2c_write_byte(0x00);   // Internal address
        i2c_write_byte(0x55);   //  first byte written
        i2c_write_byte(0x02);   // second byte written
        i2c_stop();
reading...
C:
        i2c_start();     
        i2c_write_byte(0xD0);   // RTC write address
        i2c_write_byte(0x00);   // Internal address
        i2c_repstart();
        i2c_write_byte(0xD1);   // RTC read address
        a = i2c_get_byte(1);     //  first byte received
        b = i2c_get_byte(0);   // second byte received
        i2c_stop();

I'm assuming the I2C_get_byte(); as this isn't a XC8 library routine!!!
 
Code:
void i2c_stop(void)
{
    PEN = 1;
    while(!SSPIF);
    SSPIF =0 ;
}

I assume than using interrupt flag ( SSPIF ) following source code more relabel

Code:
void i2c_stop(void)
{
    PEN = 1;
    while(PEN);
   
}
 
thanks Ian
I have corrected that but the problem still stands
my problem is what the i2c debugger displays when the ckt is simulated in proteus
it just displays start and stop but no data in between
 

Attachments

  • i2c debugger.JPG
    i2c debugger.JPG
    21.3 KB · Views: 198
:woot:problem solved .
credits to Ian . i just replaced the pull ups and it worked. while doing the practical circuit will a 10k resistor be enough?


by the way I didn't use any external libraries ,its all in my code:cool:
 
I have always use 4k7.... But it largely depends on how many devices you put on the bus... I have two..The RTC and the EEprom....

aswin said:
by the way I didn't use any external libraries ,its all in my code

Yeah! Sorry I clocked that afterwards....
 
SSPCON = 0X3B works just fine
its starts i2c mode in master mode ,slave idle . for my purpose that's enough . as its the only master, address doesn't really matters
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top