STM32F030 PA7配置为TIM17 CH1输出PWM波, 用DMA来改变占空比. keil仿真可以看到CCR1 在0x3FFF和0xBFFF之间改变,但是用示波器测量一直是固定输出0x3FFF占空比. 请问这是什么缘故?
代码如下:
uint16_t Encode_Tab[4]={16383,49151,5000,880};
void ConfigRa7PwmOut(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//--------mcu io 设置
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_5); //复用位tim17_ch1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM3是复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
#define TIM17_DMAR_ADDRESS ((uint32_t)0x4001484C) /* TIM DMAR address */
void Timer17Config(void)
{
ConfigRa7PwmOut();
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 开启定时器时钟, 即pll时钟 CK_INT=48M
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); //使能TIM17时钟
/*--------------------时基结构体初始化-------------------------*/
// 自动重装载寄存器的值,累计 TIM_Period+1 个频率后产生一个更新或者中断
TIM_TimeBaseStructure.TIM_Period=65535-1; //最大计数65ms
// 驱动 CNT 计数器的时钟 = Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_Prescaler= 48-1; //1M
// 时钟分频因子 ,配置死区时间时需要用到
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// 重复计数器的值,没用到不用管
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
// 初始化定时器
TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
/*--------------------结构体初始化-------------------*/
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 32768;
TIM_OC1Init(TIM17, &TIM_OCInitStructure);
TIM_Cmd(TIM17, ENABLE);
TIM_CtrlPWMOutputs(TIM17, ENABLE);
TIM17->SR=0;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Channel1); /* DMA1 Channel1 Config */
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)TIM17_DMAR_ADDRESS;//(uint32_t)(&(TIM17->CCR1));
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)(&(TIM17->CCR1));
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Encode_Tab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
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_Mode_Normal;//
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
//DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); // 开启发送DMA通道中断
TIM_DMAConfig(TIM17, TIM_DMABase_CCR1,TIM_DMABurstLength_1Transfer);
/* TIM1 DMA Update enable */
TIM_DMACmd(TIM17, TIM_DMA_CC1, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);/* DMA1 Channel1 enable */
}
|