当前位置:网站首页>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 )
边栏推荐
- [beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
- An Optimal Buffer Management Scheme with Dynamic Thresholds论文总结
- Postman interface test | JS script blocking sleep and non blocking sleep
- Ue4/ue5 C disk enlargement processing
- [introduction to cryoelectron microscopy] Caltech open class course notes part 3:image formation
- [flask introduction series] installation and configuration of flask Sqlalchemy
- Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
- Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
- Jianmu continuous integration platform v2.5.2 release
- (视频+图文)机器学习入门系列-第5章 机器学习实践
猜你喜欢
(视频+图文)机器学习入门系列-第5章 机器学习实践
SQL 面试碰到的一个问题
[beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?
Keyboard processing in jetpack compose
Matrix decomposition and gradient descent
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
Joseph Ring problem
阿里巴巴政委体系-第四章、政委建在连队上
[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
Mysql rownum 实现
随机推荐
Day 014 2D array exercise
Cross domain problems when downloading webapi interface files
Go, how to become a gopher, and find work related to go language in 7 days, Part 1
Cyberpunk special effect shader
[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)
Beautiful girls
[academic related] why can't many domestic scholars' AI papers be reproduced?
[untitled] format save
Jump from mapper interface to mapping file XML in idea
Effective learning of medical image segmentation annotation based on noise pseudo tags and adversarial learning
Limitations of push down analysis
Record of problems caused by PIP upgrade damage
BiSeNet v2
[note] the art of research - (tell a good story and argument)
MySQL rownum implementation
[dry goods memo] 50 kinds of Matplotlib scientific research paper drawing collection, including code implementation
Taiyuan bus route crawling
Mysql rownum 实现
Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
SQL 面试碰到的一个问题