本帖最后由 凯爷儿 于 2017-8-3 16:23 编辑
写了一个程序,是用定时器2的通道1,2产生PWM,没有重映射,直接由PA0(对应通道1),PA1(对应通道2)输出,程序应该没有问题,可是我用示波器测了一下PA0,PA1并没有PWM产生,是程序错了吗,请大家帮忙看一下,谢谢了!
/ timer.c/
vu16 CCR1_VAL=40000;
vu16 CCR2_VAL=30000;
void TIM2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=60000;
TIM_TimeBaseInitStruct.TIM_Prescaler=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_Pulse=CCR1_VAL;
TIM_OC1Init(TIM2,&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_Pulse=CCR2_VAL;
TIM_OC2Init(TIM2,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
int main(void)
{
delay_init();
TIM2_Init();
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
好的,谢谢了
一周热门 更多>