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.

[参考译文] MSP430FR2355:如何创建未定义/变量数组

Guru**** 2538950 points


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

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1002306/msp430fr2355-how-to-create-an-undefined-variable-array

器件型号:MSP430FR2355

大家好、

我们希望在下面就客户的问题寻求您的帮助。

我正在努力处理中断矢量。 我获得了一个从主器件到从器件 I2C 发送的示例代码。 此代码使用中断服务例程将数据数组发送到发送缓冲区。 为此、必须将数据数组声明为全局数组。

当 i2c 总线上有多个器件时、可能会最终出现多个不同的数组、需要通过数据数组将这些数组通过管道传递到传输缓冲区。

如何创建一个未定义/变量数组、我可以使用该数组将其他大小不同的数组传递到该数组? 这个未定义的数组是 i2c 的 TX 中断矢量的一部分、该矢量将其数据传递到 TX 缓冲区。

我正在寻找一个简单的结构。 如果我将数组大小定义为特定大小、则发送缓冲区将发送所有元素。 当然,我只想向这些元素发送所需的数据。

感谢您的支持!

此致、

Danilo

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

    您好!

    我想客户询问他们是否可以动态调整阵列的大小。 虽然这可能是可行的、但我不推荐它。 在我们 的 I2C 代码示例中、您将看到多个不同大小的数组。 通过这种方式、根据命令、无需动态调整阵列大小即可使用适当的阵列大小。

    //******************************************************************************
    // Example Commands ************************************************************
    //******************************************************************************
    
    #define SLAVE_ADDR  0x48
    
    /* CMD_TYPE_X_SLAVE are example commands the master sends to the slave.
     * The slave will send example SlaveTypeX buffers in response.
     *
     * CMD_TYPE_X_MASTER are example commands the master sends to the slave.
     * The slave will initialize itself to receive MasterTypeX example buffers.
     * */
    
    #define CMD_TYPE_0_SLAVE      0
    #define CMD_TYPE_1_SLAVE      1
    #define CMD_TYPE_2_SLAVE      2
    
    #define CMD_TYPE_0_MASTER      3
    #define CMD_TYPE_1_MASTER      4
    #define CMD_TYPE_2_MASTER      5
    
    #define TYPE_0_LENGTH   1
    #define TYPE_1_LENGTH   2
    #define TYPE_2_LENGTH   6
    
    #define MAX_BUFFER_SIZE     20
    
    /* MasterTypeX are example buffers initialized in the master, they will be
     * sent by the master to the slave.
     * SlaveTypeX are example buffers initialized in the slave, they will be
     * sent by the slave to the master.
     * */
    
    uint8_t MasterType2 [TYPE_2_LENGTH] = {'F', '4', '1', '9', '2', 'B'};
    uint8_t MasterType1 [TYPE_1_LENGTH] = { 8, 9};
    uint8_t MasterType0 [TYPE_0_LENGTH] = { 11};
    
    
    uint8_t SlaveType2 [TYPE_2_LENGTH] = {0};
    uint8_t SlaveType1 [TYPE_1_LENGTH] = {0};
    uint8_t SlaveType0 [TYPE_0_LENGTH] = {0};

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

    我处理这个问题的方法是简单地将一个指针传递到数据字符串及其长度。 然后、I2C 发送例程处理了细节。

    当然可以使用 malloc()和 free()动态调整数组大小,但不建议将其用于嵌入式系统。 除非你非常确信你永远不会耗尽堆空间并且你没有任何泄漏。