根据数据表

时间:2016-12-14 18:35:47

标签: c arrays buffer spi

我对c语言非常陌生(obj-c,通常很快),我正在使用一些赛普拉斯BLE板。我正试图控制一个简单的LED。

Per the documentation我应该简单地写一个SPI总线来控制LED。

以下是相关部分: enter image description here

所以我写了32位0来开始。然后是RGB阵列。然后是所有1的32位。以下是我目前正在尝试的内容:

static uint8 startFrame[32] = {0,0,0,0};
static uint8 colorFrame[32] = {1, 255, 0, 0};
static uint8 endFrame[32] = {1,1,1,1};


SPI_1_SpiUartPutArray(startFrame, 32);
SPI_1_SpiUartPutArray(colorFrame, 32);
SPI_1_SpiUartPutArray(endFrame,   32);

我的想法是int是8位,因此{1,1,1,1}的大小应该是32。再一次,我是非常新的并且通过这种方式闯入我的方式。非常感谢任何帮助!

SPI_1_SpiUartPutArray的文档:

/*******************************************************************************
* Function Name: SPI_1_SpiUartPutArray
****************************************************************************//**
*
*  Places an array of data into the transmit buffer to be sent.
*  This function is blocking and waits until there is a space available to put
*  all the requested data in the transmit buffer. The array size can be greater
*  than transmit buffer size.
*
* \param wrBuf: pointer to an array of data to be placed in transmit buffer. 
*  The width of the data to be transmitted depends on TX data width selection 
*  (the data bit counting starts from LSB for each array element).
* \param count: number of data elements to be placed in the transmit buffer.
*
* \globalvars
*  SPI_1_txBufferHead - the start index to put data into the 
*  software transmit buffer.
*  SPI_1_txBufferTail - start index to get data from the software
*  transmit buffer.
*
*******************************************************************************/
void SPI_1_SpiUartPutArray(const uint8 wrBuf[], uint32 count)
{
    uint32 i;

    for (i=0u; i < count; i++)
    {
        SPI_1_SpiUartWriteTxData((uint32) wrBuf[i]);
    }
}

我也试过这个:

 static uint8 startFrame[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    static uint8 colorFrame[32] = {1,1,1, 1,1,1,1,1 , 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
    static uint8 endFrame[32] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

和此:

static uint8 startFrame[4] = {0x00, 0x00, 0x00, 0x00};
static uint8 colorFrame[4] = {0xff, 0xff, 0xff, 0xff};
static uint8 endFrame[4]   = {0xff, 0xff, 0xff ,0xff};

SPI_1_SpiUartPutArray(startFrame, 4);
SPI_1_SpiUartPutArray(colorFrame, 4);
SPI_1_SpiUartPutArray(endFrame,   4);

和此:

static uint8 startFrame[4] = {0,0,0,0};
static uint8 colorFrame[4] = {255, <R>, <G>, <B>};
static uint8 endFrame[4] = {255,255,255,255};

SPI_1_SpiUartPutArray(startFrame, 4);
SPI_1_SpiUartPutArray(colorFrame, 4); // multiple of these for each LED
SPI_1_SpiUartPutArray(endFrame, 4);

如果上述设置中的任何一个是正确的,那么它必须是我如何写入SPI的问题。

3 个答案:

答案 0 :(得分:2)

你可能会遇到一些问题。

首先...... SPI通常或多或少是事务性的。与UART(作为一个常见的计数器示例)不同,您不仅可以根据需要随意编写任何内容。您通常需要一次切换一个芯片选择(有时是“从机选择”),“时钟输出”一个字,然后以另一种方式切换芯片选择。如果没有关于SPI_1_SpiUartPutArray()的文档,很难说你是否可以在同一次传输中多次调用它(在芯片选择切换之间)。

编辑:没关系芯片选择的事情。该LED驱动器似乎没有芯片选择/使能线。请注意,以便将来参考,这对于SPI来说是不寻常的。

第二个问题 - 我认为SPI_1_SpiUartPutArray()一次推出BYTES,而不是位。同样,您没有提供该功能的文档,因此很难说。如果我的假设是正确的,你会希望startFramestatic uint8 startFrame[4] = {0xff, 0xff, 0xff, 0xff};,因为一个字节中有8位给你一个32位的帧起始。同样的想法适用于颜色和帧结束。

第三个问题 - 如果我错误地知道SPI函数是如何工作的,那么你并没有完全初始化那些数组。您声明了32个字节的数组,然后只初始化其中的4个。

答案 1 :(得分:2)

你必须将startFrame声明为uint8的4个元素数组,而不是uint8的32个元素,它是256位长,因此如果SPI_1_SpiUartPutArray的第二个参数是要在SPI上写入的字节数,则必须放4(4字节 - 32位),而不是32位(256位)

static uint8 startFrame[4];
SPI_1_SpiUartPutArray(startFrame, 4);

答案 2 :(得分:1)

您需要将数组大小更改为4而不是32. uint8已经有8位,其中4位表示32位。现在除非你使用一些非标准的库,你需要一些数学来说明要设置为1的位和设置为0的那些。例如,将31 = 00001111b放在一个字节中意味着你要设置前4个位为0,最后4位为1.因此,在您的情况下,代码将是:

static uint8 startFrame[4] = {0,0,0,0};
static uint8 colorFrame[4] = {255, <R>, <G>, <B>};
static uint8 endFrame[4] = {255,255,255,255};

SPI_1_SpiUartPutArray(startFrame, 4);
SPI_1_SpiUartPutArray(colorFrame, 4); // multiple of these for each LED
SPI_1_SpiUartPutArray(endFrame, 4);