想用PWM 来驱动ws2812,然后试着用DMA+PWM 的方式,我看stm32有人做成功了,然后试着MM32F031这个芯片,
#define TIM2_CCR2_Address 0x40000038
void DMA_TIM2_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel3); //通道2
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR2_Address; //目标地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer; //数据地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向 MtoP
DMA_InitStructure.DMA_BufferSize = 10;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //TIM2_CCR2地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // stop DMA feed after buffer size is reached
//不循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //通道优先级 高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非存储器到存储器
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);
}
这是我DMA 的初始化
void TIM2_PWM_Init(u16 arr,u16 psc,u16 pluse)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开定时器2的时钟
//复用初始
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_2);
GPIO_InitStructure.GPIO_Pin = RGB_PIN; //TIM2_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = arr-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
/*****TIM_Period和TIM_Prescaler决定了周期*****=(11059200)/(arr+1)*(psc+1)*/
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ARRPreloadConfig(TIM2, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_OCStructInit(&TIM_OCInitStructure); //装载默认值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = pluse; //设置待装入捕获比较寄存器的脉冲值
/*******TIM_Pulse决定了占空比******/
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //CH2预装载使能
TIM_DMACmd(TIM2, TIM_DMA_Update|TIM_DMA_Trigger, ENABLE);
TIM_DMACmd(TIM2, TIM_DMA_CC2, ENABLE);
// TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
这是PWM 的初始化 |