Jump to content
leetrieu

EEPROM 24C04 interface to a MSP430G2553

Recommended Posts

I'm tired to interface 24C04 with msp430g2553.

post-3-0-88833500-1379855934_thumb.png
Please help me !!
this is my hardware:
post-3-0-40345400-1379855935_thumb.png

This is code in main.c

#include "msp430g2553.h"
 
#include "I2C.c"
 
unsigned char datawrite[2]={2,8};
 
unsigned char dataread[2];
 
unsigned char i,j;
 
void main( void )
 
{
 
  // Stop watchdog timer to prevent time out reset
 
  WDTCTL = WDTPW + WDTHOLD;
 
  i2c_init(0xa0);
 
  i2c_write(0x00,2,datawrite);
 
  while(1)
 
  {
 
    i=dataread[0];
 
    j=dataread[1];
 
  }
 
} 

When i debug it hang on command line: 

while (!(IFG2&UCB0TXIFG)); 

post-3-0-73914700-1379855929_thumb.png

Please guide me !! Thanks all so much !!

EEPROM 24C04.rar

Edited by bluehash
[ADMIN] - Please upload pics to 43oh.com next time

Share this post


Link to post
Share on other sites

P1.6 has an LED on the launchpad. Remove it's jumper.

Thanks for your answer !!
But I sure I removed it, please review my code and tell me what happen with it?? I did used this I2C Library for my RTC BQ32000, it still working but with 24C04 it hang in that command.
while (!(IFG2&UCB0TXIFG)); 

Share this post


Link to post
Share on other sites

You are setting up the USCI i2c controller with 

i2c_init(0xa0);

0xa0 is an 8bit address 0b10100000.

The USCI controller requires a 7 bit address.

 

You need to use

i2c_init(0x50);

0b1010000 and the controller handles the read/write bit. That's for bank 1, bank 2 is at 0x52.

 

Basically, its hanging waiting for an Ack from what I assume it trying to write to i2c device 0b01000000 0x20, which doesn't exist.

Share this post


Link to post
Share on other sites

You are setting up the USCI i2c controller with 

i2c_init(0xa0);

0xa0 is an 8bit address 0b10100000.

The USCI controller requires a 7 bit address.

 

You need to use

i2c_init(0x50);

0b1010000 and the controller handles the read/write bit. That's for bank 1, bank 2 is at 0x52.

 

Basically, its hanging waiting for an Ack from what I assume it trying to write to i2c device 0b01000000 0x20, which doesn't exist.

Thanks you so much !! It working :smile:  but i have a little problem, please help me explain it ?

I using below code to test:

#include "msp430g2553.h"
#include "I2C.c"
unsigned char datawrite[3]={0x3f,0x13,0x21};
unsigned char dataread[4];
unsigned char tmp1,tmp2,tmp3,tmp4;
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  P1DIR |= 0x0f;
  P1SEL |= BIT6 + BIT7;     // Assign I2C pins to USCI_B0 
  P1SEL2|= BIT6 + BIT7;
  i2c_init(0x50);
  i2c_write(0x01,3,datawrite);//write 3 value from datawrite to 0x01,0x02 and 0x03 registers
  __delay_cycles(50000);
  while(1)
  { 
    i2c_read(0x01,4,dataread);//read 3 regiters: 0x01,0x02 and 0x03
    tmp1=dataread[0]; //0x01 -> tmp1
    tmp2=dataread[1]; //0x02 -> tmp2 
    tmp3=dataread[2]; //0x03 -> tmp3
    tmp4=dataread[3]; // read 0x04
    _NOP(); 
  }
}

I don't understand, why it return values don't exactly in first debug?? but in second debug it true  :?

please see below picture to understand my question ! Thanks all !!!

24c04.gif

Share this post


Link to post
Share on other sites

I think thats an error in i2c.c:

     for( i=0;i<=numbyte;i++) 
      { 
        *(poirev+i-1)= UCB0RXBUF; 
        //datarev[i]= *(poirev+i); 
         while (!(IFG2&UCB0RXIFG)); 
          
      } 

poirev is a pointer to your dataread array, numbyte is the number of bytes you want to read (4). It loops 5 times, and the first time it writes to poirev + 0 - 1, or poirev - 1. It's writing to dataread[-1] which is out of bounds, but as a pointer its a valid address, probably the same as datawrite[2].

 

It's also maybe writing to dataread[4] which is also out of bounds, and probably the same memory address as tmp1. (Pointers and memory mapping is not my strong suit).

 

Notice that the first time, when its wrong, Tmp1 = 0x13 and Tmp2 = 0x21, Tmp3 = 0x83, but the second time, those three are shifted down once.

 

I'm not too sure on the USCI code, if it handles ack/nacks on reads, but I don't believe it does that right either.

 

First, try reading datawrite[2] before and after the first i2cread. If it changed values, then its a memory error from writing to the wrong memory address.

 

Then try changing the above code in i2c.c to:

 

for( i=0;i<numbyte;i++) { *(poirev+i)= UCB0RXBUF; //datarev= *(poirev+i); while (!(IFG2&UCB0RXIFG)); }

You are changing the counter from i<=numbyte to i<numbyte (from 0-4 [5 count] to 0-3 [4 count]) and poirev+i - 1 to just poirev+i (from -1 to +3 to 0 to 3)

 

(Or you can just change i=0 to i=1. That will fix the issue as well. (Loops from 1 to 4 [4 count] and poirev+i - 1 is 0 to 3)

     for( i=1;i<=numbyte;i++) 
      { 
        *(poirev+i-1)= UCB0RXBUF; 
        //datarev[i]= *(poirev+i); 
         while (!(IFG2&UCB0RXIFG)); 
          
      } 

Share this post


Link to post
Share on other sites

 

I think thats an error in i2c.c:

     for( i=0;i<=numbyte;i++) 
      { 
        *(poirev+i-1)= UCB0RXBUF; 
        //datarev[i]= *(poirev+i); 
         while (!(IFG2&UCB0RXIFG)); 
          
      } 

poirev is a pointer to your dataread array, numbyte is the number of bytes you want to read (4). It loops 5 times, and the first time it writes to poirev + 0 - 1, or poirev - 1. It's writing to dataread[-1] which is out of bounds, but as a pointer its a valid address, probably the same as datawrite[2].

 

It's also maybe writing to dataread[4] which is also out of bounds, and probably the same memory address as tmp1. (Pointers and memory mapping is not my strong suit).

 

Notice that the first time, when its wrong, Tmp1 = 0x13 and Tmp2 = 0x21, Tmp3 = 0x83, but the second time, those three are shifted down once.

 

I'm not too sure on the USCI code, if it handles ack/nacks on reads, but I don't believe it does that right either.

 

First, try reading datawrite[2] before and after the first i2cread. If it changed values, then its a memory error from writing to the wrong memory address.

 

Then try changing the above code in i2c.c to:

You are changing the counter from i<=numbyte to i<numbyte (from 0-4 [5 count] to 0-3 [4 count]) and poirev+i - 1 to just poirev+i (from -1 to +3 to 0 to 3)

 

(Or you can just change i=0 to i=1. That will fix the issue as well. (Loops from 1 to 4 [4 count] and poirev+i - 1 is 0 to 3)

     for( i=1;i<=numbyte;i++) 
      { 
        *(poirev+i-1)= UCB0RXBUF; 
        //datarev[i]= *(poirev+i); 
         while (!(IFG2&UCB0RXIFG)); 
          
      } 

Thanks you so much !!!

I sloved my problem with "read twice" and now it is really working !!

24c04-ok.gif

 

Today, I have a problem with two I2C devices. There are RTC(BQ3200) and 24C04. I don't known, how do these two devices work together??

Please guide me !!

Thanks all !!

Share this post


Link to post
Share on other sites

Today, I have a problem with two I2C devices. There are RTC(BQ3200) and 24C04. I don't known, how do these two devices work together??

Please guide me !!

Thanks all !!

I suggest you start a new topic for your problems.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...