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.

[参考译文] MSP430FR2673:具有 I2C 的 MSP430FR2673外部 EEPROM 接口

Guru**** 1456440 points
Other Parts Discussed in Thread: MSP430FR2673
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1352452/msp430fr2673-msp430fr2673-external-eeprom-interface-with-i2c

器件型号:MSP430FR2673

您好!

 我从事 MSP430FR2673工作、我想使用 I2C 从外部 EEPROM 24LC64检测数据并读取数据。 我编写了一个代码、但它不起作用。 我不知道为什么? 请重播。  

谢谢!

阿图利亚

#include <msp430.h>

#define EEPROM_ADDRESS 0xA0    // EEPROM I2C address
#define EEPROM_SIZE 256        // EEPROM size in bytes

void initI2C() {
    WDTCTL = WDTPW | WDTHOLD;

    // Configure GPIO
    P1OUT &= ~BIT0;                         // Clear P1.0 output latch
    P1DIR |= BIT0;                          // For LED
    P3SEL0 |= BIT2 | BIT6;                  // I2C pins

    // Disable the GPIO power-on default high-impedance mode to activate
    // previously configured port settings
    PM5CTL0 &= ~LOCKLPM5;

    // Configure USCI_B0 for I2C mode
    UCB1CTLW0 |= UCSWRST;                   // Software reset enabled
    UCB1CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C mode, Master mode, sync
    UCB1CTLW1 |= UCASTP_2;                  // Automatic stop generated
                                            // after UCB0TBCNT is reached
    UCB1BRW = 0x78;                       // baudrate = SMCLK / 8
    UCB1TBCNT = 0x0002;                     // number of bytes to be received
    UCB1I2CSA = 0x00A0;                     // Slave address
    UCB1CTL1 &= ~UCSWRST;
    UCB1IE |= UCRXIE | UCNACKIE | UCBCNTIE;
}

void writeByteToEEPROM(unsigned char data, unsigned int address) {
    while (UCB1STATW & UCBBUSY);    // Wait for I2C bus to be idle

    // Set EEPROM write address
    UCB1I2CSA = EEPROM_ADDRESS;

    // Set write mode
    UCB1CTLW0 |= UCTR;

    // Set memory address to write to
    UCB1CTLW0 |= UCTXSTT;           // Send start condition
    while (!(UCB1IFG & UCTXIFG1));  // Wait for start condition to be sent

    // Send memory address (MSB then LSB)
    UCB1TXBUF = (address >> 8) & 0xFF;  // MSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent
    UCB1TXBUF = address & 0xFF;         // LSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent

    // Send data byte
    UCB1TXBUF = data;
    while (!(UCB1IFG & UCTXIFG1));     // Wait for data to be sent

    // Send stop condition
    UCB1CTLW0 |= UCTXSTP;
}

unsigned char readByteFromEEPROM(unsigned int address) {
    unsigned char data;

    while (UCB1STATW & UCBBUSY);    // Wait for I2C bus to be idle

    // Set EEPROM write address
    UCB1I2CSA = EEPROM_ADDRESS;

    // Set write mode
    UCB1CTLW0 |= UCTR;

    // Set memory address to read from
    UCB1CTLW0 |= UCTXSTT;           // Send start condition
    while (!(UCB1IFG & UCTXIFG1));  // Wait for start condition to be sent

    // Send memory address (MSB then LSB)
    UCB1TXBUF = (address >> 8) & 0xFF;  // MSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent
    UCB1TXBUF = address & 0xFF;         // LSB
    while (!(UCB1IFG & UCTXIFG1));     // Wait for address to be sent

    // Set read mode
    UCB1CTLW0 &= ~UCTR;

    // Send repeated start condition
    UCB1CTLW0 |= UCTXSTT;
    while (UCB1CTLW0 & UCTXSTT);    // Wait for repeated start to be sent

    // Receive data byte
    data = UCB1RXBUF;

    // Send stop condition
    UCB1CTLW0 |= UCTXSTP;

    return data;
}

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

    initI2C();

    // Example usage: Write data to EEPROM
    unsigned int address = 0;
    unsigned char data_to_write = 0x55;
    writeByteToEEPROM(data_to_write, address);

    // Example usage: Read data from EEPROM
    unsigned char data_read = readByteFromEEPROM(address);

    while (1) {
        // Do something with the data
    }

    return 0;
}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、 

    1.检查 MSP 和 EEPROM 是否均已通电。

    2. 尝试监控硬件 I2C。 检查硬件 I2C 是否正常工作。

    此致、

    赫利克

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    我使用逻辑分析仪检查 SDA 和 SCL。 但这两行是空闲的。 硬件 已加电。

    谢谢

    阿图利亚

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、 

    似乎没有通过 MSP 的 I2C 发送地址。

    请参阅 SDK 中的 I2C 演示代码。

    ----------------

    您好像在使用以下演示: msp430fr267x_euscib0_i2c_10.c。

     writeByteToEEPROM 的地址(unsigned char 数据、unsigned int 地址)是从器件的地址。

    我发现您会将0传递到 UCB0I2CSA。 可能这里错了。

    我想确认您是否需要将 0x55写入 EEPROM 的地址= 0。

    尝试阅读 I2C 的一些描述。

    UCB0I2CSA 是从器件的 I2C 地址、大部分来自器件的数据表。

    此致、

    赫利克

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    我使用了 EESPROM 地址 OS 0xA0,并尝试 TMO 写入数据0x55 到0x00地址。

    谢谢!

    阿图利亚

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、 

    请参阅用户指南: https://www.ti.com/lit/ug/slau445i/slau445i.pdf

    24.4.14 UCBxI2CSA 寄存器

    以下是  UCB0->I2CSA ( 在 UG 中搜索 I2CSA)的描述:

    I2C slave address.
    The I2CSAx bits contain the slave address of the external device 
    to be addressed by the eUSCIx_B module. 
    It is only used in master mode. 
    The address is right justified. 
    In 7-bit slave addressing mode, 
    bit 6 is the MSB and bits 9-7 are ignored. 
    In 10-bit slave addressing mode, 
    bit 9 is the MSB.

    您需要将0xA0写入此寄存器。

    将数据0x55写入 0x00地址.

    这应该从 EEPROM 的数据表中获取。

    ----------------

    其他寄存器操作与 UCBxI2CSA 相同。

    此致、

    赫利克