当前位置:网站首页>Ws2812b color lamp driver based on f407zgt6
Ws2812b color lamp driver based on f407zgt6
2022-07-29 08:06:00 【Bug can't be finished】
Hardware connection
WS2812B It's a full color LED control IC, Single bus control , You can cascade .

Single lamp bead connection will DIN The foot is connected to a IO Port and connect the power supply
A plurality of lamp beads are connected in series to connect the first lamp bead DOUT The foot is connected to the next lamp bead DIN foot
Driving principle
1. Sequence diagram

Pictured , Represented by different durations of high and low levels 0 perhaps 1, Use a long period of time to pull down to show reset, The following figure shows the corresponding different duration .
2. Data transmission mode

Pictured ,MCU To be connected in series GRB The code is transmitted into the first light bead , Each lamp receives 24bit data (GRB Color value ),D1 The light is receiving 24bit After the data , Will save the data , If you still receive data , Will pass DO Pass the foot to D2.
a key : The time of high and low levels must be controlled within the requirements of the specification
Driver code
Code part reference _ Xiangzi @ This elder brother's article
WS2812.h
typedef struct color{
// Structure definition
uint8_t R;
uint8_t G;
uint8_t B;
}Color_TypeDef;
#define LED_NUM 12 // Define the number of light beads , Easy to modify
#define WS2812B_High() HAL_GPIO_WritePin(RGB_GPIO_Port,RGB_Pin,GPIO_PIN_SET); // Define higher and lower
#define WS2812B_Low() HAL_GPIO_WritePin(RGB_GPIO_Port,RGB_Pin,GPIO_PIN_RESET);
/******************************************* function *********************************************/
void delay_320ns(void);
void delay_1000ns(void);
void MY_delay_us(uint16_t num);
void WS2812B_Reset(void);
void WS2812B_WriteByte(uint8_t dat);
void WS2812B_WriteColor(Color_TypeDef *pColor);
void Copy_Color(Color_TypeDef *pDst,Color_TypeDef *pScr);
void WS2812B_Refresh(void);
void WS2812B_Fillcolor(uint16_t start,uint16_t end,Color_TypeDef *pColor);
void WS2812B_Move(uint8_t dir);
WS2812.c
1. First write send 0 code 1 The function of the code , Used __nop(); function , One __nop() Corresponding to a machine cycle , The specific duration needs to be measured
void delay_320ns(void) //T0H & T1L Time delay 320ns
{
uint8_t i;
for(i=0;i<5;i++)
{
__nop();
}
}
void delay_1000ns(void) //T0L & T1H // Time delay 1000ns
{
uint8_t i;
for(i=0;i<25;i++)
{
__nop();
}
}
void MY_delay_us(uint16_t num) //us Stage delay
{
uint8_t i;
while(num)
{
for(i=0;i<25;i++)
{
__nop();
}
num--;
}
}
How to measure the time delay & function
stay main This function is called in the function , Then open debug, Test the delay duration of two functions in turn , Keep changing the number of cycles until the duration meets the timing requirements .
void WS2812B_Test2(void)
{
WS2812B_Hi();
delay_320ns();
//delay_1000ns();
WS2812B_Low();
}
debug The test method
Add breakpoints before and after the test function and place them in while Inside , Get into debug Look back at the lower right corner 
If it is t0 Right click to select t2 perhaps t1.
When the breakpoint in front of the test function stops, right-click to clear the value , The value displayed when the breakpoint to the end stops is the actual time of the delay function
2. Write data sending function
void WS2812B_Reset(void) //reset function
{
WS2812B_Low();
int i=60;
while(i--)
{
delay_1000ns();
}
}
void WS2812B_WriteByte(uint8_t dat) // Write a byte
{
uint8_t i;
for (i=0;i<8;i++)
{
// Send the high bit first
if (dat & 0x80) //1 // data &0x80 , If the high order is 1 The result is 1, Otherwise 0
{
WS2812B_High();
delay_1000ns(); //T1H
WS2812B_Low();
delay_320ns(); //T1L
}
else //0
{
WS2812B_High();
delay_320ns(); //T0H
WS2812B_Low();
delay_1000ns(); //T0L
}
dat<<=1;
}
}
void WS2812B_WriteColor(Color_TypeDef *pColor) // Send color , Complete by sending bytes of three colors respectively
{
WS2812B_WriteByte(pColor->G);
WS2812B_WriteByte(pColor->R);
WS2812B_WriteByte(pColor->B);
}
void WS2812B_Refresh(void) // Refresh the latest structure data to the light display
{
uint8_t i;
for(i=0;i<LED_NUM;i++)
{
WS2812B_WriteColor(&WS2812B_Buf[i]);
}
}
3. Lamp bead color treatment
( Realize the running lantern / Rainbow gradient )
//1. Copy colors
void Copy_Color(Color_TypeDef *pDst,Color_TypeDef *pScr) // Give the color of the last lamp to the previous lamp bead
{
pDst->R = pScr->R;
pDst->G = pScr->G;
pDst->B = pScr->B;
}
//2. Fill color ( Used to give a long string of light beads the same color value )
void WS2812B_Fillcolor(uint16_t start,uint16_t end,Color_TypeDef *pColor) // Began in , finally , Color value
{
if(start>end)
{
uint16_t temp;
temp=start;
start=end;
end=temp;
}
if (start>=LED_NUM)return;
if(end>=LED_NUM)end=LED_NUM-1;
while(start<=end)
{
Copy_Color(&WS2812B_Buf[start],pColor);
start++;
}
}
//3. Move the lamp bead color
void WS2812B_Move(uint8_t dir) //dir Is the number of moves , The bigger, the more
{
Color_TypeDef temp;
uint8_t i;
if(dir)
{
Copy_Color(&temp,&WS2812B_Buf[LED_NUM-1]);
i = LED_NUM-1;
while(i)
{
Copy_Color(&WS2812B_Buf[i],&WS2812B_Buf[i-1]);
i--;
}
Copy_Color(&WS2812B_Buf[0],&temp);
}
else
{
Copy_Color(&temp,&WS2812B_Buf[0]);
i = 0;
while(i<(LED_NUM-1))
{
Copy_Color(&WS2812B_Buf[i],&WS2812B_Buf[i+1]);
i++;
}
Copy_Color(&WS2812B_Buf[LED_NUM-1],&temp);
}
}
main.c
Definition
Color_TypeDef WS2812B_Buf[LED_NUM];
initialization
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
uint8_t i;
for(i=0;i<8;i++) // Before giving 12 The two light beads are different GRB value , What's given here is R-G-B The gradient
{
WS2812B_Buf[i].R=63-7*i;
WS2812B_Buf[i].G=7*i;
WS2812B_Buf[i].B=0;
HAL_Delay(1);
}
for(i=8;i<12;i++)
{
WS2812B_Buf[i].R=0;
WS2812B_Buf[i].G=127-7*i;
WS2812B_Buf[i].B=7*i-56;
HAL_Delay(1);
}
WS2812B_Refresh(); // Because the previous step only updated the color value , It doesn't show , So join refresh Refresh the display
loop
while(1)
{
HAL_Delay(40); // The time delay is used to adjust the speed
WS2812B_Move(5); //move Function controls the number of moves
WS2812B_Refresh(); // Refresh the display
}
Effect display
Initialized light effect ( Add a uniform board or painted acrylic board to make the light more uniform )
( Casually picked up a piece of paint sprayed on the waste board )
边栏推荐
- [note] the art of research - (tell a good story and argument)
- Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
- 阿里巴巴政委体系-第四章、政委建在连队上
- Arduinoide + stm32link burning debugging
- [deep learning] data preparation -pytorch custom image segmentation data set loading
- UE4 principle and difference between skylight and reflecting sphere
- Use the cloud code to crack the problem of authentication code encountered during login
- Crawl expression bag
- Solving linear programming problems based on MATLAB
- [cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
猜你喜欢

网络安全之安全基线

STM32 serial port garbled

Unity beginner 4 - frame animation and protagonist attack (2D)

10 common software architecture modes

How to connect VMware virtual machine to external network under physical machine win10 system

Cyberpunk special effect shader

sql判断语句的编写

@Use of jsonserialize annotation

V-Ray 5 acescg workflow settings

CDM - code division multiplexing (easy to understand)
随机推荐
关于pip升级损坏导致的问题记录
CentOS deploy PostgreSQL 13
In an SQL file, a test table and data are defined above, and you can select* from the test table below
An optimal buffer management scheme with dynamic thresholds paper summary
[beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
Operator overloading
Detailed explanation of the find command (the most common operation of operation and maintenance at the end of the article)
[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet
Effective learning of medical image segmentation annotation based on noise pseudo tags and adversarial learning
Cross domain problems when downloading webapi interface files
Some simple uses of crawler requests Library
JVM garbage collection mechanism (GC)
Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
【NOI模拟赛】计算几何(凸包,暴力,并查集)
Greenplus enterprise deployment
330. Complete the array as required
QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
Unity - default rendering pipeline - sculpt shader
Why don't you like it? It's easy to send email in cicd