各位坛友,有没有使用过STM8L151 timer2的ETR(PB3)来计数。小弟最近配置使用没有成功,update中断或计数中断都没有触发。请大家帮忙看看问题出在哪里?多谢!
main函数
void main()
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); //内部时钟为1分频 = 16Mhz
TIM2_Init();
while(1)
{
}
}
timer2 的初始化
void TIM2_Init()
{
GPIO_Init(GPIOB , GPIO_Pin_3 , GPIO_Mode_In_PU_No_IT); //PB3为timer2 的外部触发入口,上拉输入无中断
TIM2_DeInit(); //复位TIME2
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2,ENABLE); //开启TIME2的时钟
TIM2_TimeBaseInit(TIM2_Prescaler_1,TIM2_CounterMode_Up,255);//不分频 ,计数 65535
TIM2_ETRClockMode2Config(TIM2_ExtTRGPSC_OFF,TIM2_ExtTRGPolarity_NonInverted,0);//没有外部触发预分频器,高电平或上升沿有效,使能外部时钟模式2
TIM2_SelectInputTrigger(TIM2_TRGSelection_ETRF); //选择外部触发
TIM2_ARRPreloadConfig(ENABLE);
TIM2_ClearFlag(TIM2_FLAG_Update);
TIM2_ITConfig(TIM2_IT_Update,ENABLE); //打开更新中断
TIM2_ClearFlag(TIM2_IT_Trigger);
TIM2_ITConfig(TIM2_IT_Trigger,ENABLE); //打开触发中断
TIM2_SetCounter(0);
TIM2_Cmd(ENABLE);
}
中断函数
extern u8 ul;
INTERRUPT_HANDLER(TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler,19)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if(TIM2_GetITStatus(TIM2_IT_Update) != RESET)
{
ul++;
TIM2_ClearITPendingBit(TIM2_IT_Update);
}
}
/**
* @brief Timer2 Capture/Compare / USART2 RX Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(TIM2_CC_USART2_RX_IRQHandler,20)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if(TIM2_GetITStatus(TIM2_IT_Update) != RESET)
{
ul++;
TIM2_ClearITPendingBit(TIM2_IT_Update);
}
}
|