Systick system timer tick

A 24-bit count-down timer, the count to zero, from the auto-reload register RELOAD timing of the initial value. Unless it makes the SysTick Control and status register bit cleared, it does not stand still, can work even in the sleep mode.

 

Four registers:

CTRL SysTick Control and Status Register 

LOAD SysTick auto-reload register value addition 

VAL SysTick Current Value Register 

CALIB SysTick Calibration Value Register

 

 

The clock source selection CTRL:

void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)

Parameters can fill: SYSTICK_CLKSOURCE_HCLK and SYSTICK_CLKSOURCE_HCLK_DIV8 eight or poor, regardless of frequency

 

After one cycle of ticks interrupt occurs (interrupt opening provided clock source enable SYSTICK set load value)

__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  {
    return (1UL);                                                   /* Reload value impossible */
  }

  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */
}

 

 

// used to store the system clock frequency 
static U32 fac_us = 0 ;                             // US delay times multiplier 



// initialization delay function
 // When ucos when ucos This function initializes the clock pulse
 @ SYSTICK fixed clock AHB clock 1/8
 // the SYSCLK: system clock frequency 
void delay_init (U8 the SYSCLK) 
{ 
    HAL_SYSTICK_CLKSourceConfig (SYSTICK_CLKSOURCE_HCLK); // the SysTick frequency HCLK 
    fac_us = the SYSCLK;                             // regardless of whether OS, fac_us need to use 
}     


// casting when nus
 // nus is the number of us want to delay.    
 // Note: the value of not more than nus 1000us 
void delay_us (U32 nus) 
{        
    ticks U32; 
    U32 Told, Tnow, TCNT = 0 ; // TCNT last used to store the current number of cycles used and ticks Comparative 
    U32 reload ; = SysTick-> LOAD                 // will be reset after the LOAD value reload value to achieve this number counter   	           
    ticks = NUS * fac_us;                          // required number of beats 
    Told = SysTick-> VAL;                         // counter value VAL when just entering into Told stored in 
    the while ( . 1 ) 
    { 
        Tnow = SysTick-> VAL;    
         IF (Tnow =! told) // case has not been overloaded 
        {         
            IF (Tnow <Told) + = Told-TCNT Tnow;     //Here note is a down counter SYSTICK it. 
            The else TCNT reload-tnow + + = Told; // Told <overload situation after tnow 
            Told = tnow;
             IF (TCNT> = ticks) BREAK ;             // time than / equal to to delay time, exit. 
        }   
    }; 
} 
 
// delay NMS
 @ NMS: ms delay to the number of 
void Delay_ms (U16 NMS) 
{ 
    U32 I; 
    for (I = 0 ; I <NMS; I ++) delay_us ( 1000 ); 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

IF (! Tnow = Told) {     case if (tnow <told) // not yet overloaded { TCNT + = Told-Tnow; // this is a note SYSTICK down counter on it. } the else // Told <overload situation after tnow { TCNT + = Told + reload-tnow;     } Told = tnow; IF (TCNT> = ticks) BREAK; // time than / equal to the delay time, then exits. }  

Guess you like

Origin www.cnblogs.com/qifeng1024/p/12052284.html