目的: 使用DMA读取buffer里面数据,填充TIM2的OC1的CCR1寄存器,输出不同占空比的PWM。现在不知怎么使用DMA,参照stdperiph中TIM1 DMA的例程。无法实现pwm输出。求帮助。
#define TIM2_CCR1_Address 0x40000034
#define TIME2_DMA_ENABLE
uint16_t SRC_Buffer[10] = {200,500,200,500,200,500,200,500,200,500};
void Time2_Initialzation(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
DMA_InitTypeDef DMA_InitStructure;
/* TIM2 Configuration ---------------------------------------------------
TIM2CLK = 72 MHz, Prescaler = 0, TIM2 counter clock = 72 MHz
TIM2 frequency = TIM2CLK/(TIM2_Period + 1) = 17.57 KHz
- TIM2 Channel1 & Channel1N duty cycle = TIM2->CCR1 / (TIM2_Period + 1) = 50%
----------------------------------------------------------------------- */
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1023;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 2;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* GPIOB Configuration: 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);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;;
TIM_OCInitStructure.TIM_Pulse = 0;
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
//TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
#ifdef TIME2_DMA_ENABLE
/* DMA1 Channel5 Config */
DMA_DeInit(DMA1_Channel5);
//DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR1_Address;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->CCR1;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 3;
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_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
/* DMA1 Channel5 enable */
DMA_Cmd(DMA1_Channel5, ENABLE);
//TIM_DMAConfig(TIM2,TIM_DMABase_CCR1,TIM_DMABurstLength_2Byte);
/* TIM1 Update DMA Request enable */
TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
//TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
//
#endif
/* TIM2 counter enable */
TIM_Cmd(TIM2, ENABLE);
/* TIM2 Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
RCC 已配置,请忽略。 |