写入和读取数据到EEPROM 24LC32A

时间:2017-11-29 13:05:47

标签: c eeprom

我一直试图通过I2C协议向EEPROM 23LC32A写入一个字节。我正在使用uvision 5 Keil来编写我的程序。

当我在Termite.exe终端窗口中运行我的代码时,输​​出似乎表明正在执行读取功能。它返回255,这是清零EEPROM的默认值。所以事实是数据的写入没有发生。

我已根据数据表正确配置和编码控制寄存器。什么可能出错?另外请帮我写EEPROM的页面。以下是我的代码:

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();
    }
}

0 个答案:

没有答案