我正在尝试使用 EK-TM4C123GXL 制作将数据读写到外部 EEPROM 的功能

问题描述 投票:0回答:0

我正在尝试使 TM4C123GXL 读取和写入一个字节到外部 EEPROM 的功能,但它不起作用。我正在尝试遵循 EEPROM

的数据表

enter image description here

enter image description here

但它不起作用。似乎有时我为地址 0 放置的数据存储在地址 1 中,但这可能是错误的,因为我的读取字节函数很可能是错误的。

这是我的代码:

void I2C_setup(){
    RCGCGPIO |= 0x8;//port E clock enable
    RCGCI2C |= 0x8;//I2C module 3 enable
    GPIODEN_PORTD = 0x3;//enable digital for port D0 and D1 => 00000011
    GPIOAFSEL_PORTD |= 0x3;//There is a alternative function for D0 and D1
    GPIOPCTL_PORTD |= 0x33;//function 3 for D0 and D1 which is SCL and SDA
    GPIOODR_PORTD |= 0x2;//drain for D1 00000010
    I2CMCR_3 = 0x10;//this is master
    I2C_set_SCL_peiiod(0.0001);
}

void I2C_set_SCL_peiiod(double SCL_period){
    I2CMTPR_3 = SCL_period * 800000 + 1;
}

void I2Ceeprom_byte_write(char data, int address)
{
    char upper_address = (unsigned)address >> 8;
    char lower_address = (unsigned)address & 0xff;
    // Send start condition
    I2CMSA_3 = 0xA0;//1010 000 0 transmit
    I2CMCS_3 = 0x3;//00011 generate start bit and I2C enable
    I2C_eeprom_wait();


    I2CMDR_3 = upper_address;//send address(1/2)
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMDR_3 = lower_address;//send address(2/2)
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();

    I2CMDR_3 = data;
    I2CMCS_3 = 0x5;//generate stop
    I2C_eeprom_wait();
    I2C_eeprom_bus_wait();
}

int I2Ceeprom_byte_read(int address)
{
    char upper_address = (unsigned)address >> 8;
    char lower_address = (unsigned)address & 0xff;
    int data;

    I2CMSA_3 = 0xA0;
    I2CMCS_3 = 0x3;
    I2C_eeprom_wait();

    I2CMDR_3 = upper_address;
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMDR_3 = lower_address;
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMSA_3 = 0xA1;
    I2CMCS_3 = 7;
    I2C_eeprom_wait();
    I2C_eeprom_bus_wait();
    data = I2CMDR_3;
    return data;
}

void I2C_eeprom_wait(){
    while(I2CMCS_3 & 0x1);
}

void I2C_eeprom_bus_wait(){
    while(I2CMCS_3 & 0x40);
}

如何将一个字节的数据存储到正确的地址?

c embedded i2c eeprom
© www.soinside.com 2019 - 2024. All rights reserved.