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.

[参考译文] MSP432E401Y:I2C 读取和写入函数

Guru**** 2535150 points
Other Parts Discussed in Thread: MSP432E401Y, ENERGIA

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

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1237337/msp432e401y-i2c-read-and-write-functions

器件型号:MSP432E401Y
Thread 中讨论的其他器件: Energia

尊敬的论坛:

我曾尝试在 MSP432E401Y 中学习 I2C 传输、现在需要传输16位数据

此致、

TeX  

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

    您好!

     我想在这里 帖子 您能够使用 driverlib 来开发您的 I2C 应用。 您想要切换到 Energia 的原因是什么? 我们没有 Energia 的相关经验。  请参阅  常见问题 TM4C129页面、与 MSP432E 相同、我们在 e2e 上不支持 Energia、  如果您 坚持 为您的应用使用 Energia、您可以从 forum.43oh.com/.../获取帮助。 无论使用哪种方法(SDK 或 Energia 中的 driverlib)、您都需要最好使用逻辑分析仪来捕获发送到外部传感器的命令序列。 您是否符合传感器数据表中规格的时序和命令序列? 如果您认为您的命令序列正确、但传感器未返回正确的数据、则需要咨询传感器供应商。 如果传感器是 TI 器件、请打开指定相应传感器器件型号的新螺纹。 向他们展示您的 I2C 命令序列的完整捕获、让他们提供有关其为何不起作用的提示。  

     您也可以在 Google 上搜索如何将 Wire.write 用于 I2C。

    https://docs.particle.io/reference/device-os/api/wire-i2c/write/

     https://energia.nu/guide/libraries/wire/wire_write/

      似乎对于多字节写入、您需要使用 Wire.write (data、length)语法、而不是 Wire.write (value)、因为它在您的中会执行单个字节。 在任何情况下、您都应咨询 https://forum.43oh.com/forum/119-energia/、

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

    感谢您的回复。

    我实际上是在寻找 driverlib 程序、我忘了提及、请尝试提供答案。

    此致

    TeX

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

    您好!

    我实际上是在寻找 driverlib 程序

     我不明白。 我不确定您是否与我在这篇文章 https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1233558/msp432e401y-i2c-read-register-data-driverlib-example/4666405?tisearch=e2e-sitesearch&keymatch=%2520user%253A563145#4666405中提供帮助的人是同一个人。有时、我会发现同一家公司的多名员工共享同一个帐户。  

     在那篇文章中、显示的所有代码都是 driverlib 函数、我想您已经使其最终工作了。 我还展示了在哪里可以找到所有 I2C 示例。 我会在这里再次将我的答案粘贴到那个帖子上。  

    有大量的 I2C 示例可供您参考。 首先、安装 适用于 MSP432E 的 SimpleLink SDK 、然后将以下目录中的这些工程导入 CCS。

    您可以从 CCS 内的 Resource Explorer 导入相同的项目。  

    您可以在以下位置找到所有 driverlib API:

    https://software-dl.ti.com/simplelink/esd/simplelink_msp432e4_sdk/3.10.00.11/docs/driverlib/msp432e4/html/

    尽管下面的这个示例(读取湿度传感器)适用于 TM4C129 MCU、但它与共享相同 driverlib 的 MSP432E MCU 是相同的器件。 以下是示例代码、您也可以查看。  

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_ints.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>Humidity Measurement with the SHT21 (humidity_sht21_simple)</h1>
    //!
    //! This example demonstrates the usage of the I2C interface to obtain the
    //! temperature and relative humidity of the environment using the Sensirion
    //! SHT21 sensor.
    //!
    //! The I2C7 on the EK-TM4C1294XL launchPad is used to interface with the
    //! SHT21 sensor.  The SHT21 sensor is on the BOOSTXL_SENSHUB boosterPack
    //! expansion board that can be directly plugged into the Booster pack 1
    //! connector of the EK-TM4C1294XL launchPad board.  Please make sure proper
    //! pull-up resistors are on the I2C SCL and SDA buses.
    //!
    //! This example uses the following peripherals and I/O signals.  You must
    //! review these and change as needed for your own board:
    //! - I2C7 peripheral
    //! - GPIO Port D peripheral
    //! - I2C7_SCL - PD0
    //! - I2C7_SDA - PD1
    //!
    //! UART0, connected to the Virtual Serial Port and running at 115,200, 8-N-1,
    //! is used to display messages from this application.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // Define SHT21 I2C Address.
    //
    //*****************************************************************************
    #define SHT21_I2C_ADDRESS  0x40
    
    //*****************************************************************************
    //
    // Define SHT21 Commands.  Please refer to the SHT21 datasheet for
    // all available commands.
    //
    //*****************************************************************************
    #define SW_RESET                0xFE
    #define TRIGGER_RH_MEASUREMENT  0xF5
    #define TRIGGER_T_MEASUREMENT   0xF3
    
    //*****************************************************************************
    //
    // The variable g_ui32SysClock contains the system clock frequency in Hz.
    //
    //*****************************************************************************
    uint32_t g_ui32SysClock;
    
    //*****************************************************************************
    //
    // Application function to capture ASSERT failures and other debug conditions.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    //*****************************************************************************
    //
    // Configure the UART and its pins.  This must be called before UARTprintf().
    //
    //*****************************************************************************
    void
    ConfigureUART(void)
    {
        //
        // Enable the GPIO Peripheral used by the UART.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    
        //
        // Enable UART0.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
        //
        // Configure GPIO Pins for UART mode.
        //
        MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
        MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
        MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
        //
        // Initialize the UART for console I/O.
        //
        UARTStdioConfig(0, 115200, g_ui32SysClock);
    }
    
    //*****************************************************************************
    //
    // This function sends the specified command to the I2C slave device.
    //
    //*****************************************************************************
    void
    I2CWriteCommand(uint32_t ui32Command)
    {
        //
        // Set up the slave address with write transaction.
        //
        MAP_I2CMasterSlaveAddrSet(I2C7_BASE, SHT21_I2C_ADDRESS, false);
    
        //
        // Store the command data in I2C data register.
        //
        MAP_I2CMasterDataPut(I2C7_BASE, ui32Command);
    
        //
        // Start the I2C transaction.
        //
        MAP_I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    
        //
        // Wait until the I2C transaction is complete.
        //
        while(MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
    }
    
    //*****************************************************************************
    //
    // This function will read three 8-bit data from the I2C slave. The first
    // two 8-bit data forms the humidity data while the last 8-bit data is the
    // checksum.  This function illustrates three different I2C burst mode
    // commands to read the I2C slave device.
    //
    //*****************************************************************************
    void
    I2CReadCommand(uint32_t * pui32DataRx)
    {
        //
        // Modify the data direction to true, so that seeing the address will
        // indicate that the I2C Master is initiating a read from the slave.
        //
        MAP_I2CMasterSlaveAddrSet(I2C7_BASE, SHT21_I2C_ADDRESS, true);
    
        //
        // Setup for first read.  Use I2C_MASTER_CMD_BURST_RECEIVE_START
        // to start a burst mode read.  The I2C master continues to own
        // the bus at the end of this transaction.
        //
        MAP_I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    
        //
        // Wait until master module is done transferring.
        // The I2C module has a delay in setting the Busy flag in the register so
        // there needs to be a delay before checking the Busy bit.  The below loops
        // wait until the Busy flag is set, and then wait until it is cleared to
        // indicate that the transaction is complete.  This can take up to 633 CPU
        // cycles @ 100 kbit I2C Baud Rate and 120 MHz System Clock.  Therefore, a
        // while loop is used instead of SysCtlDelay.
        //
        while(!MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
        while(MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
    
        //
        // Read the first byte data from the slave.
        //
        pui32DataRx[0] = MAP_I2CMasterDataGet(I2C7_BASE);
    
        //
        // Setup for the second read.  Use I2C_MASTER_CMD_BURST_RECEIVE_CONT
        // to continue the burst mode read.  The I2C master continues to own
        // the bus at the end of this transaction.
        //
        MAP_I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    
        //
        // Wait until master module is done transferring.
        //
        while(!MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
        while(MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
    
        //
        // Read the second byte data from the slave.
        //
        pui32DataRx[1] = MAP_I2CMasterDataGet(I2C7_BASE);
    
        //
        // Setup for the third read.  Use I2C_MASTER_CMD_BURST_RECEIVE_FINISH
        // to terminate the I2C transaction.  At the end of this transaction,
        // the STOP bit will be issued and the I2C bus is returned to the
        // Idle state.
        //
        MAP_I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    
        //
        // Wait until master module is done transferring.
        //
        while(!MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
        while(MAP_I2CMasterBusy(I2C7_BASE))
        {
        }
        
        //
        // Note the third 8-bit data is the checksum byte.  It will be
        // left to the users as an exercise if they want to verify if the
        // checksum is correct.
        pui32DataRx[2] = MAP_I2CMasterDataGet(I2C7_BASE);
    }
    
    //*****************************************************************************
    //
    // Main 'C' Language entry point.
    //
    //*****************************************************************************
    int
    main(void)
    {
        float fTemperature, fHumidity;
        int32_t i32IntegerPart;
        int32_t i32FractionPart;
        uint32_t pui32DataRx[3] = {0};
    
        //
        // Run from the PLL at 120 MHz.
        // Note: SYSCTL_CFG_VCO_240 is a new setting provided in TivaWare 2.2.x and
        // later to better reflect the actual VCO speed due to SYSCTL#22.
        //
        g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                 SYSCTL_OSC_MAIN |
                                                 SYSCTL_USE_PLL |
                                                 SYSCTL_CFG_VCO_240), 120000000);
    
        //
        // Initialize the UART.
        //
        ConfigureUART();
    
        //
        // Print the welcome message to the terminal.
        //
        UARTprintf("\033[2JSHT21 Example\n");
    
        //
        // The I2C7 peripheral must be enabled before use.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C7);
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    
        //
        // Configure the pin muxing for I2C7 functions on port D0 and D1.
        // This step is not necessary if your part does not support pin muxing.
        //
        MAP_GPIOPinConfigure(GPIO_PD0_I2C7SCL);
        MAP_GPIOPinConfigure(GPIO_PD1_I2C7SDA);
    
        //
        // Select the I2C function for these pins.  This function will also
        // configure the GPIO pins pins for I2C operation, setting them to
        // open-drain operation with weak pull-ups.  Consult the data sheet
        // to see which functions are allocated per pin.
        //
        MAP_GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
        MAP_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
    
        //
        // Enable interrupts to the processor.
        //
        MAP_IntMasterEnable();
    
        //
        // Enable and initialize the I2C7 master module.  Use the system clock for
        // the I2C7 module.  The last parameter sets the I2C data transfer rate.
        // If false the data rate is set to 100kbps and if true the data rate will
        // be set to 400kbps.  For this example we will use a data rate of 100kbps.
        //
        MAP_I2CMasterInitExpClk(I2C7_BASE, g_ui32SysClock, false);
    
        //
        // Initialize the SHT21 Humidity and Temperature sensors.
        //
        I2CWriteCommand(SW_RESET);
    
        //
        // Per SHT21 sensor datasheet, wait for at least 15ms before the
        // software reset is complete.  Here we will wait for 20ms.
        //
        MAP_SysCtlDelay(g_ui32SysClock / (50 * 3));
    
        while(1)
        {
            //
            // Write the command to start a humidity measurement.
            //
            I2CWriteCommand(TRIGGER_RH_MEASUREMENT);
    
            //
            // Per SHT21 sensor datasheet, the humidity measurement
            // can take a maximum of 29ms to complete at 12-bit
            // resolution.  Here we will wait for 33ms.
            //
            MAP_SysCtlDelay(g_ui32SysClock / (30 * 3));
    
            //
            // Read the humidity measurements.
            //
            I2CReadCommand(&pui32DataRx[0]);
    
            //
            // Process the raw measurement and convert it to physical
            // humidity percentage.  Refer to the SHT21 datasheet for the
            // conversion formula.
            //
            pui32DataRx[0] = ((pui32DataRx[0] << 8) + (pui32DataRx[1] & 0xFC));
            fHumidity = (float)(-6 + 125 * (float)pui32DataRx[0] / 65536);
    
            //
            // Convert the floats to an integer part and fraction part for easy
            // print.  Humidity is returned as 0.0 to 1.0 so multiply by 100 to get
            // percent humidity.
            //
            i32IntegerPart = (int32_t) fHumidity;
            i32FractionPart = (int32_t) (fHumidity * 1000.0f);
            i32FractionPart = i32FractionPart - (i32IntegerPart * 1000);
            if(i32FractionPart < 0)
            {
                i32FractionPart *= -1;
            }
    
            //
            // Print the humidity value using the integers we just converted.
            //
            UARTprintf("Humidity %3d.%03d\t", i32IntegerPart, i32FractionPart);
    
            //
            // Write the command to start a temperature measurement.
            //
            I2CWriteCommand(TRIGGER_T_MEASUREMENT);
    
            //
            // Per SHT21 sensor datasheet, the temperature measurement
            // can take a maximum of 85ms to complete at 14-bit resolution.
            // Here we will wait for approximately 90ms.
            //
            MAP_SysCtlDelay(g_ui32SysClock / (11 * 3));
    
            //
            // Read the temperature measurements.
            //
            I2CReadCommand(&pui32DataRx[0]);
    
            //
            // Process the raw measurement and convert it to physical
            // temperature.  Refer to the SHT21 datasheet for the
            // conversion formula.
            //
            pui32DataRx[0] = ((pui32DataRx[0] << 8) + (pui32DataRx[1] & 0xFC));
            fTemperature = (float)(-46.85 + 175.72 * (float)pui32DataRx[0]/65536);
    
            //
            // Convert the floats to an integer part and fraction part for easy
            // print.
            //
            i32IntegerPart = (int32_t) fTemperature;
            i32FractionPart = (int32_t) (fTemperature * 1000.0f);
            i32FractionPart = i32FractionPart - (i32IntegerPart * 1000);
            if(i32FractionPart < 0)
            {
                i32FractionPart *= -1;
            }
    
            //
            // Print the temperature as integer and fraction parts.
            //
            UARTprintf("Temperature %3d.%03d\n", i32IntegerPart, i32FractionPart);
    
            //
            // Wait for one second before taking the measurements again.
            //
            MAP_SysCtlDelay(g_ui32SysClock / 3);
        }
    }

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

    尊敬的 Charles:

    是的、 这就是我。

    我的道歉 当我执行代码并添加代码时、

    上面的代码我理解,但不能做的16位数据我自己读取,因为这是我的第一个 MSP 设备后 Arduino ..

    您能给我介绍一下能够传输16位字数据的读取和写入函数吗?

    我的从设备是 MAX17055

    此致、

    TeX

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

    您负责确定第三方从器件的正确写入和读取顺序。 请咨询从站供应商、以提供波形图。 我无法帮您解决这个问题。 每个从器件可能彼此不同。 下面是读取某个从器件的示例。 请再次牢记、您需要找出对您自己的从器件执行读写操作的正确顺序。  

    要从上述示例器件读取存储器位置、您需要:

     1.从写入操作的从器件地址字节开始。 请参阅从机写入地址的第一帧。  

     2.写入与要访问的寄存器地址相等的数据。 请参阅第二帧的存储器地址

     3.重新启动从器件地址字节以进行读取操作。 查看从机读取地址的第三帧

     4.从步骤2中指定的寄存器中读取数据。  有关当前 LSB 的信息、请参阅第四帧。  

     5.如果读取一个字节、则停止读取事务。 如果您正在读取 第二个字节、则只需继续读取。  

    有一个基本的了解。 您可以使用以下示例代码开始步骤3至5、从从器件读取两个字节(16位)。


    I2CReadCommand (uint32_t * pui32DataRx)
    {
    //
    //将数据方向修改为 true,以便查看地址
    //指示 I2C 主器件正在从从器件发起读取。
    //
    MAP_I2CMasterSlaveAddrSet (I2C7_BASE、SHT21_I2C_ADDRESS、TRUE);

    //
    //设置第一次读取。 使用 I2C_MASTER_CMD_BURST_RECEIVE_START
    //开始突发模式读取。 I2C 主设备继续拥有
    //此事务结束时总线。
    //
    MAP_I2CMasterControl (I2C7_BASE、I2C_MASTER_CMD_BURST_RECEIVE_START);

    //
    //等待主模块完成传输。
    // I2C 模块在寄存器中设置 BUSY 标志时有延迟,因此
    //在检查 BUSY 位之前需要有一个延迟。 以下环路
    //等待忙标志被设置,然后等待它被清除
    //表示事务已完成。 这最多可以占用633个 CPU
    //周期@ 100 kbit I2C 波特率和120 MHz 系统时钟。 因此、
    // while 循环被用来代替 SysCtlDelay。
    //
    while (!MAP_I2CMasterBusy (I2C7_BASE))
    {

    while (MAP_I2CMasterBusy (I2C7_BASE)
    {

    //
    //从从器件读取第一个字节的数据。
    //
    Pui32DataRx[0]= MAP_I2CMasterDataGet (I2C7_BASE);

    //
    //设置第三次读取。 使用 I2C_MASTER_CMD_BURST_RECEIVE_FINISH
    //终止 I2C 事务。 在该交易结束时、
    //将发出停止位,I2C 总线返回到
    //空闲状态。
    //
    MAP_I2CMasterControl (I2C7_BASE、I2C_MASTER_CMD_BURST_RECEIVE_FINISH);

    //
    //等待主模块完成传输。
    //
    while (!MAP_I2CMasterBusy (I2C7_BASE))
    {

    while (MAP_I2CMasterBusy (I2C7_BASE)
    {


    //
    // 读取第二个字节。  
    Pui32DataRx[1]= MAP_I2CMasterDataGet (I2C7_BASE);

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

    现在,我完全理解了很多感谢你的友好的信息  

    此致

    TeX