SysTick_ system timer to realize running light

Publisher:郑大大Latest update time:2019-02-18 Source: eefocusKeywords:SysTick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

References: ARM Cortex™-M4F Technical Reference Manual - Chapter 4.5 SysTick Timer (STK)


SysTick (system tick timer) is essentially a timer embedded in NVIC. It is a peripheral in the kernel and a 24-bit down-counting counter. The counter is reduced by 1 every 1/SYSCLK. When the value of the register is reduced to 0, a (hardware) interrupt will be generated, also called a tick interrupt. The main purpose is 1. to generate accurate delays. 2. Generally used in operating systems to generate time bases, maintain the heartbeat of the operating system and provide a separate heartbeat (clock) beat for the operating system. For an introduction to SyeTick, I recommend reading this great blog https://blog.csdn.net/yx_l128125/article/details/7884423.


There are four SysTick registers in total. When using SysTick to generate timing, you only need to configure the first three registers, and the last calibration value register does not need to be used.



The following is a description of each register


The SysTick control and status register CTRL is mainly used to read the value in the current numerical register, select the clock source, enable exception requests, and enable the timer.



The SysTick reload value register LOAD is mainly used to store the count value, the maximum



SysTick current value register VAL, each counter is reduced by 1 every 1/SYSCLK. When it is reduced to 0, the count flag is set to 1, and then the value in the reload value register is written into the current value register, and the count flag is cleared.


Sys calibration value register CALIB



The above is the introduction of SysTick. Next, we need to operate it.


We need to use SysTick to time the LED light status to change once every 1S, so


1. First, you should configure the SysTick related registers to generate a 1s delay


2. Initialize LED lights


3. Write the main function


Since Systick is embedded in the kernel, only LED modules are needed in the hardware circuit. If you want to use SysTick to switch the state of the running light once every 1s, you must first configure SysTick. Here we directly use the STM32 official library function SysTick_Config (uint32_t ticks). In the kernel header file core_cm4.h, the function of this function is to configure the system timer and interrupt, and turn on SysTick, so that the counter is in free counting mode to generate a periodic interrupt. In this function, the parameter ticks is used to set the value of the reload register, which cannot exceed the value of the reload register. When the value of the reload register is reduced to 0, an interrupt will be generated, and then the value of the reload register is written into the current counter to count down again, and this cycle will continue. Next, this library function configures the interrupt priority for the interrupt (for interrupt priority, you can see the STM32 interrupt and NVIC overview), and then assigns the initial value to the current value register, then configures the clock source, enables interrupts, and turns on the timer. When it returns 0, it means that the function has been implemented, and returning 1 indicates failure. This initialization function mainly configures three registers in SysTick: LOAD, VAL, CTRL, and also calls NVIC_SetPriority() to configure the interrupt priority function.



Because SysTick is a peripheral in the core, the clock selection is HCLK180MHz clock



Since there is no official initialization function for SysTick, only the configuration function Config, we simply write a SysTick_Init() function ourselves



Because HCLK is 180MHz, when ticks=SystemFrequency/1000 (180 000 000/1000), the value of ticks decreases by 1 every time 1/SystemFrequency passes, so the counting time is ticks/(1/SystemFrequency)=1ms, so we configure SysTick to interrupt once every 10us.


After configuring SysTick, we only need to define a variable to record the number of interrupts N. N*SysTick is equal to the time we want to time.



nTime is the number of times I enter the SysTick interrupt. Every time I enter the interrupt service function, TimingDelay decreases by 1. When TimingDelay is not equal to 0, the delay ends.


The interrupt service function is as follows



Now that we have described the SysTick configuration, let's initialize the LED light.


I will not go into details about the initialization of the LED. You can refer to the STM32 firmware library function to light up the LED. The code is as follows


#include "stm32f4xx_gpio.h"

 

#define GPIO_R_Pin  GPIO_Pin_10

#define GPIO_R_Port GPIOH

 

#define GPIO_G_Pin  GPIO_Pin_11

#define GPIO_G_Port GPIOH

 

#define GPIO_B_Pin  GPIO_Pin_12

#define GPIO_B_Port GPIOH

 

/****************************Macro to control the LED light on and off***************/

 

/*Directly operate registers to control IO*/

 

#define LED_PORT_OUT_HI(p,i) { p->BSRRL = i ;} //Output is high level

#define LED_PORT_OUT_LO(p,i) { p->BSRRH = i ;} //Output is low level

#define LED_PORT_OUT_Toggle(p,i) { p->BSRRL ^= i ;} // Output is inverted

 

/*Define macros to control IO*/

 

#define LED_R_Toggle         LED_PORT_OUT_Toggle(GPIO_R_Port,GPIO_R_Pin)

#define LED_R_ON LED_PORT_OUT_LO(GPIO_R_Port,GPIO_R_Pin)

#define LED_R_OFF LED_PORT_OUT_HI(GPIO_R_Port,GPIO_R_Pin)

 

#define LED_G_Toggle         LED_PORT_OUT_Toggle(GPIO_G_Port,GPIO_G_Pin)

#define LED_G_ON LED_PORT_OUT_LO(GPIO_G_Port,GPIO_G_Pin)

#define LED_G_OFF LED_PORT_OUT_HI(GPIO_G_Port,GPIO_G_Pin)

 

#define LED_B_Toggle         LED_PORT_OUT_Toggle(GPIO_B_Port,GPIO_B_Pin)

#define LED_B_ON LED_PORT_OUT_LO(GPIO_B_Port,GPIO_B_Pin)

#define LED_B_OFF LED_PORT_OUT_HI(GPIO_B_Port,GPIO_B_Pin)

 

#define LED_RGBOFF   LED_R_OFF;\

LED_G_OFF;\

LED_B_OFF

 

void LED_GPIO_Config(void);

#include "bsp_led.h"

 

/**

  * @brief Initialize the IO to control the LED

  * @param None

  * @retval None

  */

void LED_GPIO_Config(void)

{

    /*Define a GPIO_InitTypeDef type structure*/

    GPIO_InitTypeDef GPIO_InitStructure;

 

    /*Turn on LED related GPIO peripheral clock*/

    RCC_AHB1PeriphClockCmd ( LED1_GPIO_CLK|

                     LED2_GPIO_CLK|

                     LED3_GPIO_CLK, ENABLE); 

 

    /*Select the GPIO pin to control*/    

    GPIO_InitStructure.GPIO_Pin = LED1_PIN;

 

    /*Set the pin mode to output mode*/

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;   

    

    /*Set the output type of the pin to push-pull output*/

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    

    /*Set the pin to pull-up mode*/

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

 

    /*Set the pin rate to 2MHz */   

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 

 

    /*Call library function to initialize GPIO using GPIO_InitStructure configured above*/

    GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);

    

    /*Select the GPIO pin to control*/    

    GPIO_InitStructure.GPIO_Pin = LED2_PIN;

    GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);

    

    /*Select the GPIO pin to control*/    

    GPIO_InitStructure.GPIO_Pin = LED3_PIN;

    GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);


/*Turn off the RGB light*/

LED_RGBOFF;

}

After configuring SysTick and LED, let's start writing the main function


The main function is as follows


int main(void)

{

/* LED port initialization */

LED_GPIO_Config();  

 

        /* Configure SysTick to interrupt once every 10us, and trigger a timer interrupt after the time is up.

* Enter the SysTick_Handler process of the stm32fxx_it.c file and count the number of interrupts

*/

SysTick_Init();

  

while(1)

{

 

  LED_RED; 

  Delay_us(100000);    // 10000 * 10us = 1000ms

  //Delay 1s

  

  LED_GREEN;

  Delay_us(100000);

          //Delay 1s


  LED_BLUE;

  Delay_us(100000);

          //Delay 1s

 

 

}   

}

 


Keywords:SysTick Reference address:SysTick_ system timer to realize running light

Previous article:STM32_USART serial communication detailed explanation
Next article:STM32_EXTI external interrupt study notes

Recommended ReadingLatest update time:2024-11-16 14:42

SysTick_Config configures the systic interrupt interval
It is strange that the function Systic_Config (unit32_t ticks) cannot be defined in the mdk, but it compiles without any problems. However, this configuration is still quite useful. It can set the time to enter the systic interrupt to achieve precise delay and other functions. So I found the description of Systic_Conf
[Microcontroller]
SYSTICK timer, port multiplexing and remapping in STM32
Part 1 STM32SYSTICK timer: The systick timer is a simple timer as its name suggests, mainly used for delay to avoid wasting MCU resources. It is a 24-bit inverted counter. Inverted means decreasing from the maximum number to 0. The SysTick timer is bundled in the NVIC and is used to generate a SYSTICK exception, that
[Microcontroller]
STM32f103RBT6--SysTick usage
SysTick is a system tick clock, which can be configured in three steps. 1. SysTick initialization void SysTick_Configuration(void) {        /*Select HCLK as the SysTick clock source*/       SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);        /*Set SysTick to priority 3 */       NVIC_SystemHandlerPriorityCon
[Microcontroller]
STM32 SysTick precise delay (non-interrupt mode)
SysTick is a 24-bit timer built into the Cortex-M core, so we can see its register definition and configuration function SysTick_Config() in the core_cm3.h or core_cm4.h header file. SysTick's function is very simple, it only provides a time base function and supports interrupt requests, so it has very few registers.
[Microcontroller]
STM32F4_SYSTICK timer
The description of the SYSTICK timer is not mentioned in the STM32F4 reference manual, because this timer belongs to the Cortex core. In the "Cortex M3 Authoritative Guide", you can see its detailed description at the end of Chapter 8, "NVIC and Interrupt Control", as follows. //---------------------------------------
[Microcontroller]
STM32F4_SYSTICK timer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号