找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1113|回复: 0
打印 上一主题 下一主题
收起左侧

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. }
复制代码

WS2812.zip (25.38 KB, 下载次数: 22)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表