WS2812智能外控集成LED光源:
- 控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外空像素点。
- 内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
- 内置上电复位和掉电复位电路。
- 每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真彩色显示,扫描频率不低于400Hz/S。
- 串行级联接口,能通过一根信号线完成数据的接收与解码。
- 任意两点传输距离在不超过5米是无需增加任何电路。
- 当刷新速率30帧/秒时,低速模式级联数不小于512点,高速模式不小于1024点。
- 数据发送速度可达800Kbps。
- 光的颜色高度一致,性价比高。
/*
* ws2812.h
*
* Created on: May 6, 2021
* Author: song'yu'long
*/
#ifndef INC_WS2812_H_
#define INC_WS2812_H_
#include <stdio.h>
#define COLOR_BLACK (0x000000)
#define COLOR_WHITE (0xffffff)
#define COLOR_RED (0xff0000)
#define COLOR_GREEN (0x00ff00)
#define COLOR_YELLOW (0x0000ff)
void ws2812_onepixel(unsigned int color);
void ws2812_oneframe(void);
void ws2812_waterflow(void);
#endif /* INC_WS2812_H_ */
/*
* ws2812.c
*
* Created on: May 6, 2021
* Author: song'yu'long
*/
#include "gpio.h"
#include "ws2812.h"
#include "main.h"
#define DIN_PORT GPIOB //5050RGB WS2812-DIN Port
#define DIN_PIN GPIO_PIN_9 //5050RGB WS2812-DIN Pin
#define LEDS_NUM (9)
unsigned int rgbflow[][LEDS_NUM]={
{COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW},
{COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED},
{COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED},
{COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED},
{COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN},
{COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN},
{COLOR_YELLOW,COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN},
{COLOR_YELLOW,COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW},
{COLOR_YELLOW,COLOR_RED,COLOR_RED,COLOR_RED,COLOR_GREEN,COLOR_GREEN,COLOR_GREEN,COLOR_YELLOW,COLOR_YELLOW},
};
/*
* @brief nop延时
*/
void delay_xnop(int x)
{
while(x--)
{
asm("nop");
}
}
/*
* @brief WS2812 写入一个像素颜色
* @param color:颜色RGB值#RRGGBB, 如0xff0000红色
* @return none
*/
void ws2812_onepixel(unsigned int color)
{
// WS2812 颜色数据格式 G7~G0,R7~R0,B7~B0, RGB=>GRB
unsigned int tmp = (((color&0x00ff00)<<8)|((color&0xff0000)>>8)|(color&0xff));
// T0H 0码,高电平时间 0.35us ±150ns
// T0L 0码,低电平时间 0.80us ±150ns
// T1H 1码,高电平时间 0.70us ±150ns
// T1L 1码,低电平时间 0.60us ±150ns
// RES 帧单位,低电平时间 50us以上
// 注意:因使用STM32库函数操作GPIO,满足不了最小电平反转时间,后来改为寄存器操作即可满足!STM32 8MHz晶振,72MHz主频。
int i=0;
for(i=23; i>=0; i--)
{
if ((tmp>>i)&0x01) {
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
delay_xnop(6);
DIN_PORT->BRR = DIN_PIN;
delay_xnop(1);
} else {
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BSRR = DIN_PIN;
DIN_PORT->BRR = DIN_PIN;
DIN_PORT->BRR = DIN_PIN;
DIN_PORT->BRR = DIN_PIN;
}
}
}
/*
* @brief WS2812 写入一个帧间隔
* @param none
* @note DIN Low RES:>50us
* @return none
*/
void ws2812_oneframe(void)
{
HAL_GPIO_WritePin(DIN_PORT, DIN_PIN, GPIO_PIN_RESET);
delay_xnop(500);
}
void ws2812_waterflow(void)
{
int i=0,j=0;
for (i=0; i<(sizeof(rgbflow)/sizeof(rgbflow[0])); i++)
{
for(j=0; j<LEDS_NUM; j++)
{
ws2812_onepixel(rgbflow[i][j]);
}
ws2812_oneframe();
HAL_Delay(60);
}
}
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
// 流水灯测试9LED
ws2812_waterflow();
#if 0
//TODO:UPDATE RGB LED
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_oneframe();
osDelay(500);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_oneframe();
osDelay(500);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0x0000ff);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0xff0000);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_onepixel(0x00ff00);
ws2812_oneframe();
osDelay(500);
#endif
}
/* USER CODE END StartDefaultTask */
}
main.h和gpio.h是IDE配置工具自动生成的:
main.h
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
gpio.h
/**
******************************************************************************
* @file gpio.h
* @brief This file contains all the function prototypes for
* the gpio.c file
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_GPIO_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/