怎样让定时器输出一定数量的脉冲

2019-08-14 19:28发布

如题,如何让定时器输出一定数量的PWM呢,我要用它来做工作前监测。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
15条回答
laoguren1122
2019-08-16 11:20
TIM2,TIM1进行主从配置
void TIM_PWM_Number_Init(u16 master_arr, u16 master_psc, u16 slave_arr, u16 slave_psc)

{    

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure; 
NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能定时器2的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//使能定时器3的时钟
   
   //设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8|GPIO_Pin_9;  //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);   //初始化GPIO
   

  //TIM2工作在单脉冲下
   TIM_TimeBaseStructure.TIM_Period = master_arr;//TIM2per;//重装值
   TIM_TimeBaseStructure.TIM_Prescaler = master_psc;//7200;//预分频值,每100us计数一次
   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0;//TIM_CKD_DIV1;//没有时钟分割
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
   TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

 
   TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止
   TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器
   TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);

    TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE); // 中断源, TIM 触发中断源 使能
    
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
//  TIM_OCInitStructure.TIM_Pulse = 1;//比较捕获1的预装载值
  TIM_OC1Init(TIM2,&TIM_OCInitStructure);  

  TIM_Cmd(TIM2,DISABLE);//先不使能能TIM2

   

  //TIM1工作在从模式的门控模式下的PWM输出模式
   TIM_TimeBaseStructure.TIM_Period = slave_arr; //TIM3per;设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = slave_psc; //720;设置用来作为TIMx时钟频率除数的预分频值 
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0; //TIM_CKD_DIV1;/设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3;  //TIM向上计数模式
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 10;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 
  

  TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);//TIM3为门控模式
 // TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);//使能TIM3的主从模式
  TIM_SelectInputTrigger(TIM1,TIM_TS_ITR1);//内部触发,从TIM2触发

//初始化TIM3 Channel2 WM模式  
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCNPolarity_Low; //输出极性:TIM输出比较极性高
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
  TIM_OC2Init(TIM1, &TIM_OCInitStructure); 
 
    TIM_CtrlPWMOutputs(TIM1,ENABLE);
    TIM_Cmd(TIM1,ENABLE);//使能TIM3

}


void TIM2_IRQHandler(void)   //TIM3中断
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
// LED1=!LED1;
     TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
}
}


main()
{
 //初始化TIM1,2使其固定数量脉冲输出
TIM_PWM_Number_Init(3999,719,999,71);
TIM_SetCompare1(TIM1,arr/2+400);
TIM_SetCompare2(TIM1,arr/2-400);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
 
TIM_SetCompare1(TIM2,10);
TIM_Cmd(TIM2, ENABLE);

}>

一周热门 更多>