今天按照你先前跟我说的方法试了下,发现在触发后,计数器时一直计数的,而且TIF一直都是置位,加了给它复位也不行。是不是它只要检测到是相应电平就会置位呢? 捣腾了一天也没成功。晚上又尝试了输入捕获功能,发现可以实现我的要求,但是有一点问题,就是我设置了上升沿为有效的的电平转变。但是,在我实际测量时(我需要在上升沿过零点开始采样),发现在上升和下降沿均产生捕获,而且不是交替捕获,没有规律性。不知道是为什么?请香主帮我看看问题在哪儿? 程序: RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Configuration(void); void NVIC_Configuration(void);
int main(void) { RCC_Configuration(); /* NVIC Configuration */ NVIC_Configuration(); //电网频率检测Ua-1,对应PB6,用TIM4-CH1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 29;//21; TIM_TimeBaseStructure.TIM_ClockDivision = 0; //接收最小为:72M/(29+1)/65535 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //65535为ccr1/2中的值 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2; TIM_ICInitStructure.TIM_ICFilter = 0x1; TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1); TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); TIM_Cmd(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
while(1) {
} } void RCC_Configuration(void) {。。。}//为简便,此处省略 void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
void TIM4_IRQHandler(void) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); // StartSampling();//这是我的启动采样函数,在此处未列出。 }
|