本帖最后由 xxh0201 于 2020-1-21 12:17 编辑
目的: 使用使用DMA_Channel5,TIM2_CH1。DMA读取buffer里面数据,填充TIM2的OC1的CCR1寄存器,输出不同占空比的PWM。
但,实际,输出是不正常的,不知什么原因。
如果改用 DMA1_Channel2、TIM2_UP,输出就正常,见下面代码中红色的地方是改动的。
原代码如下:
uint16_t LED_BYTE_Buffer[600];
//---------------------------------------------------------------//
void Timer2_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 90-1; //自动重装载寄存器周期的值为:90
TIM_TimeBaseStructure.TIM_Prescaler = 0; //分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = LED_BYTE_Buffer[0];
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->CCR1;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer; // this is the buffer memory
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // data shifted from memory to peripheral
DMA_InitStructure.DMA_BufferSize = 600;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_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;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
//TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
}
void PWM_send(uint8_t (void)
{
uint16_t buffersize= 600; // number of bytes bytes
DMA_SetCurrDataCounter(DMA1_Channel5, buffersize); // load number of bytes to be transferred
DMA_Cmd(DMA1_Channel5, ENABLE); // enable DMA channel 5
TIM_Cmd(TIM2, ENABLE); // enable Timer 2
while(!DMA_GetFlagStatus(DMA1_FLAG_TC5)) ; // wait until transfer complete
TIM_Cmd(TIM2, DISABLE); // disable Timer 2
DMA_Cmd(DMA1_Channel5, DISABLE); // disable DMA channel 5
DMA_ClearFlag([DMA1_FLAG_TC5); // clear DMA1 Channel 5 transfer complete flag
} |