Connect with us

what is sda setup time violation in EEPROM interfacing with PIC18F452

Discussion in 'Microcontrollers, Programming and IoT' started by billy2tom, Sep 29, 2010.

Scroll to continue with content
  1. billy2tom

    billy2tom

    2
    0
    Sep 25, 2010
    i simulated the pgm using c18 compiler. when i simulate in proteus i am getting
    error as sda setup time error. Can anyone help me plz...


    #pragma config OSC=HS, OSCS = OFF
    #pragma config PWRT = OFF, BOR = ON, BORV = 45
    #pragma config WDT = OFF
    #pragma config DEBUG = OFF, LVP = OFF, STVR = OFF

    #include<p18f452.h>
    #include<usart.h>
    #include<delays.h>
    #define SCL PORTCbits.RC3
    #define SDA PORTCbits.RC4

    int Transmit(unsigned char data);
    void Transmit_gui(void);
    int Start(void);
    void Stop(void);
    void Read24c64B(unsigned char RomAddress);
    void Read_eeprom(unsigned int e_add);
    void Ack(void);
    void NoAck(void);
    void init();
    void hex_ascii(unsigned char var);
    void display_USART(unsigned char disp);
    void New_Write_eeprom(unsigned char d);
    unsigned char Read8Bit(void);
    void delay(unsigned int itime);

    unsigned char buf[10] = {1,2,3,4,5,6,7,8,9,10};
    unsigned char a,d,indic,j,ReadData,s=0;
    unsigned int eeprom_data,data_msb,data_lsb,ia,Gui[16],Gui_Inc,i,k,Gui_Buffer,Gui_Buff[10],Msb,Lsb,Var;
    unsigned int device_add,Dt,Data;
    unsigned int lsb_add=10;
    unsigned int add_msb = 0,add_lsb = 0,x,b,c, AS_TEMP1,AS_TEMP2,var;


    void main()
    {
    TRISCbits.TRISC3=0;
    TRISCbits.TRISC7=1;
    OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE& USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 25 );
    Delay10KTCYx(100);
    while(1)
    {
    New_Write_eeprom();
    delay(10);

    Read24c64B(s);
    delay(10);
    s++;
    display_USART(ReadData);
    putcUSART(' ');
    }

    }

    int Transmit(unsigned char data)
    {
    unsigned char b=0,c=0;
    unsigned int a;
    a = 0x80;
    for(b=0;b<8;b++)
    { c = 0;
    c = data & a;
    if(c==0)
    SDA=0;
    else
    SDA=1;
    SCL=1;
    for(d=0;d<8;d++);
    SCL=0;
    a = a>>1;
    }
    SCL=0;
    return 0;
    }



    void New_Write_eeprom(unsigned char d)
    {
    Start();

    Transmit(0xA0);
    Ack();

    Transmit(0x00);
    Ack();

    Transmit(0x00);
    Ack();

    Transmit('d');
    Ack();

    Stop();
    }

    void Read_eeprom(unsigned int e_add)
    {
    lsb_add = e_add & 0xFF;
    device_add = (e_add / (0x100));
    Read24c64B(lsb_add);
    }
    void Read24c64B(unsigned char RomAddress)
    {
    Start();

    Transmit(0xA0);
    Ack();

    Transmit(0x00);
    Ack();

    Transmit(RomAddress);
    Ack();

    Start();
    Transmit(0xA1);
    Ack();

    ReadData=Read8Bit();
    NoAck();

    Stop();
    }

    int Start(void)
    {
    SDA=1;
    SCL=1;
    for(a=0; a<10;a++);
    SDA=0;
    for(a=0; a<10;a++);
    SCL=0;
    return 0;
    }

    void Stop(void)
    {
    SDA=0;
    SCL=1;
    for(a=0; a<50;a++);
    SDA=1;
    for(a=0; a<50;a++);
    }

    void Ack(void)
    {
    TRISC |= 0x10;
    SCL=1;
    if(SDA)
    indic=1;
    else
    indic=0;
    SCL=0;
    TRISC &= 0xEF;
    }

    void NoAck(void)
    {
    SDA=1;
    SCL=1;
    for(a=0; a<10;a++);
    }

    unsigned char Read8Bit(void)
    {
    unsigned char i,rbyte=0;
    SDA=1;
    TRISC |= 0x10; //setting SDA as input mode
    for(i=1;i<=8;i++)
    {
    SCL=1;
    rbyte<<=1;
    if(SDA)
    rbyte|=0x01;
    for(d=0;d<10;d++); //100
    SCL=0;
    }
    TRISC &= 0xEF; //setting SDA back to output mode
    return(rbyte);
    }

    void hex_ascii(unsigned char var)
    {

    AS_TEMP1 = var & 0x0F;
    AS_TEMP1=(AS_TEMP1>=0xa?AS_TEMP1+0x37:AS_TEMP1+0x30);
    AS_TEMP2=(var & 0xF0)>>4;
    AS_TEMP2=(AS_TEMP2>=0xa?AS_TEMP2+0x37:AS_TEMP2+0x30);
    }
    void display_USART(unsigned char disp)
    {

    hex_ascii(disp);
    delay(10);
    putcUSART(AS_TEMP2);
    delay(10);
    putcUSART(AS_TEMP1);
    delay(10);
    AS_TEMP2=AS_TEMP1=0;

    }

    void delay(unsigned int itime)
    {
    int i,j;
    for(i=0;i<itime;i++)
    for(j=0;j<2;j++);
    }
     
  2. Militoy

    Militoy

    180
    0
    Aug 24, 2010
    I’m not an expert programmer – and I haven’t taken time to look through your code (additional comments might make it easier) – but I2C functions typically employ a small delay routine in between SDA and SCL changes to give a clear sequence on the I2C bus – usually just a simple subroutine call and return as:

    Void i2c_dly (void) // i2c bus clear delay
    {
    }

    So a start bit sequence would look something like:

    Int Start (void)
    {
    SDA = 1; // i2c start bit sequence
    i2c_dly() ;
    SCL = 1;
    For(a=0 ; a<10; a++);
    i2c_dly() ;
    SDA = 0;
    For(a=0 ; a<10; a++);
    i2c_dly() ;
    SCL = 0;
    Return 0;
    }

    If you’re having setup time errors you can try clearing the bus between changes – and see if your simulation improves. If not – nothing lost.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-