STM8L的定时器分:高档定时器TIM1,通用定时器TIM
今日咱们运用根底定时器TIM4做一个延时的使命,选用两种办法完成:查询和中止的办法。
1查询的办法便是查询状况寄存器中的UIF位(update interrupt flag)是否置位,首要代码:
void delay_ms(u16 n_ms)
{
/* Init TIMER 4 */
CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE);
/* Init TIMER 4 prescaler: / (2^6) = /64 */
TIM4->PSCR = 6;
/* HSI div by 1 –> Auto-Reload value: 16M / 64 = 1/4M, 1/4M / 1k = 250*/
TIM4->ARR = 250;
/* Counter value: 2, to compensate the initialization of TIMER*/
TIM4->CNTR = 2;
/* clear update flag */
TIM4->SR1 &= ~TIM4_SR1_UIF;
/* Enable Counter */
TIM4->CR1 |= TIM4_CR1_CEN;
while(n_ms–)
{
while((TIM4->SR1 & TIM4_SR1_UIF) == 0) ;
TIM4->SR1 &= ~TIM4_SR1_UIF;
}
/* Disable Counter */
TIM4->CR1 &= ~TIM4_CR1_CEN;
}