目前在用STM8S的定时器3加外部引脚的中断来测量高低电平的时间。思路是:
1)外部引脚设置为高低电平都进入中断;
2)设置定时器时钟为1Mhz
3)在进入外部中断的时候读取定时器的计数值后清除重新计数。读取的计数值即是高电平/低电平时间,单位us
定时器的配置如下:
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER3,ENABLE);
TIM3_TimeBaseInit(TIM3_PRESCALER_16, 32000); // 1Mhz clock,32ms Cycle
TIM3_SetCounter(0);
TIM3_ITConfig(TIM3_IT_UPDATE, ENABLE);
TIM3_ClearFlag(TIM3_FLAG_UPDATE);
TIM3_Cmd(DISABLE);
中断服务程序中:
/**
* @brief External Interrupt PORTD Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if ((GPIO_ReadInputData(GPIOD) & GPIO_PIN_0) == 0x00)
{
TIM3_Cmd(DISABLE);
SignalHigh = TIM3_GetCounter();
TIM3_SetCounter(0);
TIM3_Cmd(ENABLE);
}
else
{
TIM3_Cmd(DISABLE);
SignalLow = TIM3_GetCounter();
TIM3_SetCounter(0);
TIM3_Cmd(ENABLE);
}
}
中断中读出来的时间偏差特别大,100us左右的电平读出来将近1700,不知道是什么原因 |