2016-12-14 80 views
1

我對c語言非常陌生(obj-c,通常很快),我正在使用一些賽普拉斯BLE板。我試圖控制一個簡單的LED。基於數據表在C中創建數組

the documentation我應該簡單地寫一個SPI總線來控制LED。

下面是相關的部分: enter image description here

所以我寫0的32位開始。然後是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的問題。

+1

你的'static uint8 endFrame [32] = {1,1,1,1};'既不是一個32位元的'1'位數組(只有前4個元素是1,其他元素是0),也不是4個元素的0xFF字節數組(32位)。你似乎只寫一個LED日期[原文如此]字段。請注意,它的「全局」位域可能不能是「11111b」,或者像素爲白色時,項目可能與結束幀標記混淆。 –

+0

你爲什麼改變這個問題?從評論和答案中糾正是沒有用的。 StackOverflow不是一個運行更新服務。 –

+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個。

+0

我非常感謝您的回覆。我已經用put數組方法的文檔更新了我的問題。 – random

+0

我不希望我的'endFrame'是'{0xff,0xff,0xff,0xff};'因爲它必須全爲1嗎?我的任何'startFrame'都是'{0x00,0x00,0x00,0x00}'? – random

+1

是的,你在起始幀上是正確的。對不起,把它弄倒了。此外,通過FYI快速瀏覽,「全局」值通過驅動強度控制設置整體「亮度」,其中R/G/B設置PWM佔空比。不要感到驚訝,如果1)改變亮度*也*改變顏色,並且2)該設備不能完美地映射到RGB值的設置顏色的期望。 –

2

你必須聲明startFrame將UINT8的4元件陣列,而不是32元件UINT8的是256位長,因此如果SPI_1_SpiUartPutArray的第二個參數是字節數對SPI寫,你必須把4(4字節 - 32位),而不是32(它是256位)

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

你需要改變你的數組大小至4,而不是32 UINT8已經具有8位和那些裝置32位中的4。現在,除非使用一些非標準庫,否則需要一點數學來說明要將哪些位設置爲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);