void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitSture;
TIM_OCInitTypeDef TIM_OCInitTypeSture;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//¸´ÓÃÍÆÍì
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//TIM3_CH3
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitSture.TIM_Period = arr;
TIM_TimeBaseInitSture.TIM_Prescaler = psc;
TIM_TimeBaseInitSture.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitSture.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitSture);
TIM_OCInitTypeSture.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitTypeSture.TIM_Pulse = 0;
TIM_OCInitTypeSture.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitTypeSture.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC3Init(TIM3,&TIM_OCInitTypeSture);
TIM_CtrlPWMOutputs(TIM3,ENABLE);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
主程序:
main
{
delay_init();
TIM3_PWM_Init(899,0);
while(1)
{
TIM3_PWM_Init(300,240); //1000hz
TIM_SetCompare1(TIM3,150);//
delay_ms(500);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
TIM3_PWM_Init(300,240); //1000hz
TIM_SetCompare1(TIM3,150);//
delay_ms(500);
这一堆儿都不应该放在while循环里吧
TIM3_PWM_Init(300,240);这个放到main函数初始化里
其他放到中断函数里
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
unsigned short capture ;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, 150);
}
多谢大神liujie136997963,是我太大意了把1看成l了,蠢哭
一周热门 更多>