我想用 STM32F407 TM3 做一个计数器。PA7 引脚 当输入8个CLK 生产一个中断。上升沿触发。下面是程序。
TIM_TimeBaseInitTypeDef TIM3BaseInitDef;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/////////////¶¨Ê±Æ÷1ÅäÖÃ10s///////////////////////////////////////////////////////
TIM_DeInit(TIM5);//TIM1=APB2(Max84Mhz)
//////////¶¨Ê±Æ÷1ÅäÖÃ///////////////////////////////
//¼Æ»®:PSC = 14400 Ôò CK_CNT = 84Mhz/(84) =1us
// ARR = 50 000 2us * 10 =20u
TIM3BaseInitDef.TIM_Prescaler = 0x01; //0XFFFF; //Ô¤·ÖƵ PSC(0-65535)
TIM3BaseInitDef.TIM_CounterMode = TIM_CounterMode_Up; //¼ÆÊýģʽ
TIM3BaseInitDef.TIM_Period = 8; //×Ô¶¯ÖØÔØ ARR(0-65535)
TIM3BaseInitDef.TIM_ClockDivision = TIM_CKD_DIV1; //ʱÖÓ·ÖƵÒò×Ó1,2,4
TIM3BaseInitDef.TIM_RepetitionCounter = 0;//Ñ»·¼ÆÊý´ÎÊý(Ö»ÊÇTIM1,8ÓÐ)
TIM_TimeBaseInit(TIM3,&TIM3BaseInitDef);
TIM3->SR &= ~(1<<0);
////////////////////////////////////////////////////
//ʹÄÜÅäÖÃÖжÏ=>£ºÊ¹ÄܸüÐÂʼþÖжÏ
//×¢ÒâTIM_UpdateDisableConfig ÓÃDISABLEº¯Êý´¦²Å½«¶ÔӦΪÇåÁã¶ø´ïµ½Ê¹ÄÜ(UDIS=0:ʹÄܸüÐÂʼþ)
//TIM_UpdateDisableConfig(TIM5,DISABLE);//Enables or Disables the TIMx Update event.
TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 7);
TIM_ETRClockMode1Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,7);
TIM_SetCounter(TIM3, 0);
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE );//Enables or disables the specified TIM interrupts
TIM_Cmd(TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM3->SR &= ~(1<<0);
现在不产生中断 是不是我程序有问题。。 |