久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
stm32 anh led RGB WS2812b部分程序
[打印本頁(yè)]
作者:
ngoluc
時(shí)間:
2021-9-14 11:03
標(biāo)題:
stm32 anh led RGB WS2812b部分程序
stm32 anh led WS2812b
#include "ws2812.h"
/* Variables -----------------------------------------------*/
static uint8_t LEDbuffer[LED_BUFFER_SIZE];
TIM_HandleTypeDef TimHandle;
TIM_OC_InitTypeDef sConfig;
GPIO_InitTypeDef GPIO_InitStruct;
DMA_HandleTypeDef hdma_tim;
/* Functions -----------------------------------------------*/
/**
* @brief TIM MSP Initialization
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* - DMA configuration for transmission request by peripheral
* @param htim: TIM handle pointer
* @retval None
*/
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) {
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* TIMx clock enable */
TIMx_CLK_ENABLE();
/* Enable GPIO Channel Clock */
TIMx_CHANNEL1_GPIO_CLK_ENABLE();
/* Enable DMA clock */
DMAx_CLK_ENABLE();
/* Configure TIM1_Channel1 in output, push-pull & alternate function mode */
GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF_TIMx;
HAL_GPIO_Init(TIMx_GPIO_CHANNEL1_PORT, &GPIO_InitStruct);
/* Set the parameters to be configured */
hdma_tim.Init.Request = TIMx_CC1_DMA_REQUEST;
hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tim.Init.Mode = DMA_CIRCULAR;
hdma_tim.Init.Priority = DMA_PRIORITY_HIGH;
/* Set hdma_tim instance */
hdma_tim.Instance = TIMx_CC1_DMA_INST;
/* Link hdma_tim to hdma[TIM_DMA_ID_CC1] (channel1) */
__HAL_LINKDMA(htim, hdma[TIM_DMA_ID_CC1], hdma_tim);
/* Initialize TIMx DMA handle */
HAL_DMA_Init(htim->hdma[TIM_DMA_ID_CC1]);
/*##-2- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt */
HAL_NVIC_SetPriority(TIMx_DMA_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIMx_DMA_IRQn);
}
void ws2812_init(void) {
fillBufferBlack();
TimHandle.Instance = TIMx;
TimHandle.Init.Period = TIMER_PERIOD - 1;
TimHandle.Init.RepetitionCounter = LED_BUFFER_SIZE + 1; //LED_BUFFER_SIZE + 1;//0xFFFF;
TimHandle.Init.Prescaler = (uint32_t)(
(SystemCoreClock / TIMER_CLOCK_FREQ) - 1);
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&TimHandle);
/*##-2- Configure the PWM channel 3 ########################################*/
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
//sConfig.Pulse = 0;
HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
/*##-3- Start PWM signal generation in DMA mode ############################*/
HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_1, (uint32_t *) LEDbuffer,
LED_BUFFER_SIZE);
}
void ws2812_update(void) {
HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_1, (uint32_t *) LEDbuffer,
LED_BUFFER_SIZE);
}
void setLEDcolor(uint32_t LEDnumber, uint8_t RED, uint8_t GREEN, uint8_t BLUE) {
uint8_t tempBuffer[24];
uint32_t i;
uint32_t LEDindex;
LEDindex = LEDnumber % LED_NUMBER;
for (i = 0; i < 8; i++) // GREEN data
tempBuffer[i] = ((GREEN << i) & 0x80) ? WS2812_1 : WS2812_0;
for (i = 0; i < 8; i++) // RED
tempBuffer[8 + i] = ((RED << i) & 0x80) ? WS2812_1 : WS2812_0;
for (i = 0; i < 8; i++) // BLUE
tempBuffer[16 + i] = ((BLUE << i) & 0x80) ? WS2812_1 : WS2812_0;
for (i = 0; i < 24; i++)
LEDbuffer[RESET_SLOTS_BEGIN + LEDindex * 24 + i] = tempBuffer[i];
}
void setWHOLEcolor(uint8_t RED, uint8_t GREEN, uint8_t BLUE) {
uint32_t index;
for (index = 0; index < LED_NUMBER; index++)
setLEDcolor(index, RED, GREEN, BLUE);
}
void fillBufferBlack(void) {
/*Fill LED buffer - ALL OFF*/
uint32_t index, buffIndex;
buffIndex = 0;
for (index = 0; index < RESET_SLOTS_BEGIN; index++) {
LEDbuffer[buffIndex] = WS2812_RESET;
buffIndex++;
}
for (index = 0; index < LED_DATA_SIZE; index++) {
LEDbuffer[buffIndex] = WS2812_0;
buffIndex++;
}
LEDbuffer[buffIndex] = WS2812_0;
buffIndex++;
for (index = 0; index < RESET_SLOTS_END; index++) {
LEDbuffer[buffIndex] = 0;
buffIndex++;
}
}
void fillBufferWhite(void) {
/*Fill LED buffer - ALL OFF*/
uint32_t index, buffIndex;
buffIndex = 0;
for (index = 0; index < RESET_SLOTS_BEGIN; index++) {
LEDbuffer[buffIndex] = WS2812_RESET;
buffIndex++;
}
for (index = 0; index < LED_DATA_SIZE; index++) {
LEDbuffer[buffIndex] = WS2812_1;
buffIndex++;
}
LEDbuffer[buffIndex] = WS2812_0;
buffIndex++;
for (index = 0; index < RESET_SLOTS_END; index++) {
LEDbuffer[buffIndex] = 0;
buffIndex++;
}
}
void TIMx_DMA_IRQHandler(void) {
HAL_DMA_IRQHandler(TimHandle.hdma[TIM_DMA_ID_CC1]);
}
復(fù)制代碼
51hei.png
(10.61 KB, 下載次數(shù): 37)
下載附件
2021-9-15 00:51 上傳
WS2812.zip
(25.38 KB, 下載次數(shù): 23)
2021-9-14 11:03 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
亚洲444kkkk在线观看最新
|
色吧综合
|
色永久
|
日本激情视频在线播放
|
另类一区
|
成人在线免费
|
精品1区
|
美女福利视频网站
|
天堂av中文在线
|
91久久精品国产
|
日本精品久久
|
国内精品久久久久久
|
亚洲高清av
|
91av视频在线观看
|
亚洲国产精品久久久久久
|
激情av网站
|
国产精品毛片一区二区三区
|
久久精品久久久久久
|
中文字幕第一页在线
|
国产精品视频网
|
狠狠色综合欧美激情
|
操亚洲
|
国产欧美一区二区精品忘忧草
|
av喷水
|
二区中文字幕
|
国产午夜精品一区二区三区四区
|
亚洲欧美日韩网站
|
亚洲精品日韩一区二区电影
|
日本在线观看视频
|
亚洲一区二区三区乱码aⅴ 四虎在线视频
|
99re视频
|
精品一区欧美
|
做a网站
|
日韩视频专区
|
亚洲免费精品一区
|
99精品在线
|
欧美激情精品久久久久久
|
成人免费视频观看视频
|
亚洲美乳中文字幕
|
自拍视频网站
|
最新国产精品精品视频
|