1. 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.
    Dismiss Notice

I2c with serial eeprom...

Discussion in 'Microcontrollers' started by koolguy, Aug 21, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    I am using atmlu020 EEPROM with AD0 - AD2 to GND
    and testing whether the send data is matched from reading data at same addr by switching LED On Pin...

    so, here is the code don't know why the if statement looping cont and led is blinking??


    Code (text):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
     
    #define SDATA RC4
    #define SCLK RC3
    void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
    void i2c_Wait(void);
    unsigned char i2c_Read(unsigned char ack);

    void main(void){
     
        TRISC3=1; //direction to input have be changed
        TRISC4=1;
        unsigned char ch,c;
    TRISC5=0;
    TRISD2=0;
    __delay_ms(200);
    RD2=1;

    c=0xff;
    while(1){

       I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100000);//command 1010 and 000 and 0 for write
        I2C_write(0X10);// addr at location
        I2C_write(0XFF);//data
       I2cSTOP();


     I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100001);//command 1010 and 000 and 0 for read
    I2C_write(0X10);// addr
        ch= i2c_Read(0); //read data
       I2cSTOP();
    if(ch&&0X0F){ // comparing data with ch

    RD2=0;
    __delay_ms(400);
    RD2=1;
    __delay_ms(800);
    }
    else{

    RD2=0;
    }
    ch=0;
    }

    }
     
     
     
     
    void I2C_init(void)
        {
      SSPCON = 0x38;      // set I2C master mode
     SSPCON2 = 0x00;
    SSPADD = 0x0C;  //400KHZ  20MHz xtal
    SSPSTAT|=0X80;
     PSPIF=0;      // clear SSPIF interrupt flag
     BCLIF=0;      // clear bus collision flag
    }
     
     
     
    void I2C_start(void)
    {
        i2c_Wait();
        SEN=1;
    }
     
     void I2C_write(char x){
        i2c_Wait();
    SSPBUF=x;
     
     
     
            }
     
    void i2c_Wait(void){
        while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
    }
     
    void I2cSTOP(void)
    {
        i2c_Wait();
        PEN=1;
    }

    unsigned char i2c_Read(unsigned char ack)
    {
        // Read data from slave
        // ack should be 1 if there is going to be more data read
        // ack should be 0 if this is the last byte of data read
        unsigned char i2cReadData;

        i2c_Wait();
        RCEN=1;
        i2c_Wait();
        i2cReadData = SSPBUF;
        i2c_Wait();
        if ( ack ) ACKDT=0;         // Ack
        else       ACKDT=1;         // NAck
        ACKEN=1;                    // send acknowledge sequence

        return( i2cReadData );
    }
     
     
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Maybe because the if-statement is inside infinite loop..
     
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    How, i think its fine...
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    You know the basic structure of a microcontroller program:
    Code (C):

    void main(void)
    {
        /* Initializations etc. that are executed once on startup */
        /* .... */

        /* Infinite loop that keeps repeating and keeps the microcontroller "going" */
        while(1){
            /* Whatever you put in here keeps repeating */
        }
    }
     
    You have the if-statement inside the while(1) loop.. therefore the code keeps repeating.
     
    Last edited: Aug 21, 2013
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Ok, i have edited ...
    but LED glow than off...

    Code (text):


    void main(void){
        TRISC3=1; //direction to input have be changed
        TRISC4=1;
        unsigned char ch,c;
    TRISC5=0;
    TRISD2=0;
    __delay_ms(200);
    RD2=1;
    c=0xff;
       I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100000);//esc default addr
        I2C_write(0X10);//esc default addr
        I2C_write(0XFF);//esc default addr
       I2cSTOP();
     I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100001);//esc default addr
    I2C_write(0X10);//esc default addr
        ch= i2c_Read(0);
       I2cSTOP();
    while(1){
    if(ch&&0XFF){
    RD2=0;
    __delay_ms(400);
    RD2=1;
    __delay_ms(800);
    }
    if(!ch&&0XFF){
    RD2=0; }
    ch=0;


    }
    }
     
  7. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    What is it supposed to do?

    Of course the LED turns off when you turn it off with this piece of code at the end of your while(1) loop:

    Code (C):

    while(1) {
        if(!ch&&0XFF){
            RD2=0;
        }
        ch=0;
    }
     
    if you set ch=0, then the first if-check fails and LED is turned off by the second if-statement.. or that is what I assume RD2=0; does.
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Why don't you test the data simply like this

    Code (C):

    while(1) {

        // write data "0xFF"

        // read data back to variable ch

        if(ch == 0xFF){
            RD2=1;  // LED ON
        } else {
            RD2=0;  // LED OFF
        }
    }
     
    Actually your first code is very close to that.. I just removed couple of unnecessary lines that made the LED blink.

    And instead of using 0xFF as test data, I would use 0x55.. which is a lot better because it has both zeros and ones.
     
    Last edited: Aug 21, 2013
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    Ritesh!! Can you please try and indent your code so we can read it

    Like this

    Code (C):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
     
    #define SDATA RC4
    #define SCLK RC3
    void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);  
    void i2c_Wait(void);
    unsigned char i2c_Read(unsigned char ack);
     
    void main(void)
        {
        TRISC3=1; //direction to input have be changed
        TRISC4=1;
        unsigned char ch,c;
        TRISC5=0;
        TRISD2=0;
        __delay_ms(200);
        RD2=1;

        c=0xff;
        while(1)
            {

            I2C_init();
            __delay_ms(50);
            I2C_start();
            I2C_write(0b10100000);//command 1010 and 000 and 0 for write
            I2C_write(0X10);// addr at location
            I2C_write(0XFF);//data
            I2cSTOP();
     
            I2C_init();
            __delay_ms(50);
            I2C_start();
            I2C_write(0b10100001);//command 1010 and 000 and 0 for read
            I2C_write(0X10);// addr
            ch= i2c_Read(0); //read data
            I2cSTOP();
            if(ch&&0X0F)
                { // comparing data with ch
                RD2=0;
                __delay_ms(400);
                RD2=1;
                __delay_ms(800);
                }
            else
                {
                RD2=0;
                }
            ch=0;
            }
     
        }
     
    void I2C_init(void)
        {
        SSPCON = 0x38;  // set I2C master mode
        SSPCON2 = 0x00;
        SSPADD = 0x0C;  //400KHZ  20MHz xtal
        SSPSTAT|=0X80;
        PSPIF=0;        // clear SSPIF interrupt flag
        BCLIF=0;        // clear bus collision flag
        }
     
     
     
    void I2C_start(void)
        {
        i2c_Wait();
        SEN=1;
        }
     
    void I2C_write(char x)
        {
        i2c_Wait();
        SSPBUF=x;
        }
     
    void i2c_Wait(void)
        {
        while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
        }
     
    void I2cSTOP(void)
        {
        i2c_Wait();
        PEN=1;
        }
     
    unsigned char i2c_Read(unsigned char ack)
        {
        // Read data from slave
        // ack should be 1 if there is going to be more data read
        // ack should be 0 if this is the last byte of data read
        unsigned char i2cReadData;
     
        i2c_Wait();
        RCEN=1;
        i2c_Wait();
        i2cReadData = SSPBUF;
        i2c_Wait();
        if ( ack ) ACKDT=0;         // Ack
        else       ACKDT=1;         // NAck
        ACKEN=1;                    // send acknowledge sequence
     
        return( i2cReadData );
    }

     
     
    • Like Like x 1
  10. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,
    It is working fine but i think write and read operation is changing bit position working in 0Xff but not in other condition

    Code (text):
    void main(void){
        TRISC3=1; //direction to input have be changed
        TRISC4=1;
        unsigned char ch,c;
    TRISC5=0;
    TRISD2=0;
    __delay_ms(200);
    RD2=1;
    __delay_ms(400);
    RD2=1;

       I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100000);// ad0 to ad3 gnd  write operation as per manual
        I2C_write(0X10);//addr eeprom
        I2C_write(0xff);//data work in 0xff but not in other like 0x10 etc
       I2cSTOP();
     I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100001);//ad0 to ad3 gnd  read operation as per manual
    I2C_write(0X10);//addr
        ch= i2c_Read(0);//data
       I2cSTOP();
    while(1){
      if(ch == 0xff){
            RD2=1;  // LED ON
    __delay_ms(400);
            RD2=0;  // LED ON
    __delay_ms(400);
        }


    else {
            RD2=0;  // LED OFF
        }
    }
    }
     
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    how to do this color full
     
  12. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    use:

    CODE=C

    instead of CODE and you'll get color coded code.
     
    Last edited: Aug 21, 2013
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    When you change the test data, did you also remember to change the if-statement to test for this new data?

    Code (C):

    // If you change this data..
    I2C_write(0x55);//data work in 0xff but not in other like 0x10 etc
     

    // You also need to change this test..
    if(ch == 0x55)

     
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    First make the tabs correct with your text editor... Then when you wrap your code in "code tags" ( # ) you then insert "=C" within the
    Code (text):
     [/COLOR]brackets after the word code.  like this..

    [COLOR="#0000CD"] [CODE=C] [/COLOR] then the rest..
     
  15. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I could not find that eeprom.. is it one of the Microchips I2C eeproms? The link goes to google search results and there is no chip named atmlu020.

    EDIT: Ok, it must be one of the Atmel AT24Cxxx eeproms (atml=atmel.. d'oh!). Am I right?
     
    Last edited: Aug 21, 2013
  16. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Yes i have changed don't you think while reading the MSB and LSB changed there position something like that..?


    Code (C):
    void main(void){
        TRISC3=1; //direction to input have be changed
        TRISC4=1;
        unsigned char ch,c;
    TRISC5=0;
    TRISD2=0;
    __delay_ms(200);
    RD2=1;
    __delay_ms(400);
    RD2=1;
     
       I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100000);// ad0 to ad3 gnd  write operation as per manual
        I2C_write(0X10);//addr eeprom
        I2C_write(0xff);//data work in 0xff but not in other like 0x10 etc
       I2cSTOP();
     I2C_init();
    __delay_ms(50);
        I2C_start();
        I2C_write(0b10100001);//ad0 to ad3 gnd  read operation as per manual
    I2C_write(0X10);//addr
        ch= i2c_Read(0);//data
       I2cSTOP();
    while(1){
      if(ch == 0xff){
            RD2=1;  // LED ON
    __delay_ms(400);
            RD2=0;  // LED ON
    __delay_ms(400);
        }
     
     
    else {
            RD2=0;  // LED OFF
        }
    }
    }
    Yes it is from Microchip......no idea about serial number something different is written on it..!!
     
  17. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Just like I suspected.. Ritesh, read my post #12.

    When you change the data you write in, you also have to change the if-statement to reflect that change.

    EDIT: sorry, misread the latest code. It looks ok.. but anyway..
     
  18. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Yes i have done that changes in if else statement too...
     
  19. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    So the memory test fails?
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Then what should be done i mean is there any problem in code??
    and i found this chip for voice recording its cool aPR33A1/aPR33A2/aPR33A3
     
  21. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    If the memory read/write fails, then the problem might be in code or in hardware. Check the hardware and check from the EEPROM datasheet the details how to communicate with the chip. I2C devices are all a little bit different.. there is almost always something "unexpected" that you have to do..
     

Share This Page