捕捉PWM信号

2019-07-20 23:10发布

我是用例子TIM5改的,用TIM3和TIM4捕捉PWM信号一直不成功,运行结果会一直刷新显示通道3和4,并且通道3,4的数值一直是65535,也就是说TIM3CH1_CAPTURE_VAL一直为0,而TIM3CH1_CAPTURE_STA一直是1100 0001,即溢出一次,实在是找不到原因,求大神指点一二~
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"

//ALIENTEK ̽Ë÷ÕßSTM32F407¿ª·¢°å ʵÑé10
//ÊäÈ벶»ñʵÑé -¿âº¯Êý°æ±¾
//¼¼ÊõÖ§³Ö£ºwww.openedv.com
//ÌÔ±¦µêÆÌ£ºhttp://eboard.taobao.com  
//¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾  
//×÷ÕߣºÕýµãÔ ×Ó @ALIENTEK



extern u8  TIM5CH1_CAPTURE_STA;  //ÊäÈ벶»ñ״̬         
extern u32 TIM5CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ  
extern u8  TIM2CH1_CAPTURE_STA;  //ÊäÈ벶»ñ״̬         
extern u32 TIM2CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ   
extern u8  TIM3CH1_CAPTURE_STA;  //ÊäÈ벶»ñ״̬         
extern u16 TIM3CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ   
extern u8  TIM4CH1_CAPTURE_STA;  //ÊäÈ벶»ñ״̬         
extern u16 TIM4CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ   
int main(void)
{
long long temp=0;  
long long temp2=0;  
long long temp3=0;  
long long temp4=0;  
  long long temp5=0;  
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é3
delay_init(168);  //³õʼ»¯ÑÓʱº¯Êý
uart_init(115200);//³õʼ»¯´®¿Ú²¨ÌØÂÊΪ115200

  //TIM14_PWM_Init(500-1,84-1);        //84M/84=1MhzµÄ¼ÆÊýƵÂʼÆÊýµ½500,PWMƵÂÊΪ1M/500=2Khz     
  TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1); //ÒÔ1MhzµÄƵÂʼÆÊý void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
TIM2_CH1_Cap_Init(0XFFFFFFFF,84-1); //ÒÔ1MhzµÄƵÂʼÆÊý   void TIM2_CH1_Cap_Init(u32 arr,u16 psc)
TIM3_CH1_Cap_Init(0XFFFF,84-1);
TIM4_CH1_Cap_Init(0XFFFF,84-1);
    while(1)
{
   delay_ms(10);
// TIM_SetCompare1(TIM14,TIM_GetCapture1(TIM14)+1);
// if(TIM_GetCapture1(TIM14)==300)TIM_SetCompare1(TIM14,0);   
   if(TIM5CH1_CAPTURE_STA&0X80)        //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ,×î¸ßλΪ1ʱ³ÉÁ¢
  {
   temp=TIM5CH1_CAPTURE_STA&0X3F; //¼ÆËãÒç³ö´ÎÊý
   temp*=0XFFFFFFFF;              //Òç³öʱ¼ä×ܺÍ
   temp+=TIM5CH1_CAPTURE_VAL;     //µÃµ½×ܵĸߵçƽʱ¼ä
  
   printf("1 HIGH:%lld us ",temp); //´òÓ¡×ܵĸߵãƽʱ¼ä
   TIM5CH1_CAPTURE_STA=0;        //¿ªÆôÏÂÒ»´Î²¶»ñ
  }
  delay_ms(10);
  if(TIM2CH1_CAPTURE_STA&0X80)        //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
  {
   temp2=TIM2CH1_CAPTURE_STA&0X3F;
   temp2*=0XFFFFFFFF;              //Òç³öʱ¼ä×ܺÍ
   temp2+=TIM2CH1_CAPTURE_VAL;     //µÃµ½×ܵĸߵçƽʱ¼ä
   printf("2 HIGH:%lld us ",temp2); //´òÓ¡×ܵĸߵãƽʱ¼ä
   TIM2CH1_CAPTURE_STA=0;        //¿ªÆôÏÂÒ»´Î²¶»ñ
  }
  if(TIM3CH1_CAPTURE_STA&0X80)        //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ,×î¸ßλΪ1ʱ³ÉÁ¢
  {
   temp3=TIM3CH1_CAPTURE_STA&0X3F; //¼ÆËãÒç³ö´ÎÊý,1100 0001
   temp3*=0XFFFF;              //Òç³öʱ¼ä×ܺÍ
   temp3+=TIM3CH1_CAPTURE_VAL;     //µÃµ½×ܵĸߵçƽʱ¼ä   0
   //temp5=TIM3CH1_CAPTURE_STA;
   //printf("temp5 :%lld us ",temp5); //´òÓ¡×ܵĸߵãƽʱ¼ä
   printf("temp3 :%lld us ",temp3); //´òÓ¡×ܵĸߵãƽʱ¼ä
   TIM3CH1_CAPTURE_STA=0;        //¿ªÆôÏÂÒ»´Î²¶»ñ
  }
/* delay_ms(10);
  if(TIM4CH1_CAPTURE_STA&0X80)        //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
  {
   temp4=TIM4CH1_CAPTURE_STA&0X3F;
   temp4*=0XFFFF;              //Òç³öʱ¼ä×ܺÍ
   temp4+=TIM4CH1_CAPTURE_VAL;     //µÃµ½×ܵĸߵçƽʱ¼ä
   printf("4 HIGH:%lld us ",temp4); //´òÓ¡×ܵĸߵãƽʱ¼ä
   TIM4CH1_CAPTURE_STA=0;        //¿ªÆôÏÂÒ»´Î²¶»ñ
  }*/
}
}


下面是3的中断服务程序
void TIM3_IRQHandler(void)
{      
  if((TIM3CH1_CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ
{
  if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//Òç³ö Checks whether the TIM interrupt has occurred or not
  {      
   if(TIM3CH1_CAPTURE_STA&0X40)//ÒѾ ²¶»ñµ½¸ßµçƽÁË
   {
    if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË
    {
     TIM3CH1_CAPTURE_STA|=0X80;  //±ê¼Ç³É¹¦²¶»ñÁËÒ»´Î
     TIM3CH1_CAPTURE_VAL=0XFFFF;//??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
    }else TIM3CH1_CAPTURE_STA++;
   }  
  }
  if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//²¶»ñ2·¢Éú²¶»ñʼþ
  {
   if(TIM3CH1_CAPTURE_STA&0X40)  //²¶»ñµ½Ò»¸öϽµÑØ   
   {      
    TIM3CH1_CAPTURE_STA|=0X80;  //±ê¼Ç³É¹¦²¶»ñµ½Ò»´Î¸ßµçƽÂö¿í
     TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ.
     TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ
   }else          //»¹Î´¿ªÊ¼,µÚÒ»´Î²¶»ñÉÏÉýÑØ
   {
    TIM3CH1_CAPTURE_STA=0;   //Çå¿Õ
    TIM3CH1_CAPTURE_VAL=0;
    TIM3CH1_CAPTURE_STA|=0X40;  //±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ
    TIM_Cmd(TIM3,DISABLE );  //¹Ø±Õ¶¨Ê±Æ33
     TIM_SetCounter(TIM3,0);
     TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);  //CC1P=1 ÉèÖÃΪϽµÑز¶»ñ
    TIM_Cmd(TIM3,ENABLE );  //ʹÄܶ¨Ê±Æ÷3
   }      
  }                     
  }
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_Update); //Çå³ýÖжϱê־λ
}


下面是3的初始化程序
void TIM3_CH1_Cap_Init(u16 arr,u16 psc)/////////////////////////////////////////////////////////////////////////////////////////////
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO³õʼ»¯GPIO Init structure definition  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//This structure is used with all TIMx except for TIM6 and TIM7
NVIC_InitTypeDef NVIC_InitStructure;// NVIC Init Structure definition  

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);   //TIM5ʱÖÓʹÄÜ    2-5,12-14¶¼¿ÉÒÔÓÃ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);  //ʹÄ&UumlORTAʱÖÓ A-KʱÖÓ

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //ÏÂÀ
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»&macrA0GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices
GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_TIM3); //PA0¸´ÓÃλ¶¨Ê±Æ÷
  
   
TIM_TimeBaseStructure.TIM_Prescaler=psc;  //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_Period=arr;   //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//TIMx: where x can be  1 to 14 to select the TIM peripheral

//³õʼ»¯TIM3ÊäÈ벶»ñ²ÎÊý
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01  Ñ¡ÔñÊäÈë¶Ë IC1Ó³Éäµ½TI1ÉÏ
  TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //ÉÏÉýÑز¶»ñ
  TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Ó³Éäµ½TI1ÉÏ
  TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
  TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃÊäÈëÂ˲¨Æ÷ ²»Â˲¨
  TIM_ICInit(TIM3, &TIM3_ICInitStructure);//TIM2¡¢3¡/4¡/5¿ÉÒÔ¹²ÓÃ
  
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔÊÐí¸üÐÂÖÐ¶Ï ,ÔÊÐíCC1IE²¶»ñÖжÏ

  TIM_Cmd(TIM3,ENABLE );  //ʹÄܶ¨Ê±Æ÷

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;  //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷¡¢


}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。