用中断测频率,为什么测不出5v,400hz的方波信号?

2019-08-16 21:14发布

主函数
int main(void)
{
  
u32 temp=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é2
delay_init();   //ÑÓʱº¯Êý³õʼ»¯
uart_init(9600);     //9600
  EXTIX_Init();
  TIM3_Int_Init(9999,7199);  
LED_Init();     //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
  TIM1_PWM_Init(200000,0);    //²»·ÖƵ¡&poundWMƵÂÊ=72000/(899+1)=80Khz
  //TIM2_Cap_Init(0XFFFF,72-1);  //ÒÔ1MhzµÄƵÂʼÆÊý
    while(1)
{
   delay_ms(100);
  TIM_SetCompare1(TIM1,TIM_GetCapture1(TIM1)+1);
  if(TIM_GetCapture1(TIM1)==300)TIM_SetCompare1(TIM1,0);   
  temp=allnum;
    if(temp!=0)
  {
   printf("´ÎÊý:%d ´Î ",temp);
   allnum=0;
  }
  
  
   }
}



————————————————————————————————————————————————————————


void EXTIX_Init(void)
{
EXTI_InitTypeDef  EXTI_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);

EXTI_InitStructure.EXTI_Line=EXTI_Line6;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);


   NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;   //ʹÄÜ°´¼üËùÔÚµÄÍⲿÖжÏͨµÀ
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶2
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;     //×ÓÓÅÏȼ¶1
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;        //ʹÄÜÍⲿÖжÏͨµÀ
   NVIC_Init(&NVIC_InitStructure);  
}
  u16 countnum=0;
void EXTI9_5_IRQHandler()
{

countnum++;

EXTI_ClearITPendingBit(EXTI_Line6);
}

void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

  TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏÈÕ¼ÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //´ÓÓÅÏȼ¶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure);  //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷

TIM_Cmd(TIM3,ENABLE);

}
u16 allnum=0;
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)

{
  allnum=countnum;
  countnum=0;
  TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
  
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。