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 读取寄存器数据 driverlib 示例

Guru**** 2527530 points
Other Parts Discussed in Thread: MSP432E401Y

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

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

器件型号:MSP432E401Y

您好!

我对 msp432微控制器非常陌生、

我正在寻找一个可以读取从器件寄存器数据的 I2C driverlib 示例。

此致、

TeX

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

    您好!

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

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

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

    尊敬的 Charles:

    我需要一个特定的函数来读取 i2c 从器件的寄存器数据。  

    您能帮我解决这个问题吗?

    此致、

    TeX

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

    您好!

      具体的函数是什么意思? 所有文档都位于 C:\ti\simplelink_msp432e4_sdk_4_20_00_12\docs 下。 具体而言、对于 TI 驱动程序、您可以访问 C:\ti\simplelink_msp432e4_sdk_4_20_00_12\docs\tidrivers 并启动 tidriversAPI.html。  您将在下方看到。  

    点击 I2C.h 将打开所有 I2C API 的文档以及使用示例。 下面是一个描述如何读取/写入 I2C 数据的片段。  

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

    非常感谢您提供的宝贵信息、Charles、

    我 最近开始使用 msp432e401y Driverlib 进行编程、实施起来非常困难

    是否有任何可以轻松实施的程序  

    此致、

    TeX

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

    尊敬的 Tex:

     首先、我的坏人。 在我的第二次回复中、我要向 您展示基于"TI-driver"的 I2C 代码。 TI-Driver 通常与 RTOS 一起使用。 再次阅读原始文章后、您正在查找 I2C driverlib 示例。 在我的第一次回复中、我给出了 i2c driverlib 示例列表。 i2c driverlib 示例是非基于 RTOS 的示例。 您是否有机会尝试 C:\ti\simplelink_msp432e4_SDK_4_20_00_12\examples\nortos\MSP_EXP432E401Y\driverlib\i2c_mastermode_simple_transfer? 它将执行简单的 I2C 传输。 开始时、这应该是一个简单的示例。  

     下面是为 TM4C129 MCU 开发的另一个示例。 TM4C129与 MSP432E 是相同的器件。  TM4C129的 driverlib 与 MSP432E 相同。 这两个 MCU 的区别在于 TM4C129不使用 SimpleLink SDK 平台、而是使用其 TivaWare SDK 平台。 如果您已经下载 TivaWare、您可以在  C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c1294xl-boostxl-sensub\HUMIDITY_sht21_simple 中找到示例。 我还要在这里附加示例代码。  

    #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、

    我只想使用"simplelink_msp432e4_SDK_4_20_00_12"

    因此、我已经执行了"i2c_mastermode_simple_transfer"

    它是工作良好,但我无法理解程序自定义..

    请求您发送任何可读取外部从器件寄存器地址的示例。

    "寄存器读取"功能在该示例中单独说明。

    此致、

    TeX

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

    T 是工作良好,但我无法理解程序自定义..

    请求您发送任何可读取外部从器件寄存器地址的示例。

    "寄存器读取"功能在该示例中单独说明。

    [/报价]

    我认为  i2c_mastermode_simple_transfer 已经足够简单了、可以用作起点。  

    基本上,代码在 main ()中开始,使用以下代码来启动读取事务。 它为读取留出从器件地址、并通过调用 MAP_I2CMasterControl 来在 I2C 总线上开始事务。 然后、它 在 setI2CState 等于 I2C_MASTER_RX 时循环等待 。    在 I2C ISR 处理程序中读取所有数据后、setI2CState 状态将更改为 I2C_MASTER_IDLE。  

    下面是从 main ()中启动的内容来启动读取。

    /*将从器件地址放置在总线上以进行读取*/
    MAP_I2CMasterSlaveAddrSet (I2C1_base、SLAVE_ADDRESS、TRUE);

    /*启动读取事务*/
    MAP_I2CMasterControl (I2C1_BASE、I2C_MASTER_CMD_BURST_RECEIVE_START);

    /*等待所有字节的接收*/
    while (setI2CState == I2C_MASTER_RX)

    }

    如下代码片段将读取 ISR 中的数据。

    /*接收路径的处理数据中断*/
    if ((setI2CState = I2C_MASTER_RX)&&(dataIndex < I2C_NUM_DATA-2))

    GetData[dataIndex++]= MAP_I2CMasterDataGet (I2C1_BASE);
    MAP_I2CMasterControl (I2C1_BASE、I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    }
    否则为((setI2CState = I2C_MASTER_RX)&&(dataIndex == I2C_NUM_DATA-2))

    GetData[dataIndex++]= MAP_I2CMasterDataGet (I2C1_BASE);
    MAP_I2CMasterControl (I2C1_BASE、I2C_MASTER_CMD_BURST_RECEIVE_FINISH);


    }

    我不知道 您要尝试读取哪种类型的 I2C 器件。 若要读取寄存器、您通常要执行以下序列、但需要阅读 I2C 器件数据表、以了解需要什么时序和确切的顺序。  

     -从写入操作的从地址字节开始。

     -写入与要访问的寄存器地址相等的数据。  

     -停止 I2C 事务。

     -启动从机地址以执行读取操作。

     从在步骤2中指定的寄存器中读取数据。  

     -如果您正在读取一个字节,请停止读取事务。 如果您正在读取多个字节、则只需继续读取。  

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

    非常感谢。

    最后的疑问:

    我替换了从器件地址和"sendData[I2C_NUM_DATA]"中的寄存器地址  

    我无法一次读取4个不同的寄存器、 对于前2个寄存器、我得到的寄存器值剩余的2个显示为0。

    请告诉我我在做什么错误。

    此致、

    TeX

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

    您最好使用逻辑分析仪来查看主器件输出的波形。 主设备是否符合以下读取协议? 如果你只读取2而不是4、这意味着主器件已经发出了一个 NACK、以便在读取字节后停止从器件进一步读取数据。 您需要了解如何对其进行调试。