This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430FR2673: MSP430FR2673 External EEPROM interface with I2C

Part Number: MSP430FR2673

Hi,

 I am work on MSP430FR2673, I want to strore data and read data from External EEPROM 24LC64 using I2C. I write a code but it's not working. I don't kow why? Please give replay. 

Thank you

Athulya

#include <msp430.h>

#define EEPROM_ADDRESS 0xA0    // EEPROM I2C address
#define EEPROM_SIZE 256        // EEPROM size in bytes

void initI2C() {
    WDTCTL = WDTPW | WDTHOLD;

    // Configure GPIO
    P1OUT &= ~BIT0;                         // Clear P1.0 output latch
    P1DIR |= BIT0;                          // For LED
    P3SEL0 |= BIT2 | BIT6;                  // I2C pins

    // Disable the GPIO power-on default high-impedance mode to activate
    // previously configured port settings
    PM5CTL0 &= ~LOCKLPM5;

    // Configure USCI_B0 for I2C mode
    UCB1CTLW0 |= UCSWRST;                   // Software reset enabled
    UCB1CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C mode, Master mode, sync
    UCB1CTLW1 |= UCASTP_2;                  // Automatic stop generated
                                            // after UCB0TBCNT is reached
    UCB1BRW = 0x78;                       // baudrate = SMCLK / 8
    UCB1TBCNT = 0x0002;                     // number of bytes to be received
    UCB1I2CSA = 0x00A0;                     // Slave address
    UCB1CTL1 &= ~UCSWRST;
    UCB1IE |= UCRXIE | UCNACKIE | UCBCNTIE;
}

void writeByteToEEPROM(unsigned char data, unsigned int address) {
    while (UCB1STATW & UCBBUSY);    // Wait for I2C bus to be idle

    // Set EEPROM write address
    UCB1I2CSA = EEPROM_ADDRESS;

    // Set write mode
    UCB1CTLW0 |= UCTR;

    // Set memory address to write to
    UCB1CTLW0 |= UCTXSTT;           // Send start condition
    while (!(UCB1IFG & UCTXIFG1));  // Wait for start condition to be sent

    // Send memory address (MSB then LSB)
    UCB1TXBUF = (address >> 8) & 0xFF;  // MSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent
    UCB1TXBUF = address & 0xFF;         // LSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent

    // Send data byte
    UCB1TXBUF = data;
    while (!(UCB1IFG & UCTXIFG1));     // Wait for data to be sent

    // Send stop condition
    UCB1CTLW0 |= UCTXSTP;
}

unsigned char readByteFromEEPROM(unsigned int address) {
    unsigned char data;

    while (UCB1STATW & UCBBUSY);    // Wait for I2C bus to be idle

    // Set EEPROM write address
    UCB1I2CSA = EEPROM_ADDRESS;

    // Set write mode
    UCB1CTLW0 |= UCTR;

    // Set memory address to read from
    UCB1CTLW0 |= UCTXSTT;           // Send start condition
    while (!(UCB1IFG & UCTXIFG1));  // Wait for start condition to be sent

    // Send memory address (MSB then LSB)
    UCB1TXBUF = (address >> 8) & 0xFF;  // MSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent
    UCB1TXBUF = address & 0xFF;         // LSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent

    // Set read mode
    UCB1CTLW0 &= ~UCTR;

    // Send repeated start condition
    UCB1CTLW0 |= UCTXSTT;
    while (UCB1CTLW0 & UCTXSTT);    // Wait for repeated start to be sent

    // Receive data byte
    data = UCB1RXBUF;

    // Send stop condition
    UCB1CTLW0 |= UCTXSTP;

    return data;
}

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

    initI2C();

    // Example usage: Write data to EEPROM
    unsigned int address = 0;
    unsigned char data_to_write = 0x55;
    writeByteToEEPROM(data_to_write, address);

    // Example usage: Read data from EEPROM
    unsigned char data_read = readByteFromEEPROM(address);

    while (1) {
        // Do something with the data
    }

    return 0;
}

  • Hi, 

    1. check both MSP and EEPROM is power on.

    2. Try to monitor hardware I2C. Check hardware I2C is work.

    Regards,

    Helic

  • Hi,

    I check SDA and SCL using logic analyzer. But these two lines are Idle. The hardware  is power on.

    Thank uou

    Athulya

  • Hi, 

    It seems no address is sending out via MSP's I2C.

    Please refer to the I2C demo code from SDK.

    ----------------------

    It seems you are using this demo: msp430fr267x_euscib0_i2c_10.c.

    The address of writeByteToEEPROM(unsigned char data, unsigned int address) is slave's address.

    I found you pass a 0 to UCB0I2CSA. Maybe here is wrong.

    I want to confirm that you need to write 0x55 to eeprom's address = 0.

    Try to read some I2C description.

    UCB0I2CSA is the Slave's I2C address, most is from device's datasheet.

    Regards,

    Helic

  • Hi,

    I used EESPROM address os 0xA0, And I try tmo write data 0x55  to 0X00 adress .

    Thank you

    Athulya

  • Hi, 

    Refer to User's Guide: https://www.ti.com/lit/ug/slau445i/slau445i.pdf

    24.4.14 UCBxI2CSA Register

    Here is the description of UCB0->I2CSA (search I2CSA  in UG):

    I2C slave address.
    The I2CSAx bits contain the slave address of the external device 
    to be addressed by the eUSCIx_B module. 
    It is only used in master mode. 
    The address is right justified. 
    In 7-bit slave addressing mode, 
    bit 6 is the MSB and bits 9-7 are ignored. 
    In 10-bit slave addressing mode, 
    bit 9 is the MSB.

    You need to write 0xA0 to this register.

    write data 0x55  to 0X00 adress .

    This should from EEPROM's datasheet.

    -----------------------------

    Other register operation is same as UCBxI2CSA.

    Regards,

    Helic

**Attention** This is a public forum