最近在做一個STM32項目,需要驅動24位WS2812b RGB燈環。曾寫下一篇博文《WS2812b/2811/6812燈珠 – STM32F103 CPU-SPI方式驅動》。原理是:用SPI來模擬WS2812b的驅動信號時序。雖然這個程序能夠正常工作,但也有一些缺點,其中一個最明顯的缺點是——發送數據要佔用大量CPU時間。那麼有沒有一種更好的辦法呢?
這個方法就是用DMA-SPI發送。CPU只需要把要發送的數據計算好,然後直接丟給DMA總線即可。
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __WS2812B_H
#define __WS2812B_H
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Exported types ------------------------------------------------------------*/
typedef struct
{
u8 R;
u8 G;
u8 B;
}RGBColor_TypeDef;
/* Exported constants --------------------------------------------------------*/
#define Pixel_S1_NUM 24 //燈環-1 RGB數量
#define CODE0 0x80 //0xC0不適用
#define CODE1 0xF8 //0xFC不適用
/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void WS2812b_Configuration(void);
/* Basic Color Effects -------------------------------------------------------*/
void RGB_RED(u16 Pixel_LEN);
void RGB_GREEN(u16 Pixel_LEN);
void RGB_BLUE(u16 Pixel_LEN);
void RGB_YELLOW(u16 Pixel_LEN);
void RGB_MAGENTA(u16 Pixel_LEN);
void RGB_BLACK(u16 Pixel_LEN);
void RGB_WHITE(u16 Pixel_LEN);
/* Complicated Color Effects -------------------------------------------------*/
void rainbowCycle(u8 Pixel_LEN);
#endif /* __WS2812B_H */
本程序中MCU的系統時鐘爲72MHz。應用RGB燈效的思路依然是先設定每個燈的顏色,然後用DMA一併發送出去。在打開兩個高等級的平級中斷的情況下,燈環上24位RGB的顏色正常。經過實際測試,110 pixels的燈帶也能完美驅動!
/* Includes ------------------------------------------------------------------*/
#include "WS2812b.h"
/* Private typedef -----------------------------------------------------------*/
/* Some Static Colors --------------------------------------------------------*/
const RGBColor_TypeDef RED = {255,0,0};
const RGBColor_TypeDef GREEN = {0,255,0};
const RGBColor_TypeDef BLUE = {0,0,255};
const RGBColor_TypeDef SKY = {0,255,255};
const RGBColor_TypeDef MAGENTA = {255,0,220};
const RGBColor_TypeDef YELLOW = {127,216,0};
const RGBColor_TypeDef ORANGE = {127,106,0};
const RGBColor_TypeDef BLACK = {0,0,0};
const RGBColor_TypeDef WHITE = {255,255,255};
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 pixelBuffer[ Pixel_S1_NUM][24];
/* Private functions ---------------------------------------------------------*/
/**
* @brief WS2812B SPI總線初始化
* @param
*/
void WS2812b_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);
/* Enable peripheral clocks ------------------------------------------------*/
/* GPIOA,SPI1 and DMA1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Configure SPI1 pin: MOSI ------------------------------------------------*/
/* Confugure MOSI pin as Alternate Function Push Pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; //SPI1-Tx_Only_Master
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //SPI1CLK=9MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
/* Enable SPI1 CRC calculation */
SPI_CalculateCRC(SPI1, DISABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32) &(SPI1 -> DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32) &(*pixelBuffer);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //PeripheralBaseAddr爲目標地址,即從pixelBuffer到SPI1 -> DR
DMA_InitStructure.DMA_BufferSize = ( Pixel_S1_NUM)*24;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
/* RGB RESET ---------------------------------------------------------------*/
Delay_1ms(1);
RGB_BLACK(Pixel_S1_NUM );
Delay_1ms(1);
}
//設定某個RGB LED的顏色
void rgb_SetColor(unsigned char LedId, RGBColor_TypeDef Color)
{
u8 i;
if( LedId > ( Pixel_S1_NUM ) ) return; //to avoid overflow
for(i=0;i<=7;i++) pixelBuffer[LedId][i]= ( (Color.G & (1 << (7 -i)) )? (CODE1):CODE0 );
for(i=8;i<=15;i++) pixelBuffer[LedId][i]= ( (Color.R & (1 << (15-i)) )? (CODE1):CODE0 );
for(i=16;i<=23;i++) pixelBuffer[LedId][i]= ( (Color.B & (1 << (23-i)) )? (CODE1):CODE0 );
}
/**
* @brief Configure colors to RGB pixel series.羣發
* @param RGBColor_TypeDef: pointer to a RGBColor_TypeDef structure that
* contains the color configuration information for the RGB pixel.
* @retval None
*/
void rgb_SendArray(void)
{
DMA_Cmd(DMA1_Channel3, DISABLE );
DMA_ClearFlag(DMA1_FLAG_TC3);
DMA_SetCurrDataCounter(DMA1_Channel3,( Pixel_S1_NUM ) * 24 );
DMA_Cmd(DMA1_Channel3, ENABLE);
}
/* Basic Color Effects -------------------------------------------------------*/
/**
* @brief Configure the whole serial RGB pixels to red.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_RED(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,RED);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to green.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_GREEN(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,GREEN);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to blue.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_BLUE(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,BLUE);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to yellow.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_YELLOW(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,YELLOW);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to magenta.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_MAGENTA(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,MAGENTA);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to black/all-off.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_BLACK(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,BLACK);
rgb_SendArray();
}
/**
* @brief Configure the whole serial RGB pixels to white.
* @param Pixel_LEN: the account of RGB pixels in serial
* @retval None
*/
void RGB_WHITE(u16 Pixel_LEN)
{
u8 i;
for(i=0;i<Pixel_LEN;i++)
rgb_SetColor(i,WHITE);
rgb_SendArray();
}
/* Complicated Color Effects -------------------------------------------------*/
RGBColor_TypeDef Colourful_Wheel(u8 WheelPos)
{
WheelPos = 255 - WheelPos;
RGBColor_TypeDef color;
if(WheelPos < 85)
{
color.R = 255 - WheelPos * 3;
color.G = 0;
color.B = WheelPos * 3;
return color;
}
if(WheelPos < 170)
{
WheelPos -= 85;
color.R = 0;
color.G = WheelPos * 3;
color.B = 255 - WheelPos * 3;
return color;
}
WheelPos -= 170;
color.R = WheelPos * 3;
color.G = 255 - WheelPos * 3;
color.B = 0;
return color;
}
void rainbowCycle(u8 Pixel_LEN)
{
u16 i, j=0;
for( j=0; j<1023; j++) // 1 cycles of all colors on wheel
{
for( i=0; i < Pixel_LEN; i++)
rgb_SetColor(i,Colourful_Wheel(((i * 256 / Pixel_LEN) + j)&255));
rgb_SendArray();
Delay_1ms(50);
}
}