LESSON 04: CREATE ABSOLUTELY DELAY FUNCTION WITH SYSTEM TICK TIMER STM32F1

posted in: Microcontroller | 0

1. System ticks and applications

  • System tick is a 24-bit timer located in the ARM Cortex M core. This is a down counter, the task of the system timer is to count from the reload value loaded in the SysTick Reload Value Register (SYST_RVR) to zero. The processor performs a reload of the reload value and continues counting down at the next clock.
  • With this function of the system timer, we can configure and enable it to generate a periodic SysTick interrupt event every time the countdown reaches zero, thereby creating an absolute delay function that replaces the delay functions. relative using while and for loop before.

2. Programming with ST’s standard library

  • System tick interrupt configuration command
SysTick_Config(SystemCoreClock/1000);

Where SystemCoreClock is the operating frequency of the core and 1000 is the frequency to execute the interrupt service function.

  • 1 ms absolute delay function
void Delay_ms(uint32_t time)
{
 TimingDelay = time;						
 while(TimingDelay!=0);			

}
  • Sys_tick interrupt service routine
void SysTick_Handler(void)
{
 TimingDelay--;
}

This function will be executed every time the counter value drops to 0 and is identified by the overflow flag.

  • The program in main function uses absolute delay function to blink led
int main()
{
  SysTick_Config(SystemCoreClock/1000);	// cau hinh systick ngat moi 1ms
  GPIO_Config();
  while (1)
  {
   GPIO_WriteBit(GPIOC,GPIO_Pin_13,1);
   Delay_ms(1000);
   GPIO_WriteBit(GPIOC,GPIO_Pin_13,0);
   Delay_ms(1000);
  }
}

Link to download sample code

Leave a Reply

Your email address will not be published.