請教ch32v103c8t6采用PWM+DMA驅(qū)動WS2812B問題

工程師您好,我想實現(xiàn)簡單的流水燈效果(紅綠藍交替),可是燈效太快。通過delay調(diào)節(jié)不理想,交替時,出現(xiàn)藍色只亮10個的情況(一共16個燈)。

代碼如下,有哪些需要改進的,如果可以,能否提供相關的例程,謝謝

#include?"ch32v10x.h"

#define?T0H?30
#define?T1H?60
#define?LED_NUM?16
#define?DATA_NUM?24

u16?RGB[DATA_NUM?*LED_NUM]?;

void?TIM1_PWMOUTPUT_Init(u16?arr,?u16?psc,?u16?ccp)
{
????GPIO_InitTypeDef?GPIO_InitStruct??;?????????
????TIM_OCInitTypeDef?TIM_OCInitStruct?=?{0}?;???????
????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStruct?=?{0}?;?????//這個結構體不初始化,無法點亮燈?具體原因未知

????//時鐘
????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,?ENABLE);
????RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,?ENABLE);

????//引腳
????GPIO_InitStruct.GPIO_Pin?=?GPIO_Pin_8;
????GPIO_InitStruct.GPIO_Mode?=?GPIO_Mode_AF_PP;
????GPIO_InitStruct.GPIO_Speed?=GPIO_Speed_50MHz;
????GPIO_Init(GPIOA,?&GPIO_InitStruct);

????//TIM1
????TIM_TimeBaseInitStruct.TIM_Period?=?arr?-?1;
????TIM_TimeBaseInitStruct.TIM_Prescaler?=?psc;
????TIM_TimeBaseInitStruct.TIM_ClockDivision?=?TIM_CKD_DIV1;
????TIM_TimeBaseInitStruct.TIM_CounterMode?=?TIM_CounterMode_Up;
????TIM_TimeBaseInit(TIM1,?&TIM_TimeBaseInitStruct);

????//PWM
????TIM_OCInitStruct.TIM_Pulse?=?ccp;
????TIM_OCInitStruct.TIM_OCMode?=?TIM_OCMode_PWM1;
????TIM_OCInitStruct.TIM_OutputState?=?TIM_OutputState_Enable;
//????TIM_OCInitStruct.TIM_OutputNState?=?TIM_OutputNState_Disable;
????TIM_OCInitStruct.TIM_OCPolarity?=?TIM_OCPolarity_High;
????TIM_OC1Init(TIM1,?&TIM_OCInitStruct);
????TIM_CtrlPWMOutputs(TIM1,?ENABLE);
????TIM_OC1PreloadConfig(TIM1,?TIM_OCPreload_Enable);
????TIM_ARRPreloadConfig(TIM1,?ENABLE);


}

void?DMA1_Init()
{
????DMA_InitTypeDef?DMA_InitStruct?;?????//如果這個結構體初始化,第一個燈將會最后亮

????//時鐘
????RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,?ENABLE);

????//DMA
????DMA_DeInit(DMA1_Channel2);
????DMA_InitStruct.DMA_PeripheralBaseAddr?=?(u32)&(TIM1->CH1CVR);
????DMA_InitStruct.DMA_PeripheralInc?=?DMA_PeripheralInc_Disable;
????DMA_InitStruct.DMA_MemoryBaseAddr?=?(u32)RGB;
????DMA_InitStruct.DMA_MemoryInc?=?DMA_MemoryInc_Enable;????????
????DMA_InitStruct.DMA_DIR?=?DMA_DIR_PeripheralDST;
????DMA_InitStruct.DMA_BufferSize?=?DATA_NUM;
????DMA_InitStruct.DMA_PeripheralDataSize?=?DMA_PeripheralDataSize_HalfWord;
????DMA_InitStruct.DMA_MemoryDataSize?=?DMA_MemoryDataSize_HalfWord;
????DMA_InitStruct.DMA_Mode?=?DMA_Mode_Normal;
????DMA_InitStruct.DMA_Priority?=?DMA_Priority_Medium;
????DMA_InitStruct.DMA_M2M?=?DMA_M2M_Disable;
????DMA_Init(DMA1_Channel2,?&DMA_InitStruct);

????TIM_DMACmd(TIM1,?TIM_DMA_CC1,?ENABLE);
}

void?Reset()
{

}

void?COlors(u16?num,?u32?rgb)
{
????u8?i,j;
????for(j?=?0;?j?<?num;?j++)
????{
????????for(i?=?0;?i?<8;?i++)???????????????//GRB格式發(fā)送,數(shù)組已對GRB編碼順序,改為RGB順序
????????{???????????????????????????????????????????????????????????????????????
????????????RGB[j?*?24?+?i?+?8]?=?((rgb?>>?8?>>?8?>>?i)?&?0x1)???T1H?:?T0H;?????//GREEN
????????????RGB[j?*?24?+?i]?=?((rgb?>>?8?>>?i)?&?0x1)???T1H?:?T0H;???????????????//RED
????????????RGB[j?*?24?+?i?+16]?=?((rgb?>>?i)?&?0x1)???T1H?:?T0H;???????????????//BLUE
????????}
????}
}


void?PWM_Show(u16?num)
{
????DMA_SetCurrDataCounter(DMA1_Channel2,?num?*?DATA_NUM);
????DMA_Cmd(DMA1_Channel2,?ENABLE);
????TIM_Cmd(TIM1,?ENABLE);
????while(DMA_GetFlagStatus(DMA1_FLAG_TC2)?!=?SET);
????DMA_Cmd(DMA1_Channel2,?DISABLE);
????TIM_Cmd(TIM1,?DISABLE);
????DMA_ClearFlag(DMA1_FLAG_TC2);
}

u8?i;

int?main(void)
{
????Delay_Init();
????TIM1_PWMOUTPUT_Init(90,?0,?0);
????DMA1_Init();
????while(1)
????{
????????for(i?=?0;?i?<?16;?i++)
????????{
????????????COlors(i+1,?0xff0000);
????????????PWM_Show(i+1);
????????????Delay_Ms(10);
????????}

????????for(i?=?0;?i?<?16;?i++)
????????{
????????????COlors(i+1,?0x00ff00);
????????????PWM_Show(i+1);
????????????Delay_Ms(10);
????????}

????????for(i?=?0;?i?<?16;?i++)
????????{
????????????COlors(i+1,?0x0000ff);
????????????PWM_Show(i+1);
????????????Delay_Ms(10);
????????}
????}
}


您好,附件為相關例程,可以參考下

icon_rar.gifCH32V103 PWM_WS2812_1.zip



非常感謝大佬的幫助,謝謝


ok,學習了


只有登錄才能回復,可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频