嵌入式STM32学习笔记(5)——定时器主从模式,精确输出PWM脉冲数量

当要控制步进电机或其他通过控制器输入脉冲控制电机的时候,需要能根据需求调整的脉冲数量等;STM32中发出脉冲一般有两种方式:1)利用定时中断输出脉冲,但是间隔的延时会影响其他主程序的进程,当控制多个电机的时候就非常不可取;2)利用PWM脉宽调制,并通过主从定时器进行设定,好处是不占用主程序时钟,且能精准控制;

主从模式主要设置如下几个函数,比如设置定时器1为主,定时器1为从的时候:

   ///*///////定时器3主模式设置///////*/
    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
    //设置或者重置 TIMx 主/从模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_MasterSlaveMode:定时器主/从模式,TIM 主/从模式使能
    
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
    //选择 TIMx 触发输出模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_TRGOSource:触发输出模式
    //TIM_TRGOSource_Update:使用更新事件作为触发输出
        
    ///*///////定时器1从模式设置///////*/
    TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2);//TIM3主连接TIM1,TIM_TS_ITR2
    TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_External1);// 等同 TIM1->SMCR|=0x07 //设置从模式寄存器 
    //   TIM2->SMCR|=0x07;  

其中/TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2);中的参数需要注意,参考手册查对应的可用连接,如下:

嵌入式STM32学习笔记(5)——定时器主从模式,精确输出PWM脉冲数量_第1张图片

嵌入式STM32学习笔记(5)——定时器主从模式,精确输出PWM脉冲数量_第2张图片

如上必须选择对应的ITR参数;

下面贴出代码:

其中在主函数main.c,循环前加如下代码即可,代表产生1KH的PWM,8K脉冲后停止   :

Pulse_output(1000,8000);//1KHZ,8000个脉冲

对应的头文件中添加如下:

void Pulse_output(u32 Cycle,u32 PulseNum);//入口函数

void TIM1_config(u32 Cycle);//1为主模式
void TIM2_config(u32 PulseNum);//2为从模式

//void TIM3_config(u32 PulseNum);//3为主模式
//void TIM4_config(u32 Cycle);//4为从

//void TIM3_config(u32 PulseNum);//3为从模式
//void TIM4_config(u32 Cycle);//4为主模式

//void TIM1_config(u32 PulseNum);//1为从模式
//void TIM3_config(u32 Cycle);//3为主模式

1)/***定时器1主模式,定时器2从模式***/

/***定时器1主模式,定时器2从模式***/
void TIM1_config(u32 Cycle)
{
    GPIO_InitTypeDef GPIO_InitStructure; //GPIO设置,创建结构体
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器设置结构体
    TIM_OCInitTypeDef  TIM_OCInitStructure; //pwm波对应设置结构体
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1 , ENABLE); //开启时钟
 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11;  //TIM1_CH4 PA11
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    TIM_TimeBaseStructure.TIM_Period = Cycle-1;                                                   
    TIM_TimeBaseStructure.TIM_Prescaler =71;                    //设置用来作为TIMx时钟频率除数的预分频值                                                     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //设置时钟分割:TDTS= Tck_tim            
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;            //重复计数,一定要=0!!!
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);               //装载                        
 
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;          //选择定时器模式:TIM脉冲宽度调制模式1       
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
    TIM_OCInitStructure.TIM_Pulse = Cycle/2-1;                    //设置待装入捕获寄存器的脉冲值                                   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;      //输出极性       
 
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);     //装载通道1,PA8 
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);     //装载通道2,PA9,被串口占用
    TIM_OC4Init(TIM1, &TIM_OCInitStructure);     //装载通道4,PA11
 
    TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
    //设置或者重置 TIMx 主/从模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_MasterSlaveMode:定时器主/从模式,TIM 主/从模式使能
    
    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
    //选择 TIMx 触发输出模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_TRGOSource:触发输出模式
    //TIM_TRGOSource_Update:使用更新事件作为触发输出(TRGO)
    
 
//    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //使能或者失能 TIMx 在 CCR3 上的预装载寄存器
//    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);  //使能或者失能 TIMx 在 CCR3 上的预装载寄存器
    
    TIM_ARRPreloadConfig(TIM1, ENABLE);  // 使能或者失能 TIMx 在 ARR 上的预装载寄存器 
    //允许或禁止在定时器工作时向ARR的缓冲器中写入新值,以便在更新事件发生时载入覆盖以前的值
}

/***定时器2从模式***/
void TIM2_config(u32 PulseNum)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; //对应结构体声明
    NVIC_InitTypeDef NVIC_InitStructure;  //NVIC 对应结构体声明
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
    TIM_TimeBaseStructure.TIM_Period = PulseNum-1;   
    TIM_TimeBaseStructure.TIM_Prescaler =0;    
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
 
    TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);//选择 TIMx 输入触发源,TIM 内部触发 0      
    TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1 );// 等同 TIM2->SMCR|=0x07 //设置从模式寄存器 
    //   TIM2->SMCR|=0x07;                                  //设置从模式寄存器       
    TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE); //
 
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;        
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
}

//入口设定函数
void Pulse_output(u32 Cycle,u32 PulseNum)
{
    TIM2_config(PulseNum);//装载
    TIM_Cmd(TIM2, ENABLE);//使能
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除TIMx 的中断待处理位
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能或者失能指定的 TIMx 中断
    TIM1_config(Cycle); //装载   
    TIM_Cmd(TIM1, ENABLE);//使能
    TIM_CtrlPWMOutputs(TIM1, ENABLE);   //高级定时器一定要加上,主输出使能
}
  
//中断处理函数
void TIM2_IRQHandler(void) 
{ 
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)     // TIM_IT_CC1
    { 
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // 清除中断标志位 
        TIM_CtrlPWMOutputs(TIM1, DISABLE);  //主输出使能
        TIM_Cmd(TIM1, DISABLE); // 关闭定时器 
        TIM_Cmd(TIM2, DISABLE); // 关闭定时器 
        TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);         
    } 
} 

2)/***定时器3主模式,定时器1从模式***/,这个代码由于是高级定时器1为从稍有不同

//定时器3主模式,定时器1从模式
//PWM输出
void TIM3_config(u32 Cycle)
{
    GPIO_InitTypeDef GPIO_InitStructure;//GPIO设置,创建结构体
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器设置结构体
    TIM_OCInitTypeDef  TIM_OCInitStructure; //pwm波对应设置结构体
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM_CH1和T
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseStructure.TIM_Period = Cycle-1;                                                   
    TIM_TimeBaseStructure.TIM_Prescaler =71;                    //设置用来作为TIMx时钟频率除数的预分频值                                                     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //设置时钟分割:TDTS= Tck_tim            
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                                       

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;          //选择定时器模式:TIM脉冲宽度调制模式1       
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
    TIM_OCInitStructure.TIM_Pulse = Cycle/2-1;                    //设置待装入捕获寄存器的脉冲值                                   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;      //输出极性       
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);   //通道      
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);   //通道   
     
    ///*///////定时器3主模式设置///////*/
    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
    //设置或者重置 TIMx 主/从模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_MasterSlaveMode:定时器主/从模式,TIM 主/从模式使能
    
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
    //选择 TIMx 触发输出模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_TRGOSource:触发输出模式
    //TIM_TRGOSource_Update:使用更新事件作为触发输出
      
    ///*///////定时器1从模式设置///////*/
    TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2);//TIM3主连接TIM1,TIM_TS_ITR2
    TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_External1);// 等同 TIM1->SMCR|=0x07 //设置从模式寄存器 
    //   TIM2->SMCR|=0x07;        
          
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //通道1使能 
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //通道2使能 
    TIM_ARRPreloadConfig(TIM3, ENABLE);  //使能或者失能 TIMx 在 ARR 上的预装载寄存器                                                        
}

/***定时器1从模式***/
void TIM1_config(u32 PulseNum)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//TIMx初始化结构体
    NVIC_InitTypeDef NVIC_InitStructure; //NVIC结构体
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); //使能TIM1时钟

    TIM_TimeBaseStructure.TIM_Period = PulseNum-1; //设置自动重装载周期值
    TIM_TimeBaseStructure.TIM_Prescaler =0;    
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;     
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//tim1必须加这句不然没反应
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);   //      
    TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE); //使能中断

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn ;  //全局中断          
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);      
}

//入口函数
void Pulse_output(u32 Cycle,u32 PulseNum)
{
    TIM1_config(PulseNum);
    TIM_Cmd(TIM1, ENABLE);
    TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
    TIM3_config(Cycle);
    TIM_Cmd(TIM3, ENABLE);
}

//中断函数
void TIM1_UP_IRQHandler(void)//tim的中断函数比较特殊,注意区别比其他多个UP
{ 
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)     // TIM_IT_CC1
    { 
        TIM_ClearITPendingBit(TIM1,TIM_IT_Update); // 清除中断标志位 
        TIM_CtrlPWMOutputs(TIM1, DISABLE);  //主输出使能
        TIM_Cmd(TIM3, DISABLE); // 关闭定时器 
        TIM_Cmd(TIM1, DISABLE); // 关闭定时器 
        TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE); //关闭        
    } 
} 

3)/***定时器3主模式,定时器4从模式***/

//定时器3主模式,定时器4从模式
//PWM输出
void TIM3_config(u32 Cycle)
{
    GPIO_InitTypeDef GPIO_InitStructure;//GPIO设置,创建结构体
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器设置结构体
    TIM_OCInitTypeDef  TIM_OCInitStructure; //pwm波对应设置结构体
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM_CH1和T
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseStructure.TIM_Period = Cycle-1;                                                   
    TIM_TimeBaseStructure.TIM_Prescaler =71;                    //设置用来作为TIMx时钟频率除数的预分频值                                                     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //设置时钟分割:TDTS= Tck_tim            
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                                       

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;          //选择定时器模式:TIM脉冲宽度调制模式1       
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
    TIM_OCInitStructure.TIM_Pulse = Cycle/2-1;                    //设置待装入捕获寄存器的脉冲值                                   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;      //输出极性       
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);   //通道      
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);   //通道   
    
    /*///////定时器3主模式设置///////*/
    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
    //设置或者重置 TIMx 主/从模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_MasterSlaveMode:定时器主/从模式,TIM 主/从模式使能
    
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
    //选择 TIMx 触发输出模式
    //TIMx: x 可以是 2, 3 或者 4,来选择 TIM 外设
    //TIM_TRGOSource:触发输出模式
    //TIM_TRGOSource_Update:使用更新事件作为触发输出(TRG
        
    /*///////定时器4从模式设置///////*/
    TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);//TIM3主连接TIM4,TIM_TS_ITR2
    TIM_SelectSlaveMode(TIM4,TIM_SlaveMode_External1);// 等同 TIM2->SMCR|=0x07 //设置从模式寄存器 
    //   TIM2->SMCR|=0x07;        
          
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //通道1使能 
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //通道2使能 
    TIM_ARRPreloadConfig(TIM3, ENABLE);  //使能或者失能 TIMx 在 ARR 上的预装载寄存器                                                        
}

/***定时器4从模式***/
void TIM4_config(u32 PulseNum)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure; 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = PulseNum-1; //设置自动重装载周期值
    TIM_TimeBaseStructure.TIM_Prescaler =0;    
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;     
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);  
    
//    TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);//TIM3主连接TIM4,TIM_TS_ITR2
//    TIM_SelectSlaveMode(TIM4,TIM_SlaveMode_External1);// 等同 TIM2->SMCR|=0x07 //设置从模式寄存器 
//    //   TIM2->SMCR|=0x07;        
    
    TIM_ITConfig(TIM4,TIM_IT_Update,DISABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //全局中断      
    
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
}

void Pulse_output(u32 Cycle,u32 PulseNum)
{
    TIM4_config(PulseNum);
    TIM_Cmd(TIM4, ENABLE);
    TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
    TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
    TIM3_config(Cycle);
    TIM_Cmd(TIM3, ENABLE);
}

void TIM4_IRQHandler(void) 
{ 
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)     // TIM_IT_CC1
    { 
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update); // 清除中断标志位 
        TIM_CtrlPWMOutputs(TIM4, DISABLE);  //主输出使能
        TIM_Cmd(TIM3, DISABLE); // 关闭定时器 
        TIM_Cmd(TIM4, DISABLE); // 关闭定时器 
        TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE);         
    } 
} 

4)/***定时器4主模式,定时器3从模式***/

//定时器4主模式,定时器3从模式
//PWM输出
void TIM4_config(u32 Cycle)
{
    GPIO_InitTypeDef GPIO_InitStructure;//GPIO设置,创建结构体
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器设置结构体
    TIM_OCInitTypeDef  TIM_OCInitStructure; //pwm波对应设置结构体
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //开启时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                   //TIM2_CH2 PB7
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    TIM_TimeBaseStructure.TIM_Period = Cycle-1;                                                   
    TIM_TimeBaseStructure.TIM_Prescaler =71;                    //设置用来作为TIMx时钟频率除数的预分频值                                                     
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //设置时钟分割:TDTS= Tck_tim            
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);                                       

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;          //选择定时器模式:TIM脉冲宽度调制模式1       
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
    TIM_OCInitStructure.TIM_Pulse = Cycle/2-1;                    //设置待装入捕获寄存器的脉冲值                                   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;      //输出极性       
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);                                                         

    TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);
    TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Update);

    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);                              
    TIM_ARRPreloadConfig(TIM4, ENABLE);                                                          
}


/***定时器3从模式***/
void TIM3_config(u32 PulseNum)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure; 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = PulseNum-1; //设置自动重装载周期值
    TIM_TimeBaseStructure.TIM_Prescaler =0;    
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;     
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  

    TIM_SelectInputTrigger(TIM3, TIM_TS_ITR3);
    TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_External1 );// 等同 TIM2->SMCR|=0x07 //设置从模式寄存器 
    //   TIM2->SMCR|=0x07;            
    TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;        
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
}

void Pulse_output(u32 Cycle,u32 PulseNum)
{
    TIM3_config(PulseNum);
    TIM_Cmd(TIM3, ENABLE);
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    TIM4_config(Cycle);
    TIM_Cmd(TIM4, ENABLE);
}

void TIM3_IRQHandler(void) 
{ 
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)     // TIM_IT_CC1
    { 
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // 清除中断标志位 
        TIM_CtrlPWMOutputs(TIM3, DISABLE);  //主输出使能
        TIM_Cmd(TIM4, DISABLE); // 关闭定时器 
        TIM_Cmd(TIM3, DISABLE); // 关闭定时器 
        TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);         
    } 
} 

 

 

 

你可能感兴趣的:(stm32)