WS2812b幻彩ARGB灯珠的STM32F103的CPU-SPI方式驱动

本文介绍了一种使用 STM32 的 SPI 接口来驱动 WS2812b ARGB 灯环的方法。通过对 SPI 波特率的精确调整,实现了对灯环颜色的精确控制,包括基本颜色效果和复杂颜色效果,如彩虹跑马灯。测试表明,该方法能够稳定驱动多达 110 个像素的灯带。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我到某公司第二天就接到的小项目——驱动WS2812 ARGB灯环。那时候我STM32玩的还不太行,用STM8的水平倒是半斤八两。第一眼看WS2812的时序,我非常头大。然后,我从Github上找到了能用的代码,是STM8的,成功点亮并进行颜色设置。可它的ARGB底层驱动是汇编语言编写的,我不懂如何移植到STM32上。
  驱动WS2812b最重要的就是时序,不论用哪种片上外设,还是用GPIO模拟方式,都是在控制高低电平的持续时间。目前,想要发送数据,除去用GPIO模拟,还有UART、I2C、SPI这些外设:

1、UART的波特率连续可调,但带有低电平的起始位没法忽略;
2、I2C的工作速度不合适;
3、只剩下SPI了,它的波特率分档可调,可以用来发送字节数据,也没有起始位和停止位,空闲时可设置为高电平。
  
  以WS2812b为例,其数据发送速度为800Kbps,每个周期发送一个bit(CODE1或CODE0,前者的占空比比后者高)。如果用SPI发送的byte模拟WS2812的bit,那么,要把SPI的速度设定在800K8=6.4Mbps。但SPI的波特率只能是系统时钟的整数分频,在6.4M附近的SPI波特率只有9M和4.5M。好在根据WS2812b的数据手册,单个bit有很大的时间容差(TH+TL=1.25μs±600ns),当SPI波特率为9M时,换算得到bit的发送周期为18/9M=0.89us,落在了该区间内。因此,只要确定CODE1和CODE0对应的数值即可(代码中有)。

/* 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数量

//用SPI字节 来模拟bit(CODEx)码
#define CODE0 0xC0      	//0x80 | 0xC0
#define CODE1 0xFC      	//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灯效的思路依然是先设定每个灯的颜色,然后通过SPI一起发送。在打开两个高等级的平级中断的情况下,灯环上24颗ARGB的颜色正常。经过实际测试,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 YELLOW   = {255,255,0};
const RGBColor_TypeDef MAGENTA  = {255,0,255};
const RGBColor_TypeDef BLACK    = {0,0,0};
const RGBColor_TypeDef WHITE    = {255,255,255};

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 LedsArray[ ( Pixel_S1_NUM  ) * 3 ];					//[Gloable Variables]

/* Private functions ---------------------------------------------------------*/
/**
  * @brief      WS2812B SPI总线初始化
  * @param 
  */
void WS2812b_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
  
  /* Enable peripheral clocks ------------------------------------------------*/
  /* GPIOA,SPI1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
  
  /* Configure SPI1 pins: MOSI -----------------------------------------------*/
  /* Confugure MOSI pins 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;							//IDLE=High
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  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;								//Default Value
  SPI_Init(SPI1, &SPI_InitStructure);
  
  /* Enable SPI1 CRC calculation */
  SPI_CalculateCRC(SPI1, DISABLE);
  
  /* Enable SPI1 */
  SPI_Cmd(SPI1, ENABLE);
  
  /* RGB RESET ---------------------------------------------------------------*/
  Delay_1ms(1);
  RGB_BLACK(Pixel_S1_NUM  );
  Delay_1ms(1);
}

/**
  * @brief  Transmits one of 24 bits meassages through the SPIx peripheral.
  * @param  Data: where x can be
  *   - =0   CODE0
  *   - >0  CODE1
  * @retval None
  */
void SPI_Send_1bit(volatile u8 Data)
{
  /* Wait for SPI1 Tx buffer empty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  
  /* Send SPI1 data */
  switch(Data)
  {
  case 0:       SPI_I2S_SendData(SPI1, CODE0);  break;
  default:      SPI_I2S_SendData(SPI1, CODE1);  break;
  }
}

/**
  * @brief  Config colors to a RGB pixel.
  * @param  
  * @retval None
  */
void rgb_SetColor(unsigned char LedId, RGBColor_TypeDef Color)  
{
  if(LedId > Pixel_S1_NUM ) return; //to avoid overflow
  //G-->R-->B
  LedsArray[LedId*3]   = Color.G;
  LedsArray[LedId*3+1] = Color.R;
  LedsArray[LedId*3+2] = Color.B;
}

/**
  * @brief  Apply 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)
{
  u8 i;
  s16 j;
  for(i=0;i<(Pixel_S1_NUM);i++)
  {
    for(j=7;j>=0;j--)  SPI_Send_1bit(LedsArray[i*3]  &(1<<j));
    for(j=7;j>=0;j--)  SPI_Send_1bit(LedsArray[i*3+1]&(1<<j));
    for(j=7;j>=0;j--)  SPI_Send_1bit(LedsArray[i*3+2]&(1<<j));
  }
}

/* Basic Color Effects -------------------------------------------------------*/
/**
  * @brief  Apply red to the every pixel
  * @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  Apply green to the every pixel
  * @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  Apply Blue to the every pixel
  * @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  Apply yellow to the every pixel
  * @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  Apply MAGENTA to the every pixel
  * @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  Apply Black to the every pixel
  * @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  Apply White to the every pixel
  * @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,k=256;
  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 * k / Pixel_LEN) + j) & 255));	//k值:RGB像素点之间的差距
    rgb_SendArray();
    Delay_1ms(10);
  }
}

如果把SPI设定在4.5M,是不是需要用SPI的一个字节模拟两个CODEx呢?

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值