[Stm32] systick

Source: Internet
Author: User

 

(1) Background
In traditional embedded system software, the following methods are usually used to implement the delay (n) function:
For (I = 0; I <= x; I ++ );
X --;Loop value corresponding to N milliseconds
For the stm32 series microprocessor, only dozens of ns are executed for a single command.
The X value of N milliseconds is very large, and due to the wide frequency of the system, it is difficult to calculate the exact value of the delay n milliseconds.
For the stm32 microprocessor, a new method needs to be re-designed to implement this function, so that it can be used in programs.
Delay (n ).

(2) Introduction to stm32 javasick
The kernel of the Cortex-M3 contains a clock. Systick is a 24-bit descending counter,
When the initial value is set and enabled, the Count value is reduced by 1 every time one system clock period passes. When the count is 0,
The specified ick counter automatically reinstalls the initial value and Continues counting. The internal countflag flag is set to trigger
Interrupt (if the interruption is enabled ).
In the stm32 application, the clock of the Cortex-M3 kernel is used as the timer clock, set every millisecond
Generates an interruption. In the interrupt processing function, minus N by one. In the delay (n) function, the system cyclically checks whether N is 0,
If the value is not 0, the system waits cyclically. If the value is 0, the clock of systick is disabled and the function is exited.
Note: The global variable timingdelay must be defined as volatile. The delay time does not match the system clock frequency.
Rate Change.

(3) javasick library files
How to Use the function library of St to use javasick
1. Call systick_countercmd () -- indicates that the counter cannot be used.
2. Call javasick_itconfig () -- the fault occurs when the fault occurs.
3. Call javasick_clksourceconfig () -- set the clock source of javasick.
4. Call javasick_setreload () -- set the unique loaded value.
5. Call systick_itconfig () -- enable systick interruption
6. Call systick_countercmd () -- enable the systick counter

(4) practical use of the eagick Project

The external crystal oscillator is 8 MHz, 9 multiplier, the system clock is 72 MHz, and the maximum frequency of systick is 9 MHz (maximum
Hclk/8). Under this condition, set the systick verification value to 9000 and set the systick clock
If it is set to 9 MHz, a time base value of 1 ms can be generated, that is, the interruption of 1 ms can be generated by ick.

Step 1: configure the RCC register and systick register

1 /************************************** ************************************* 2 * name: void rcc_configuration (void) 3 * function: the system clock is configured to 72 MHz, and the peripheral clock is configured with 4 * entry parameters: None 5 * exit parameters: None 6 * description: 7 * Call method: no 8 ************************************** * ***********************************/9 void rcc_configuration (void) {10 systeminit (); 11 rcc_apb2periphclockcmd (kernel | rcc_apb2periph_gpioc | rcc_apb2periph_gpiod | rcc_apb2periph_gpioa | kernel, enable); 12}

Write an RCC function to configure the system clock and peripheral clock enabling

1 If (clock ick_config (72000) // 1 ms at the time of clock cycle interruption for the Timer 2 {3/* capture Error */4 While (1); 5}

Call the library function javasick (72000) in the main function to initialize the system clock.

Step 2: configure the systick interrupt function
Here we define a static _ IO uint32_t timingdelay; global variable for analysis using the logic analyzer that comes with the Keil software.

1 void SysTick_Handler(void)2 {3   TimingDelay_Decrement();4 }

This is a wake ick interrupt trigger function, which calls a timingdelay_decrement () function, as follows:

1 /************************************** ************************************* 2 * name: void timingdelay_decrement (void) 3 * function: Get the beat program 4 * entry parameter: None 5 * exit parameter: None 6 * Description: 7 * Call method: no 8 ************************************** * ***********************************/9 void timingdelay_decrement (void) 10 {11 if (timingdelay! = 0x00) 12 {13 timingdelay --; 14} 15}

Timingdelay_decrement () is responsible for the global variable timingdelay minus 1 each time

Step 3: Compile the delay latency Function

1 /************************************** ************************************* 2 * name: void delay (_ IO uint32_t ntime) 3 * function: 1 ms for scheduled latency program 4 * entry parameter: None 5 * exit parameter: None 6 * description: 7 * Call method: no 8 ************************************** * ***********************************/9 void delay (_ IO uint32_t ntime) 10 {11 timingdelay = ntime; 12 while (timingdelay! = 0); 13}

Step 4: Call delay in the main function

1 While (1) 2 {3 gpio_setbits (gpiob, gpio_pin_8); // led1 bright 4 delay (500); // latency ms5 gpio_resetbits (gpiob, gpio_pin_8 ); // led1 off 6 delay (500); // latency}

Step 5: Simulation

Configuration 1: Software Simulation (hardware simulation on the right)

Set 2: Open the Waveform simulation interface

 

Set 3: import port

Settings 4: Execution and waveform Adjustment

Configuration 5: hardware simulation

Set 6: Tracing debugging

 

 

Resource Link: http://pan.baidu.com/s/1hqmTYhI

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.