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.

MSP430F2232: I2C to 24AA128 EEPROM

Part Number: MSP430F2232
Other Parts Discussed in Thread: MSP430FR5969

Tool/software:

Hello,

I am trying to send data from a MSP430F2232 to a 24AA128 EEPROM using I2C. I have done it with the MSP430FR5969 but want to do it with the MSP430F2232.

I have found out that there is a difference between the two microcontrollers when it comes to programming the I2C code. 

But now I get stuck on something. When I run the code I get stuck in the second "while (!(IFG2 & UCB0TXIFG));" loop in the write function. 

It seems like the 24AA128 doesn't send an ACK back but I am not sure. Can anyone tell me if there is something wrong with my code for the MSP430F2232?

The code is:

#include <msp430.h>

#define EEPROM_ADDR 0x50 // 7-bit I2C adres (A2:A0 = GND)

void i2c_init(void);
void i2c_write_eeprom_byte(unsigned int mem_addr, unsigned char data);
unsigned char i2c_read_eeprom_byte(unsigned int mem_addr);
void delay_ms(unsigned int ms);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD;
DCOCTL = 0;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

i2c_init();

// Schrijf een byte naar EEPROM
i2c_write_eeprom_byte(0x00, 0xAB);
delay_ms(10); // Schrijfvertraging

// Lees hem terug
unsigned char waarde = i2c_read_eeprom_byte(0x00);

// Zet de waarde ergens in debug of LED, afhankelijk van jouw toepassing
// bijv. als (waarde == 0xAB) dan LED aan

while (1);
}

void i2c_init(void)
{
P3SEL |= BIT1 + BIT2; // P3.1 = SDA, P3.2 = SCL
UCB0CTL1 |= UCSWRST; // USCI in reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C master, sync mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // SMCLK, USCI nog in reset
UCB0BR0 = 10; // 100kHz bij 1 MHz SMCLK
UCB0BR1 = 0;
UCB0I2CSA = 0x50; // Slave adres
UCB0CTL1 &= ~UCSWRST; // USCI uit reset
IFG2 &= ~(UCB0TXIFG + UCB0RXIFG); // Clear flags
}

void i2c_write_eeprom_byte(unsigned int mem_addr, unsigned char data)
{
while (UCB0STAT & UCBBUSY); // Wacht tot bus vrij is
UCB0CTL1 |= UCTR + UCTXSTT; // TX mode + START

while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = (mem_addr >> 8) & 0xFF; // Hoog byte adres

while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = mem_addr & 0xFF; // Laag byte adres

while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = data; // Gegevensbyte

while (!(IFG2 & UCB0TXIFG));
UCB0CTL1 |= UCTXSTP; // Stopconditie
while (UCB0CTL1 & UCTXSTP);
}

unsigned char i2c_read_eeprom_byte(unsigned int mem_addr)
{
unsigned char data;
while (UCB0CTL1 & UCTXSTP); // Wacht op vorige STOP

// Stap 1: Geef adres door (write)
UCB0CTL1 |= UCTR + UCTXSTT; // TX mode + START
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = (mem_addr >> 8) & 0xFF; // Hoog byte

while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = mem_addr & 0xFF; // Laag byte

while (!(IFG2 & UCB0TXIFG));
// Stap 2: Herstart + lees
UCB0CTL1 &= ~UCTR; // RX mode
UCB0CTL1 |= UCTXSTT; // Herstart

while (UCB0CTL1 & UCTXSTT); // Wacht tot adres verzonden is
UCB0CTL1 |= UCTXSTP; // Stop na volgende byte

while (!(IFG2 & UCB0RXIFG)); // Wacht op RX
data = UCB0RXBUF;

return data;
}


void delay_ms(unsigned int ms)
{
while (ms--) __delay_cycles(1000); // 1 ms bij 1 MHz
}

**Attention** This is a public forum