STM32:TIMER PWM 输入检测

PWM输入检测是输入捕获的一个特例,可以测量频率与占空比

与输入捕获不同的是PWM输入模式会将同一个输入信号(TI1或TI2)连接到两个捕获装置(IC1和IC2)。这两个捕获装置一个捕获上升沿一个捕获下降沿。TI1FP1、TI2FP2它们中的一个被选择为触发输入且从模式控制器被配置为复位模式。

注意:只有TI1FP1和TI2FP2连到了从模式控制器,所以PWM输入模式只能使用TIMx_CH1 /TIMx_CH2信号。

 1 //Timer4 CH2测周期,CH1测占空比,PB7引脚来输入
 2 //这个捕捉的周期的范围是1-65535us,如果需要测更高频率的话更改里面的两种方式:
 3 //1,TIM_TimeBaseStructure.TIM_Prescaler =(72-1);更改分配系数调整精度
 4 //溢出计数也可以使用 
 5 void TIM4_CH2_CH1_PB7_PWMInPut_Init(void)
 6 {  
 7       
 8     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 9     NVIC_InitTypeDef         NVIC_InitStructure;
10     TIM_ICInitTypeDef        TIM4_ICInitStructure;
11     GPIO_InitTypeDef         GPIO_InitStructure;
12  
13     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);     //Open TIM4 clock
14     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //open gpioB clock
15     
16     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                               //GPIO??
17     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
18     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
19     GPIO_Init(GPIOB, &GPIO_InitStructure);
20  
21     TIM_TimeBaseStructure.TIM_Period = 0xffff; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值  
22     TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //设置用来作为TIMx时钟频率除数的预分频值  
23     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
24     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
25     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
26  
27     
28     /*配置中断优先级*/
29     NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;                     
30     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
31     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
32     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
33     NVIC_Init(&NVIC_InitStructure);
34  
35     TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2;                   
36     TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;       
37     TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;   
38     TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 
39     TIM4_ICInitStructure.TIM_ICFilter = 0x0;    
40     TIM_PWMIConfig(TIM4, &TIM4_ICInitStructure);     //PWM输入配置    
41            
42     TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);     //选择有效输入端        
43     TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);  //配置为主从复位模式
44     TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);                                       
45     TIM_ITConfig(TIM4, TIM_IT_CC2|TIM_IT_Update, ENABLE);          //中断配置
46 
47     TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
48     TIM_Cmd(TIM4, ENABLE);    
49 }
TIM4_CH2_CH1_PB7_PWMInPut_Init
 1 void TIM4_IRQHandler(void)
 2 {
 3   
 4     if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)
 5     {
 6 //        LED1_Toggle();
 7         TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
 8     }
 9 
10     if(TIM_GetITStatus(TIM4,TIM_IT_CC1)!=RESET)
11     {
12 
13         TIM_ClearITPendingBit(TIM4,TIM_IT_CC1);
14     }
15 
16     if(TIM_GetITStatus(TIM4,TIM_IT_CC2)!=RESET)
17     {
18 
19          printf("TIM4 frequency=%d , duty=%d \r\n",(1000/TIM_GetCapture2(TIM4)),(100*TIM_GetCapture1(TIM4)/TIM_GetCapture2(TIM4)));
20         TIM_ClearITPendingBit(TIM4,TIM_IT_CC2);
21     }
22 
23     if(TIM_GetITStatus(TIM4,TIM_IT_CC4)!=RESET)
24     {
25         TIM_ClearITPendingBit(TIM4,TIM_IT_CC4);
26     }    
27 
28 }
TIM4_IRQHandler

 

注意,如果用CH2测量频率,CH1测量占空比的话,只需要设置CH2,开启CC2中断即可,测量频率的CHX设置为上升沿捕获,

设置函数为: TIM_PWMIConfig(TIM4, &TIM4_ICInitStructure);
而不是TIM_ICInit(TIM4, &TIM4_ICInitStructure);如果选择这个函数初始化的话还需要设置CH1。

 

同样如果是CH2测量频率的话TIM_TS_TI2FP2,TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);     //选择有效输入端

如果CH1测量频率则为TIM_TS_TI1FP1

 

下面是TIMER3 配置CH1频率,CH2占空比

 1 //Timer3 CH1测周期,CH2测占空比,PA6引脚来输入
 2 //这个捕捉的周期的范围是1-65535us,如果需要测更高频率的话更改里面的两种方式:
 3 //1,TIM_TimeBaseStructure.TIM_Prescaler =(72-1);更改分配系数调整精度
 4 //溢出计数也可以使用
 5 void TIM3_CH1_CH2_PA6_PWMInPut_Init(void)
 6 {  
 7 
 8     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 9     NVIC_InitTypeDef         NVIC_InitStructure;
10     TIM_ICInitTypeDef        TIM3_ICInitStructure;
11     GPIO_InitTypeDef         GPIO_InitStructure;
12 
13     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //使能GPIOA时钟
14     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
15 
16     GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_6;  //PA0 清除之前设置  
17     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入  
18     GPIO_Init(GPIOA, &GPIO_InitStructure);
19     GPIO_ResetBits(GPIOA,GPIO_Pin_6);
20 
21     TIM_TimeBaseStructure.TIM_Period = 0xffff; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值     计数到5000为500ms
22     TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //设置用来作为TIMx时钟频率除数的预分频值  1Mhz的计数频率  
23     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
24     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
25     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
26 
27      //pwm输入模式ch1配置
28     TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1;      //选择输入端 IC1映射到TI1上
29       TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;    //上升沿捕获
30       TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 //CC1S=01
31       TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;     //配置输入分频,不分频 
32       TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
33     TIM_PWMIConfig(TIM3, &TIM3_ICInitStructure);     //PWM输入配置 
34 //      TIM_ICInit(TIM3, &TIM3_ICInitStructure);
35 
36 //    //pwm输入模式ch2配置
37 //    TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2;     //选择输入端 IC2映射到TI1上
38 //      TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;    //下降沿捕获
39 //      TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI; //映射到TI1上 //CC2S=10 
40 //      TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;     //配置输入分频,不分频 
41 //      TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
42 //      TIM_ICInit(TIM3, &TIM3_ICInitStructure);
43 
44     TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);     //选择有效输入端        
45     TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);  //配置为主从复位模式
46     TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); 
47     TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1,ENABLE ); //使能指定的TIM3中断,允许更新中断
48 
49     NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
50     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
51     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
52     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
53     NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
54 
55     TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设
56 }
TIM3_CH1_CH2_PA6_PWMInPut_Init
 1 void TIM3_IRQHandler(void)
 2 {
 3 
 4     if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
 5     {
 6         LED2_Toggle();
 7         TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
 8     }
 9 
10     if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET)
11     {
12          printf("TIM3 frequency=%d , duty=%d \r\n",(1000/TIM_GetCapture1(TIM3)),(100*TIM_GetCapture2(TIM3)/TIM_GetCapture1(TIM3)));
13         TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
14     }
15 
16     if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)
17     {
18 
19         TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
20     }
21     
22 
23 }
TIM3_IRQHandler

 

   

posted @ 2013-11-22 11:46  wwjdwy  阅读(3835)  评论(0编辑  收藏  举报