欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
STM32控制WS2812B燈的源碼
[打印本頁]
作者:
hazhenlin
時間:
2018-10-10 15:25
標題:
STM32控制WS2812B燈的源碼
這個文件很有用,分享給大家
單片機源程序如下:
#include "WS2812B.h"
/* Buffer that holds one complete DMA transmission
*
* Ensure that this buffer is big enough to hold
* all data bytes that need to be sent
*
* The buffer size can be calculated as follows:
* number of LEDs * 24 bytes + 42 bytes
*
* This leaves us with a maximum string length of
* (2^16 bytes per DMA stream - 42 bytes)/24 bytes per LED = 2728 LEDs
*/
//#define TIM3_CCR3_Address 0x4000043c // physical memory address of Timer 3 CCR1 register
//#define TIM3_CCR1_Address 0x40000434 // physical memory address of Timer 3 CCR1 register
#define TIM2_CCR1_Address 0x40000034
#define TIMING_ONE 50
#define TIMING_ZERO 25
uint16_t LED_BYTE_Buffer[300];
//---------------------------------------------------------------//
void Timer2_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Compute the prescaler value */
//PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 90-1; // 800kHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR1_Address; // physical address of Timer 3 CCR1
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer; // this is the buffer memory
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // data shifted from memory to peripheral
DMA_InitStructure.DMA_BufferSize = 42;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // stop DMA feed after buffer size is reached
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
}
/* This function sends data bytes out to a string of WS2812s
* The first argument is a pointer to the first RGB triplet to be sent
* The seconds argument is the number of LEDs in the chain
*
* This will result in the RGB triplet passed by argument 1 being sent to
* the LED that is the furthest away from the controller (the point where
* data is injected into the chain)
*/
void WS2812_send(uint8_t (*color)[3], uint16_t len)
{
uint8_t i;
uint16_t memaddr;
uint16_t buffersize;
buffersize = (len*24)+43; // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
memaddr = 0; // reset buffer memory index
while (len)
{
for(i=0; i<8; i++) // GREEN data
{
LED_BYTE_Buffer[memaddr] = ((color[0][1]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
for(i=0; i<8; i++) // RED
{
LED_BYTE_Buffer[memaddr] = ((color[0][0]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
for(i=0; i<8; i++) // BLUE
{
LED_BYTE_Buffer[memaddr] = ((color[0][2]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
len--;
}
//===================================================================//
//bug:最后一個周期波形不知道為什么全是高電平,故增加一個波形
LED_BYTE_Buffer[memaddr] = ((color[0][2]<<8) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
//===================================================================//
memaddr++;
while(memaddr < buffersize)
{
LED_BYTE_Buffer[memaddr] = 0;
memaddr++;
}
DMA_SetCurrDataCounter(DMA1_Channel2, buffersize); // load number of bytes to be transferred
DMA_Cmd(DMA1_Channel2, ENABLE); // enable DMA channel 6
TIM_Cmd(TIM2, ENABLE); // enable Timer 3
while(!DMA_GetFlagStatus(DMA1_FLAG_TC2)) ; // wait until transfer complete
TIM_Cmd(TIM2, DISABLE); // disable Timer 3
DMA_Cmd(DMA1_Channel2, DISABLE); // disable DMA channel 6
DMA_ClearFlag(DMA1_FLAG_TC2); // clear DMA1 Channel 6 transfer complete flag
}
復制代碼
所有資料51hei提供下載:
STM32F1_DMA_PWM_WS2813E_2018_05_06.rar
(282.3 KB, 下載次數: 296)
2018-10-10 16:54 上傳
點擊文件名下載附件
STM32_WS2812B
下載積分: 黑幣 -5
作者:
小小少年000
時間:
2018-10-11 11:21
感謝樓主,在此謝過!
作者:
654894
時間:
2018-10-11 15:44
STM32控制WS2812直接發送數據不就可以了嗎
作者:
edwardyan
時間:
2019-1-30 09:37
感謝樓主分享啊哈哈哈
作者:
paoapao
時間:
2019-3-14 17:24
學習了,謝謝
作者:
feng100520
時間:
2019-5-13 20:05
能請問一下 為什么用的是定時器2通道1 而DMA 使用的卻是通道2嘛? 對應表的話 應該是DMA通道5
作者:
chh543
時間:
2019-6-13 10:54
"WS2812B.h"文件沒有貼出來啊
作者:
lsfjing
時間:
2019-6-13 12:51
學習一下了!!!!!!!!!!!
作者:
tarchen
時間:
2019-7-16 15:25
謝謝分享,攢夠積分再下載。
作者:
kaixuan520
時間:
2019-7-18 20:14
feng100520 發表于 2019-5-13 20:05
能請問一下 為什么用的是定時器2通道1 而DMA 使用的卻是通道2嘛? 對應表的話 應該是DMA通道5
DMA通道選用TIMx_UP,別的通道不能用,反正我用不了別的
作者:
pxh316
時間:
2019-9-5 15:54
感謝樓主,在此謝過!
作者:
一直努力奔跑
時間:
2019-10-21 10:01
感謝樓主,在此謝過!
作者:
32654153
時間:
2019-11-29 09:56
感謝樓主
作者:
15717737432
時間:
2020-7-27 22:29
好東西,謝謝樓主分享,真的好用
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1