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.

TM4C123GH6PM:External eeprom (24LC64) uses i2c to communicate,I can't write the current value into the EEPROM.

Part Number: TM4C123GH6PM

code

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/qei.h"
#include "driverlib/sysctl.h"
#include "driverlib/pwm.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "math.h"
#include "driverlib/i2c.h"
#include "driverlib/adc.h"
#include "driverlib/timer.h"

void Init_Clk( void ) ;
void Init_Timer( void ) ;
void Timer0_ISR( void ) ;
void Init_I2C( void ) ;

#define SLAVE_ADDRESS 0x50
#define WRITE_ADDRESS 0x01
#define DATA 0xAB
int a=0;
volatile long Cnt32_1 = 0 ;
int b=0;
void Init_I2C( void )
{
a=1;
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA) ;
GPIOPinConfigure( GPIO_PA6_I2C1SCL);
GPIOPinConfigure( GPIO_PA7_I2C1SDA);
GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
I2CMasterInitExpClk(I2C1_BASE,SysCtlClockGet()/100,false) ;
I2CMasterEnable(I2C1_BASE);
//---------------<Write>-------------------//
a=2;
I2CMasterSlaveAddrSet( I2C1_BASE, SLAVE_ADDRESS, false ) ;
I2CMasterDataPut( I2C1_BASE, 0x00) ;
while(I2CMasterBusBusy(I2C1_BASE)){}
I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START ) ;
while(I2CMasterBusy(I2C1_BASE)){}
a=3;
I2CMasterDataPut( I2C1_BASE,WRITE_ADDRESS);
a=10;
while(I2CMasterBusBusy(I2C1_BASE)){}
I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C1_BASE)){}
a=4;
I2CMasterDataPut( I2C1_BASE, DATA ) ;
while(I2CMasterBusBusy(I2C1_BASE)){}
I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH) ;
while(I2CMasterBusy(I2C1_BASE)) {}
a=5;
//---------------<Read>-------------------//
I2CMasterSlaveAddrSet( I2C1_BASE, SLAVE_ADDRESS, false ) ;

I2CMasterDataPut( I2C1_BASE, 0x00 ) ;
while(I2CMasterBusBusy(I2C1_BASE)){}
I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START ) ;
while(I2CMasterBusy(I2C1_BASE)) {}
a=6;
I2CMasterDataPut( I2C1_BASE, WRITE_ADDRESS) ;
while(I2CMasterBusBusy(I2C1_BASE)){}
I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT ) ;
while(I2CMasterBusy(I2C1_BASE)){}
a=7;
I2CMasterSlaveAddrSet( I2C1_BASE, SLAVE_ADDRESS, true ) ;
while(I2CMasterBusy(I2C1_BASE)){}
while(I2CMasterBusBusy(I2C1_BASE)){}
a=8;
I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE ) ;
while(I2CMasterBusy(I2C1_BASE)){}
// Get Value
Cnt32_1 = I2CMasterDataGet(I2C1_BASE);
}
void main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
Init_I2C( ) ;
while( 1 )
{
}
}

  • Hi,

      I'm not familiar with Microchip 24LC64. But looking at your code I could see one thing wrong. In the Read section you wrote as follows where you specify false. False means it is a write. I suppose you want to read. You should pass true instead. 

    //---------------<Read>-------------------//
    I2CMasterSlaveAddrSet( I2C1_BASE, SLAVE_ADDRESS, false ) ;

      Another thing I notice is that for reads, you start with I2C_MASTER_CMD_BURST_SEND_START and followed by I2C_MASTER_CMD_BURST_SEND_CONT. These are for transmit, not receive. You should use : I2C_MASTER_CMD_BURST_RECEIVE_START, I2C_MASTER_CMD_BURST_RECEIVE_CONT and : I2C_MASTER_CMD_BURST_RECEIVE_FINISH if you want to use burst mode for reads. 

      Please refer to this app note on how to use I2C module. https://www.ti.com/lit/pdf/spma073

      Also make sure you have correct slave address value and correct pullup resistors on SCL and SDA buses. The best way to debug I2C is to use scope or logic analyzer. 

  • I have tried your way without success.
    My idea is to find out the location of eeprom, and then read the data
    But the value I read is 0xFF

  • The best way is to look at the scope capture or even better use a logic analyzer. It should clear show what is being written to the eeprom on the bus and how it is reading compared to what 24LC64 requires.