久久久久久久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
  1. #include "ws2812.h"

  2. /* Variables -----------------------------------------------*/
  3. static uint8_t LEDbuffer[LED_BUFFER_SIZE];

  4. TIM_HandleTypeDef TimHandle;
  5. TIM_OC_InitTypeDef sConfig;
  6. GPIO_InitTypeDef GPIO_InitStruct;
  7. DMA_HandleTypeDef hdma_tim;

  8. /* Functions -----------------------------------------------*/

  9. /**
  10. * @brief TIM MSP Initialization
  11. *        This function configures the hardware resources used in this example:
  12. *           - Peripheral's clock enable
  13. *           - Peripheral's GPIO Configuration
  14. *           - DMA configuration for transmission request by peripheral
  15. * @param htim: TIM handle pointer
  16. * @retval None
  17. */
  18. void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) {
  19.         /*##-1- Enable peripherals and GPIO Clocks #################################*/
  20.         /* TIMx clock enable */
  21.         TIMx_CLK_ENABLE();

  22.         /* Enable GPIO Channel Clock */
  23.         TIMx_CHANNEL1_GPIO_CLK_ENABLE();

  24.         /* Enable DMA clock */
  25.         DMAx_CLK_ENABLE();

  26.         /* Configure TIM1_Channel1 in output, push-pull & alternate function mode */
  27.         GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL1;
  28.         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  29.         GPIO_InitStruct.Pull = GPIO_PULLUP;
  30.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  31.         GPIO_InitStruct.Alternate = GPIO_AF_TIMx;
  32.         HAL_GPIO_Init(TIMx_GPIO_CHANNEL1_PORT, &GPIO_InitStruct);

  33.         /* Set the parameters to be configured */
  34.         hdma_tim.Init.Request = TIMx_CC1_DMA_REQUEST;
  35.         hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH;
  36.         hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE;
  37.         hdma_tim.Init.MemInc = DMA_MINC_ENABLE;
  38.         hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  39.         hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  40.         hdma_tim.Init.Mode = DMA_CIRCULAR;
  41.         hdma_tim.Init.Priority = DMA_PRIORITY_HIGH;

  42.         /* Set hdma_tim instance */
  43.         hdma_tim.Instance = TIMx_CC1_DMA_INST;

  44.         /* Link hdma_tim to hdma[TIM_DMA_ID_CC1] (channel1) */
  45.         __HAL_LINKDMA(htim, hdma[TIM_DMA_ID_CC1], hdma_tim);

  46.         /* Initialize TIMx DMA handle */
  47.         HAL_DMA_Init(htim->hdma[TIM_DMA_ID_CC1]);

  48.         /*##-2- Configure the NVIC for DMA #########################################*/
  49.         /* NVIC configuration for DMA transfer complete interrupt */
  50.         HAL_NVIC_SetPriority(TIMx_DMA_IRQn, 0, 0);
  51.         HAL_NVIC_EnableIRQ(TIMx_DMA_IRQn);
  52. }

  53. void ws2812_init(void) {
  54.         fillBufferBlack();

  55.         TimHandle.Instance = TIMx;

  56.         TimHandle.Init.Period = TIMER_PERIOD - 1;
  57.         TimHandle.Init.RepetitionCounter = LED_BUFFER_SIZE + 1; //LED_BUFFER_SIZE + 1;//0xFFFF;
  58.         TimHandle.Init.Prescaler = (uint32_t)(
  59.                         (SystemCoreClock / TIMER_CLOCK_FREQ) - 1);
  60.         TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  61.         TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  62.         HAL_TIM_PWM_Init(&TimHandle);

  63.         /*##-2- Configure the PWM channel 3 ########################################*/
  64.         sConfig.OCMode = TIM_OCMODE_PWM1;
  65.         sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  66.         sConfig.OCFastMode = TIM_OCFAST_DISABLE;
  67.         sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
  68.         //sConfig.Pulse        = 0;
  69.         HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);

  70.         /*##-3- Start PWM signal generation in DMA mode ############################*/
  71.         HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_1, (uint32_t *) LEDbuffer,
  72.                         LED_BUFFER_SIZE);
  73. }

  74. void ws2812_update(void) {
  75.         HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
  76.         HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_1, (uint32_t *) LEDbuffer,
  77.                         LED_BUFFER_SIZE);
  78. }

  79. void setLEDcolor(uint32_t LEDnumber, uint8_t RED, uint8_t GREEN, uint8_t BLUE) {
  80.         uint8_t tempBuffer[24];
  81.         uint32_t i;
  82.         uint32_t LEDindex;
  83.         LEDindex = LEDnumber % LED_NUMBER;

  84.         for (i = 0; i < 8; i++) // GREEN data
  85.                 tempBuffer[i] = ((GREEN << i) & 0x80) ? WS2812_1 : WS2812_0;
  86.         for (i = 0; i < 8; i++) // RED
  87.                 tempBuffer[8 + i] = ((RED << i) & 0x80) ? WS2812_1 : WS2812_0;
  88.         for (i = 0; i < 8; i++) // BLUE
  89.                 tempBuffer[16 + i] = ((BLUE << i) & 0x80) ? WS2812_1 : WS2812_0;

  90.         for (i = 0; i < 24; i++)
  91.                 LEDbuffer[RESET_SLOTS_BEGIN + LEDindex * 24 + i] = tempBuffer[i];
  92. }

  93. void setWHOLEcolor(uint8_t RED, uint8_t GREEN, uint8_t BLUE) {
  94.         uint32_t index;

  95.         for (index = 0; index < LED_NUMBER; index++)
  96.                 setLEDcolor(index, RED, GREEN, BLUE);
  97. }

  98. void fillBufferBlack(void) {
  99.         /*Fill LED buffer - ALL OFF*/
  100.         uint32_t index, buffIndex;
  101.         buffIndex = 0;

  102.         for (index = 0; index < RESET_SLOTS_BEGIN; index++) {
  103.                 LEDbuffer[buffIndex] = WS2812_RESET;
  104.                 buffIndex++;
  105.         }
  106.         for (index = 0; index < LED_DATA_SIZE; index++) {
  107.                 LEDbuffer[buffIndex] = WS2812_0;
  108.                 buffIndex++;
  109.         }
  110.         LEDbuffer[buffIndex] = WS2812_0;
  111.         buffIndex++;
  112.         for (index = 0; index < RESET_SLOTS_END; index++) {
  113.                 LEDbuffer[buffIndex] = 0;
  114.                 buffIndex++;
  115.         }
  116. }

  117. void fillBufferWhite(void) {
  118.         /*Fill LED buffer - ALL OFF*/
  119.         uint32_t index, buffIndex;
  120.         buffIndex = 0;

  121.         for (index = 0; index < RESET_SLOTS_BEGIN; index++) {
  122.                 LEDbuffer[buffIndex] = WS2812_RESET;
  123.                 buffIndex++;
  124.         }
  125.         for (index = 0; index < LED_DATA_SIZE; index++) {
  126.                 LEDbuffer[buffIndex] = WS2812_1;
  127.                 buffIndex++;
  128.         }
  129.         LEDbuffer[buffIndex] = WS2812_0;
  130.         buffIndex++;
  131.         for (index = 0; index < RESET_SLOTS_END; index++) {
  132.                 LEDbuffer[buffIndex] = 0;
  133.                 buffIndex++;
  134.         }
  135. }

  136. void TIMx_DMA_IRQHandler(void) {
  137.         HAL_DMA_IRQHandler(TimHandle.hdma[TIM_DMA_ID_CC1]);
  138. }
復(fù)制代碼

WS2812.zip (25.38 KB, 下載次數(shù): 23)






歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 亚洲444kkkk在线观看最新 | 色吧综合 | 色永久| 日本激情视频在线播放 | 另类一区| 成人在线免费 | 精品1区 | 美女福利视频网站 | 天堂av中文在线 | 91久久精品国产 | 日本精品久久 | 国内精品久久久久久 | 亚洲高清av | 91av视频在线观看 | 亚洲国产精品久久久久久 | 激情av网站 | 国产精品毛片一区二区三区 | 久久精品久久久久久 | 中文字幕第一页在线 | 国产精品视频网 | 狠狠色综合欧美激情 | 操亚洲 | 国产欧美一区二区精品忘忧草 | av喷水 | 二区中文字幕 | 国产午夜精品一区二区三区四区 | 亚洲欧美日韩网站 | 亚洲精品日韩一区二区电影 | 日本在线观看视频 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 99re视频| 精品一区欧美 | 做a网站| 日韩视频专区 | 亚洲免费精品一区 | 99精品在线| 欧美激情精品久久久久久 | 成人免费视频观看视频 | 亚洲美乳中文字幕 | 自拍视频网站 | 最新国产精品精品视频 |