I have to say that STMicroelectronics is really a bit of a flirt! Even a bit of a pervert. I am not flattered by the level of editing of the ST document stm32f10xxx reference Manual. Many of the manuals are vague, and even a lot of important to the beginner of the place is a stroke, let a person really can not touch the head. Like I said earlier about the introduction of NVIC nested vector interrupt Controller, this Part I think is very important, but when you read his part of the introduction, you do not set the interrupt service program, he has any registers do not know, let alone set up,NVIC The detailed introduction is in the cotex-m3 in the detailed introduction, does not say much. Today we are talking about the systick timer.
The Systick timer is the same as I said above and is not described in the manual. I took Dickens to find the English version of the Systick timer in a horn Glarey. In COTEX-M3 has introduced, why want to find STM32 introduction, is because the function set up also a little difference. First look at the role of the Systick timer, the following is a passage in cotex-m3:
The Systick timer is bundled in the Nvic to generate a Systick exception (exception number: 15). In the past, most operating systems needed a hardware timer to generate tick breaks required by the operating system as the base-times for the entire system. For example, for multiple tasks to allow a different number of time slices, to ensure that no one task can occupy the system, or a certain time range of each timer cycle to specific tasks, as well as the operating system provides a variety of timing functions, are related to this tick timer. Therefore, a timer is required to generate periodic interrupts, and it is best to allow the user program to have no discretionary access to its registers to maintain the rhythm of the operating system's "heartbeat".
A simple timer is included inside the CORTEX‐M3 processor. Because all CM3 chips have this timer, the porting of software between different CM3 devices is simplified. The clock source for this timer can be the internal clock (the free-running clock on the fclk,cm3), or the external clock (the STCLK signal on the CM3 processor). However, the specific source of STCLK is determined by the chip designer, so the clock frequency between the different products may vary greatly, and you need to examine the device Manual of the chip to decide what to choose as the clock source. (Know why I'm looking for St's instructions on Systick).
part of the content belongs to nvic control section, total
stk_load, 0xe000e014 -- Reload Register
current value Register
calibration value Register
First look at the STK_CSR Control Register: There are 4 digit T in the Register meaning
No. 0 bit: Enable,systick Enable bit (0: Turn off Systick function; 1: Turn on Systick function)
1th bit: Tickint,systick interrupt Enable bit (0: Shutdown Systick interrupt; 1: Turn on Systick interrupt)
2nd bit: Clksource,systick Clock Source Selection (0: Use HCLK/8 as Systick clock; 1: Use HCLK as Systick clock)
3rd bit: Countflag,systick count comparison flag, if the SysTick has been counted to 0 after the last read of this register, the bit is 1. If the bit is read, the bit will automatically clear 0
Stk_load Reload Register:
The Systick is a descending timer, and when the timer decrements to 0 , the value in the Reload register is reloaded and continues to start decreasing. The stk_load heavy-duty Register is a maximum count of 0xFFFFFFregisters.
stk_val Current value register:
It is also a 24-bit register that returns the value of the current inverted count when read, writes it to zero, and clears the COUNTFLAG flag in the Systick control and status register.
STK_CALRB Calibration Value Register:
This register as if the current level I can not use, the general meaning to understand the point, the English explanation put it:
Bit to noref:1= no external reference clock (stclk unavailable) 0 = external reference clock available
Digit skew:1= calibration value is not accurate 1ms 0 = calibration value is accurate 1ms
Bit [23:0]: Calibration value
Indicates the calibration value when the SysTick counter runs on HCLK MAX/8 as external clock. The value is product dependent, refer to the product Reference Manual, and SysTick calibration Value section. When HCLK are programmed at the maximum frequency, the SysTick period is 1ms. If calibration Information is isn't known, calculate the calibration value required from the frequency of the processor Cloc K or external clock.
In addition to serving the operating system, the SysTick timer can be used for other purposes: as an alarm, for measuring time, etc. It is important to note that the Systick timer will also be suspended when the processor is called to stop during commissioning (Halt).
Here we apply the Systick timer to bare-Ben, to use it as a timer, or a stereotype, add a definition register in the register header file:
//*****************************************************************
* Systemtick-register
//*******************************************************************
#define SYSTICK_TENMS (* (volatile unsigned long *) 0xe000e01c)
#define SYSTICK_CURRENT (* (volatile unsigned long *) 0xe000e018)
#define SYSTICK_RELOAD (* (volatile unsigned long *) 0xe000e014)
#define SYSTICK_CSR (* (volatile unsigned long *) 0xe000e010)
To configure the Systick register:
void Systick_configuration (void)
{
systick_current=0; Current Value Register
systick_reload=20000; Reload register, System clock 20M interrupt once 1mS
systick_csr|=0x06;//HCLK as SYSTICK clock, SYSTICK interrupt enable bit
}
Interrupt handling:
void Systick_handler (void)//Interrupt function
{
extern unsigned long timingdelay; Delay time, note defined as global variable
systick_current=0;
if (timingdelay! = 0x00)
timingdelay--;
}
Using the Systick delay function:
unsigned long timingdelay; Delay time, note defined as global variable
void delay (unsigned long ntime)//Delay function
{
systick_csr|=0x07; Enable Systick counter
Timingdelay = Ntime; Read delay time
while (Timingdelay! = 0); Determine if the delay is over
systick_csr|=0x06;//Closing the SYSTICK counter
}
int main ()
{
SystemInit0 (); System (Clock) initialization
Stm32_gpiosetup (); Gpio initialization
Systick_configuration (); Configuring the Systick Timer
while (1)
{
gpio_portb_odr|= (1<<5);
Delay (1000); 1S
gpio_portb_odr&=~ (1<<5);
Delay (1000); 1S
}
}
Complete! Delay (1000), the realization of a 1S accurate delay, the use of delay (unsigned long ntime), with the Systick timer can achieve any time accurate delay, of course, through the timer TIMX is also possible to do so, I just use it to illustrate the use of the Systick timer.
STM32 Learning Note 9 (Systick tick clock)