使用STM32F407 TIM_DMA方式,驱动WS2812B。参考正点原子灯板的代码。但是只要我开启DMA( //DMA_ClearITPendingBit(DMA2_Stream6, DMA_IT_TCIF6);
//DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE);开启这两句)中断,板子就死机了。代码如下:
u16 MyTestDmaBuf0[24] = {67,142,67,142,67,142,67,142,67,142,67,142,67,142,67,142,67,142,67,142,67,142,67,142};
//u16 MyTestDmaBuf0[24] = {70,34,70,34,70,34,70,34,70,34,70,34,70,34,70,34,70,34,70,34,70,34,70,34};
//u16 MyTestDmaBuf0[24] = {105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104};
u16 MyTestDmaBuf1[24] = {105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104,105,104};
#if 1
void GTLM_Fun_Time1_Ch3_Pwm_DMA(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //使能PORTB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能TIM2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //使能DMA时钟
//初始化灯环RGB灯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA2_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&TIM1->CCR3) ;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)MyTestDmaBuf0;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 24;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream6, &DMA_InitStructure);
DMA_DoubleBufferModeCmd(DMA2_Stream6, ENABLE);//使能双缓冲
DMA_DoubleBufferModeConfig(DMA2_Stream6, (u32)MyTestDmaBuf1, DMA_Memory_0);
/***********************************************问题**************************************************/
/*************************开启下面两条语句,程序就不能启动,调试可以发现就挂在这个函数里面。********************/
//DMA_ClearITPendingBit(DMA2_Stream6, DMA_IT_TCIF6);
//DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 209;
//TIM_TimeBaseStructure.TIM_Period = 104;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 3 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_Pulse = MyTestDmaBuf0[0];
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
/* Enable preload feature */
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* DMA enable*/
DMA_Cmd(DMA2_Stream6, ENABLE);
/* TIM1 Update DMA Request enable */
TIM_DMACmd(TIM1, TIM_DMA_CC3, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
//TIM1->BDTR |= TIM_BDTR_MOE;
//printf("1111111111
");
//printf("2222222222222
");
//DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE);
//DMA_ClearITPendingBit(DMA2_Stream6, DMA_IT_TCIF6);
//printf("33333333333
");
}
#endif
void DMA2_Stream6_IRQHandler(void)
{
//printf("DMA1_Stream6_IRQHandler ");
ws2812DmaIsr();
}
一周热门 更多>