0

I have been trying to write a byte to the EEPROM 23LC32A through I2C protocol. I am using uvision 5 Keil to code my program.

When I run my code in the Termite.exe terminal window, the output seems to indicate that read function is executing. It returns 255, which is the default value of a cleared EEPROM. So the thing is that the writing of the data is not taking place.

I have configured and coded the control registers correctly as per the datasheet. What could have possibly gone wrong? And also please do help with me page writing of EEPROM too. Here is my code below:

void write8eeprom(uint8_t addr, uint8_t reghigh, uint8_t reglow, uint8_t value) 
{
    uint8_t  bytes [3];
    bytes[0] = reghigh;
    bytes[1] = reglow;
    bytes[2] = value;
    bool success = twi_master_transfer(addr, (uint8_t*) &bytes, sizeof(bytes), TWI_ISSUE_STOP);
}

uint8_t read8(uint8_t addr, uint8_t reg)
{
    uint8_t value;
    uint8_t  bytes [1];
    bytes[0] =  reg;
    uint8_t regValue[1] ;

    twi_master_transfer(addr, (uint8_t*) &bytes, sizeof(bytes), TWI_DONT_ISSUE_STOP);
    twi_master_transfer(addr|TWI_READ_BIT, (uint8_t*) &regValue, sizeof(regValue), TWI_ISSUE_STOP);

    value = regValue[0] ;
    return value;
}


void  eeprom_write()
{
    uint8_t value;
    value = 1;
    write8eeprom(160, 0x1000, 0x0001, value);
    printf("Data %d is written\n", value);
}


void eeprom_read()
{
    uint8_t value;
    value = read8(160, 0x00000001);
    printf("The data %d is read\n", value);
}


int main(void)
{
    nrf_drv_clock_lfclk_request();
    timers_init();
    application_timers_start();
    nrf_delay_ms(2000);
    uart_config();
    gpio_config();
    bool tt= twi_master_init    (   )   ;
    nrf_delay_ms(2000);
    printf("\n\r I2C  \n\r");
    eeprom_write();
    nrf_delay_ms(1000);
    eeprom_read();

    for(;;)
    {
        __WFE();
    }
}
m00am
  • 5,910
  • 11
  • 53
  • 69
Ahmed
  • 1
  • 1
  • 2
    Can you please format your code better, and suppress the many unnecessary CRs – Déjà vu Nov 29 '17 at 13:08
  • yes like Ring says please format your code properly – WhatsThePoint Nov 29 '17 at 13:10
  • Formatting does not only concern the code but the question as a whole. Please learn how to format a question. It's easy, but it requires a minimal effort. – Jabberwocky Nov 29 '17 at 13:28
  • Welcome to SO. I took the liberty to format your code a little more so that it can be read more easily. I also rephrased your question so that is is easier to read. – m00am Nov 29 '17 at 13:55
  • Thank You m00am, i am sorry for the messy and unformatted code since I am not much of a coder. So I would like to know what could be done to solve the problem – Ahmed Nov 29 '17 at 19:40

0 Answers0