OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 261|回复: 0

STM32G070RBT6使用DMA PWM驱动WS2812波形不正确

[复制链接]

14

主题

27

帖子

0

精华

初级会员

Rank: 2

积分
116
金钱
116
注册时间
2018-4-24
在线时间
41 小时
发表于 2024-8-28 16:03:27 | 显示全部楼层 |阅读模式
50金钱
用的LL库驱动WS2812,占空比变化时,PWM的周期就变了,研究了很久请问是什么问题?
//初始化函数//
void MX_TIM15_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
       
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* Peripheral clock enable */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM15);

  /* TIM1 DMA Init */

  /* TIM1_CH3 Init */
  LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_7, LL_DMAMUX_REQ_TIM15_CH1);

  LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_7, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);

  LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_7, LL_DMA_PRIORITY_HIGH);

  LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_7, LL_DMA_MODE_NORMAL);

  LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_7, LL_DMA_PERIPH_NOINCREMENT);

  LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_7, LL_DMA_MEMORY_INCREMENT);

  LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_7, LL_DMA_PDATAALIGN_HALFWORD);

  LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_7, LL_DMA_MDATAALIGN_BYTE);

  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 79;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  TIM_InitStruct.RepetitionCounter = 0;
  LL_TIM_Init(TIM15, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM15);
//        LL_TIM_EnableARRPreload(TIM15);
       
  LL_TIM_SetClockSource(TIM15, LL_TIM_CLOCKSOURCE_INTERNAL);
  LL_TIM_OC_EnablePreload(TIM15, LL_TIM_CHANNEL_CH1);
       
  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.CompareValue = 0;
  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  LL_TIM_OC_Init(TIM15, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  LL_TIM_OC_DisableFast(TIM15, LL_TIM_CHANNEL_CH1);
       
  LL_TIM_SetTriggerOutput(TIM15, LL_TIM_TRGO_RESET);
  LL_TIM_SetTriggerOutput2(TIM15, LL_TIM_TRGO2_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM15);
  LL_TIM_OC_DisablePreload(TIM15, LL_TIM_CHANNEL_CH1);
       
  TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
  TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
  TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
  TIM_BDTRInitStruct.DeadTime = 0;
  TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
  TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
  TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
  TIM_BDTRInitStruct.BreakAFMode = LL_TIM_BREAK_AFMODE_INPUT;
  TIM_BDTRInitStruct.Break2State = LL_TIM_BREAK2_DISABLE;
  TIM_BDTRInitStruct.Break2Polarity = LL_TIM_BREAK2_POLARITY_HIGH;
  TIM_BDTRInitStruct.Break2Filter = LL_TIM_BREAK2_FILTER_FDIV1;
  TIM_BDTRInitStruct.Break2AFMode = LL_TIM_BREAK_AFMODE_INPUT;
  TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  LL_TIM_BDTR_Init(TIM15, &TIM_BDTRInitStruct);
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
       
  LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_7,DATA_SIZE*LED_NUM);
        LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_7,(uint32_t)Single_LED_Buffer);
        LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_7,(uint32_t)&(TIM15->CCR1));
        LL_DMA_ClearFlag_GI7(DMA1);
        LL_DMA_ClearFlag_TC7(DMA1);
        LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_7);                //使能传送完成中断
        LL_TIM_EnableDMAReq_CC1(TIM15);                                                                //使能TIM15的CC1 DMA请求
        LL_TIM_EnableAllOutputs(TIM15);                                                                //使能TIM的输出
        LL_TIM_CC_SetDMAReqTrigger(TIM15,LL_TIM_CCDMAREQUEST_CC);                //设置TIM15 DMA请求触发器
        LL_TIM_CC_EnableChannel(TIM15,LL_TIM_CHANNEL_CH1);                //使能TIM15的CH1通道

        LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_7);
        LL_TIM_CC_EnableChannel(TIM15, LL_TIM_CHANNEL_CH1);
        LL_TIM_EnableCounter(TIM15);
        LL_TIM_EnableDMAReq_UPDATE(TIM15);

//        LL_TIM_CC_EnablePreload
}


void PWM_WS2812B_Red(uint16_t num)
{
        PWM_WS2812B_Write_24Bits(num,0x00FF00);
        WS281x_Show();
//        WS2812B_Reset();
}


void WS281x_Show(void)
{
        ws2812_xfer_flag = 1;
    LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_7, DATA_SIZE*LED_NUM);
    LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_7);
    LL_TIM_EnableCounter(TIM15);
    while(ws2812_xfer_flag);
}


void PWM_WS2812B_Write_24Bits(uint16_t num,uint32_t GRB_Data)
{
  uint8_t i,j;
        memset(Single_LED_Buffer,0,DATA_SIZE*LED_NUM);
  for(j = 0; j < LED_NUM; j++)
  {
                        for(i = 0; i < DATA_SIZE; i++)
                {
                        /*因为数据发送的顺序是GRB,高位先发,所以从高位开始判断,判断后比较值先放入缓存数组*/
                        Single_LED_Buffer[(j*DATA_SIZE)+i] = ((GRB_Data << i) & 0x800000) ? T1H : T0H;
//                        Single_LED_Buffer[i] = ((GRB_Data << i) & 0x800000) ? T1H : T0H;
                }
  }
}




微信图片_20240828160236.png
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-22 09:51

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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