I have been trying to interface the msp430g2553 with the serial EEPROM 24LC512. I found this bit of code which works and I am able to store and receive data, however, the I2C bus is extremely slow with data transfer around 8Hz. The code is as follows:
#include "msp430g2553.h"
//Address word + "HELLO WORLD"
unsigned char txdata[14] = {0x00, 0x00, 0x00, 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x20, 0x57, 0x4F, 0x52, 0x4C, 0x44};
unsigned char rxdata[12];
unsigned char tx_byte_count;
unsigned char rx_byte_count;
unsigned char tx_byte_counter;
unsigned char rx_byte_counter;
unsigned char i;
unsigned char tx_rx;
void i2c_tx(unsigned char tx_count);
void i2c_rx(unsigned char rx_count);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop WDT
//DCOCTL = 0x00; // Set DCOCLK to 16MHz
BCSCTL1 = CALBC1_16MHZ; //Set DCO to 1MHz
DCOCTL = CALDCO_16MHZ;
P1SEL |= BIT6 + BIT7; //Set I2C pins
P1SEL2|= BIT6 + BIT7;
UCB0CTL1 |= UCSWRST; //Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; //Use SMCLK, keep SW reset
UCB0BR0 = 160; //fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x50; //Slave Address
UCB0CTL1 &= ~UCSWRST; //Clear SW reset, resume operation
IE2 |= UCB0TXIE; //Enable TX interrupt
IE2 |= UCB0RXIE; //Enable RX interrupt
//__delay_cycles(20000); //Just a start up delay
//i2c_tx(13); //i2c TX 13 bytes(Address word + "HELLO WORLD"
__delay_cycles(20000); //Allow 24LC256 to write data
i2c_tx(2); //i2c TX address
i2c_rx(12); //i2c RX data
__bis_SR_register(CPUOFF + GIE); //Wait for a reset
}
void i2c_tx(unsigned char tx_count)
{
tx_rx = 0;
tx_byte_count = tx_count + 1;
tx_byte_counter = tx_count; // Load TX byte counter
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
// Remain in LPM0 until all data is TX'd
}
void i2c_rx(unsigned char rx_count)
{
tx_rx = 1;
rx_byte_count = rx_count + 1;
rx_byte_counter = rx_count; // Load RX byte counter
UCB0CTL1 &= ~UCTR; // I2C RX
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
// Remain in LPM0 until all data is RX'd
}
//interrupt(USCIAB0TX_VECTOR) USCIAB0TX_ISR(void)
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void) //For mspgcc
{
if(tx_rx == 0)
{
if (tx_byte_counter > 0) //Check TX byte counter
{
UCB0TXBUF = txdata[tx_byte_count - tx_byte_counter]; // Load TX buffer
tx_byte_counter--; //Decrement TX byte counter
}
else if(tx_byte_counter == 0)
{
UCB0CTL1 |= UCTXSTP; //I2C stop condition
while (UCB0CTL1 & UCTXSTP); //Ensure stop condition got sent
IFG2 &= ~UCB0TXIFG; //Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF+GIE); //Exit LPM0
}
}
else if(tx_rx == 1)
{
if (rx_byte_counter > 0) //Check RX byte counter
{
rxdata[rx_byte_count - rx_byte_counter] = UCB0RXBUF;
rx_byte_counter--; //Decrement RX byte counter
}
else if(rx_byte_counter == 0)
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
rxdata[rx_byte_count - (rx_byte_counter + 1)] = UCB0RXBUF;
rxdata[rx_byte_count - (rx_byte_counter + 1)] = UCB0RXBUF;
IFG2 &= ~UCB0RXIFG; // Clear USCI_B0 RX int flag
__bic_SR_register_on_exit(CPUOFF+GIE); // Exit LPM0
}
}
}
Changing the value of UCB0BR0 and UCBR0BR1 or changing the clock speed does not affect the speed of communication Any help on this would be greatly appreciated.