PWM 捕获3

前段时间抄袭其他的stm多通道捕获代码,调试没有通过,由于半路出家也不是读得太懂,后来发现官方有个例子,原来固件库里面自带PWM输入功能,经调试稳定可用,代码如下:

 

C代码   收藏代码
  1. //计时器和gpio口的初始化  
  2. GPIO_InitTypeDef GPIO_InitStructure;  
  3.   NVIC_InitTypeDef NVIC_InitStructure;  
  4.   
  5.   /* TIM2 clock enable */  
  6.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
  7.   
  8.   /* GPIOB clock enable */  
  9.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  
  10.     
  11.   /* TIM2 chennel2 configuration : PA.01 */  
  12.   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;  
  13.   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;  
  14.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  15.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  16.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;  
  17.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  18.     
  19.   /* Connect TIM pin to AF1 */  
  20.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1);  
  21.   
  22.   /* Enable the TIM2 global Interrupt */  
  23.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  24.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  25.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  26.   NVIC_Init(&NVIC_InitStructure);  
  27.       
  28.     TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;  
  29.   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;  
  30.   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;  
  31.   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
  32.   TIM_ICInitStructure.TIM_ICFilter = 0x0;  
  33.   
  34.   TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);  
  35.   
  36.   /* Select the TIM2 Input Trigger: TI2FP2 */  
  37.   TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);  
  38.   
  39.   /* Select the slave Mode: Reset Mode */  
  40.   TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);  
  41.   TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);  
  42.   
  43.   /* TIM enable counter */  
  44.   TIM_Cmd(TIM2, ENABLE);  
  45.   
  46.   /* Enable the CC2 Interrupt Request */  
  47.    TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);  

 相关中断函数如下:

 

 

C代码   收藏代码
  1. void TIM2_IRQHandler(void)  
  2. {  
  3.   RCC_GetClocksFreq(&RCC_Clocks);  
  4.   
  5.   /* Clear TIM2 Capture compare interrupt pending bit */  
  6.   TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);  
  7.   
  8.   /* Get the Input Capture value */  
  9.   IC2Value = TIM_GetCapture2(TIM2);  
  10.   
  11.   if (IC2Value != 0)  
  12.   {  
  13.     /* Duty cycle computation */  
  14.     DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value;  
  15.   
  16.     /* Frequency computation  
  17.        TIM2 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */  
  18.   
  19.     Frequency = RCC_Clocks.HCLK_Frequency / IC2Value;  
  20.   }  
  21.   else  
  22.   {  
  23.     DutyCycle = 0;  
  24.     Frequency = 0;  
  25.   }  
  26. }  

 这个方法经测试,是简单好用的,但根据资料表明这个是通道1和通道2配合实现,其他资料也没有说明如何捕获多个通道,是否能用3和4通道配合捕获,是否1个定时器能捕获2个通道。于是网上查到另一种方法可以1个定时器捕获4个通道(),但是待测试,记录如下:

 

C代码   收藏代码
  1. // STM32 TIM2 4 Channel Input Capture STM32F0-Discovery - sourcer32@gmail.com  
  2.    
  3. #include "stm32f0xx.h"  
  4. #include "stm32f0_discovery.h"  
  5.    
  6. //**************************************************************************************  
  7.    
  8. void TIM2_Config(void)  
  9. {  
  10.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  11.   TIM_ICInitTypeDef        TIM_ICInitStructure;  
  12.    
  13.   /* TIM2 Periph clock enable */  
  14.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
  15.    
  16.   /* TIM2 Configuration */  
  17.   TIM_DeInit(TIM2);  
  18.    
  19.   /* Time base configuration */  
  20.   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
  21.   TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz, from 48 MHz  
  22.   TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal, TIM2 is 32-bit counter  
  23.   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  
  24.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  25.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
  26.    
  27.   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // Rising/Falling/BothEdge  
  28.   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;  
  29.   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
  30.   TIM_ICInitStructure.TIM_ICFilter = 0x0;  
  31.    
  32.   TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;  
  33.   TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  34.   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;  
  35.   TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  36.   TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;  
  37.   TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  38.   TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;  
  39.   TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  40.    
  41.   /* TIM1 enable counter */  
  42.   TIM_Cmd(TIM2, ENABLE);  
  43.    
  44.   /* Enable the CCx Interrupt Request */  
  45.   TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);  
  46. }  
  47.    
  48. //**************************************************************************************  
  49.    
  50. volatile uint32_t Freq[4];  
  51.    
  52. void TIM2_IRQHandler(void)  
  53. {  
  54.     uint32_t Current, Delta;  
  55.     static uint32_t Last[4];  
  56.        
  57.   if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)  
  58.     {  
  59.         /* Clear TIM2_CH1 Capture compare interrupt pending bit */  
  60.         TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);  
  61.    
  62.         Current = TIM_GetCapture1(TIM2);  
  63.         Delta = Current - Last[0];  
  64.         Last[0] = Current;  
  65.         if (Delta)  
  66.             Freq[0] = 1000000 / Delta; // 1MHz clock  
  67.            
  68.         // ..  
  69.     }  
  70.        
  71.   if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)  
  72.     {  
  73.         /* Clear TIM2_CH2 Capture compare interrupt pending bit */  
  74.         TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);  
  75.    
  76.         Current = TIM_GetCapture2(TIM2);  
  77.         Delta = Current - Last[1];  
  78.         Last[1] = Current;  
  79.         if (Delta)  
  80.             Freq[1] = 1000000 / Delta; // 1MHz clock  
  81.            
  82.         // ..  
  83.     }  
  84.        
  85.   if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)  
  86.     {  
  87.         /* Clear TIM2_CH3 Capture compare interrupt pending bit */  
  88.         TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);  
  89.    
  90.         Current = TIM_GetCapture3(TIM2);  
  91.         Delta = Current - Last[2];  
  92.         Last[2] = Current;  
  93.         if (Delta)  
  94.             Freq[2] = 1000000 / Delta; // 1MHz clock  
  95.            
  96.         // ..  
  97.     }  
  98.        
  99.   if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)  
  100.     {  
  101.         /* Clear TIM2_CH4 Capture compare interrupt pending bit */  
  102.         TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);  
  103.    
  104.         Current = TIM_GetCapture4(TIM2);  
  105.         Delta = Current - Last[3];  
  106.         Last[3] = Current;  
  107.         if (Delta)  
  108.             Freq[3] = 1000000 / Delta; // 1MHz clock  
  109.            
  110.         // ..  
  111.     }  
  112. }  
  113.    
  114. //**************************************************************************************  
  115.    
  116. void NVIC_Config(void)  
  117. {  
  118.   NVIC_InitTypeDef NVIC_InitStructure;  
  119.    
  120.   /* Enable and set TIM2 Interrupt */  
  121.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  122.   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;  
  123.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  124.   NVIC_Init(&NVIC_InitStructure);  
  125. }  
  126.    
  127. //**************************************************************************************  
  128.    
  129. void GPIO_Config(void)  
  130. {  
  131.   GPIO_InitTypeDef GPIO_InitStructure;  
  132.    
  133.   /* GPIOA Periph clock enable */  
  134.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  
  135.    
  136.   /* Configure TIM2 input */  
  137.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;  
  138.   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;  
  139.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  140.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  141.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  142.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  143.    
  144.   /* Connect TIM pins to AF2 */  
  145.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_2); // TIM2_CH1 PA5  
  146.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2); // TIM2_CH2 PA1  
  147.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_2); // TIM2_CH3 PA2  
  148.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_2); // TIM2_CH4 PA3  
  149. }  
  150.    
  151. //**************************************************************************************  
  152.    
  153. int main(void)  
  154. {  
  155.   NVIC_Config();  
  156.    
  157.   GPIO_Config();  
  158.    
  159.   TIM2_Config();  
  160.    
  161.   while(1); /* Infinite loop */  
  162. }  

 还有另外一种方法待测试:

C代码   收藏代码
  1. // Hold onto the Channel 1 init structure -- we will use it to reverse  
  2. // polarity on every edge interrupt.  
  3. static TIM_ICInitTypeDef TIM_CH1_ICInitStructure;  
  4.   
  5. #define GPIO_AF_TIM2 GPIO_AF_2  
  6.   
  7. void ConfigPwmIn() {  
  8.  GPIO_InitTypeDef GPIO_InitStructure;  
  9.  TIM_ICInitTypeDef TIM_ICInitStructure;  
  10.  NVIC_InitTypeDef NVIC_InitStructure;  
  11.   
  12.  TIM_DeInit(TIM2 );  
  13.   
  14.  /* TIM2 clock enable */  
  15.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
  16.   
  17.  /* GPIOC clock enable */  
  18.  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);  
  19.   
  20.  /* TIM2 GPIO pin configuration : CH1=PD3, C2=PD4, CH3=PD7, CH4=PD6 */  
  21.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7 | GPIO_Pin_6;  
  22.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  23.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  24.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  25.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  26.  GPIO_Init(GPIOD, &GPIO_InitStructure);  
  27.   
  28.  /* Connect pins to TIM3 AF2 */  
  29.  GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_TIM2 );  
  30.  GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_TIM2 );  
  31.  GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_TIM2 );  
  32.  GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_TIM2 );  
  33.   
  34.  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  35.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  
  36.  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  37.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  38.  NVIC_Init(&NVIC_InitStructure);  
  39.   
  40.  /* Enable capture*/  
  41.  TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1;  
  42.  TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;  
  43.  TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;  
  44.  TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
  45.  TIM_CH1_ICInitStructure.TIM_ICFilter = 0;  
  46.  TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  47.  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;  
  48.  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;  
  49.  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;  
  50.  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
  51.  TIM_ICInitStructure.TIM_ICFilter = 0;  
  52.  TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  53.  TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;  
  54.  TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  55.  TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;  
  56.  TIM_ICInit(TIM2, &TIM_ICInitStructure);  
  57.   
  58.  /* Enable TIM2 */  
  59.  TIM_Cmd(TIM2, ENABLE);  
  60.   
  61.  /* Enable CC1-4 interrupt */  
  62.  TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);  
  63.   
  64.  /* Clear CC1 Flag*/  
  65.  TIM_ClearFlag(TIM2, TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4 );  
  66. }  
  67.   
  68. static volatile uint32_t ccr[4];  
  69. static volatile char pulseState = 0;  
  70.   
  71. void TIM2_IRQHandler() {  
  72.  if (TIM2 ->SR & TIM_IT_CC1 ) {  
  73.   TIM2 ->SR &= (~TIM_IT_CC1 );  
  74.   
  75.   if (pulseState == 0) {  
  76.    TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;  
  77.   
  78.    // Any time we get a rising edge on CH1, we reset the counter. All channels are  
  79.    // phase aligned, so they all use this as a reference.  
  80.    TIM_SetCounter(TIM2, 0);  
  81.   } else {  
  82.    TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;  
  83.   
  84.    // Pull the value on the falling edge.  
  85.    ccr[0] = TIM_GetCapture1(TIM2 );  
  86.   }  
  87.   pulseState = !pulseState;  
  88.   
  89.   // Reverse polarity.  
  90.   TIM_ICInit(TIM2, &TIM_CH1_ICInitStructure);  
  91.  }  
  92.   
  93.  if (TIM2 ->SR & TIM_IT_CC2 ) {  
  94.   TIM2 ->SR &= (~TIM_IT_CC2 );  
  95.   ccr[1] = TIM_GetCapture2(TIM2 );  
  96.  }  
  97.  if (TIM2 ->SR & TIM_IT_CC3 ) {  
  98.   TIM2 ->SR &= (~TIM_IT_CC3 );  
  99.   ccr[2] = TIM_GetCapture3(TIM2 );  
  100.  }  
  101.  if (TIM2 ->SR & TIM_IT_CC4 ) {  
  102.   TIM2 ->SR &= (~TIM_IT_CC4 );  
  103.   ccr[3] = TIM_GetCapture4(TIM2 );  
  104.  }  
  105. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值