The SysTick timer is integrated into the NVIC. Therefore, any microcontroller with a Cortex-M3 core has it. This study note uses the SysTick timer to implement the revolving lantern function.
The SysTick timer is very simple, with only four registers. The meaning of these four registers is very clear in the book "Cortex-M3 Definitive Guide", so I won't repeat it here. Let's just talk about what's special about SysTick on STM32. According to the CMSIS standard, the register names used when accessing these four registers in C language are as follows:
SysTick->CTRL
SysTick->LOAD
SysTick->VAL
SysTick->CALIB
The value of SysTick->CALIB is fixed to 9000, so a 1ms time base is generated only when the system tick clock is set to 9MHz (the maximum value of HCLK/8).
STM32 provides 2 clock sources:
0: AHB/8
1: Processor clock (AHB)
Therefore, SysTick->CTRL = 7 means using the processor clock as the clock source, enabling SysTick, and enabling the SysTick interrupt. When SysTick->CTRL = 3, the frequency is reduced to 1/8 of the original.
There are four LEDs on my development board, corresponding to PD2, PD3, PD4 and PD7 of GPIO port D.
The following is an example program, which still sets the registers directly first.
#include "stm32f10x.h"
#define RCC_GPIO_LED RCC_APB2Periph_GPIOD
#define GPIO_LED_PORT GPIOD
#define GPIO_LED1 GPIO_Pin_2
#define GPIO_LED2 GPIO_Pin_3
#define GPIO_LED3 GPIO_Pin_4
#define GPIO_LED4 GPIO_Pin_7
#define GPIO_LED_ALL GPIO_LED1 |GPIO_LED2 |GPIO_LED3 |GPIO_LED4
void LED_Spark(void)
{
static int state = 0;
switch (state)
{
case 0:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED1);
state++;
break;
case 1:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED2);
state++;
break;
case 2:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED3);
state++;
break;
case 3:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED4);
state = 0;
break;
default:
state = 0;
break;
}
}
int main(void)
{
SystemInit();
RCC->APB2ENR |= 0x00000020;
GPIOD->CRL = 0x24422244; //PD2 PD3 PD4 PD7 Set to Output mode
SysTick->LOAD = 24000000/200;
SysTick->CTRL = 3;
for(;;)
{
}
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
static int count = 0;
count++;
if (count == 100)
{
LED_Spark();
count = 0;
}
}
Then there are examples of using the functions provided by the STM32 firmware library.
#include "stm32f10x.h"
#define RCC_GPIO_LED RCC_APB2Periph_GPIOD
#define GPIO_LED_PORT GPIOD
#define GPIO_LED1 GPIO_Pin_2
#define GPIO_LED2 GPIO_Pin_3
#define GPIO_LED3 GPIO_Pin_4
#define GPIO_LED4 GPIO_Pin_7
#define GPIO_LED_ALL GPIO_LED1 |GPIO_LED2 |GPIO_LED3 |GPIO_LED4
void LED_Spark(void)
{
static int state = 0;
switch (state)
{
case 0:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED1);
state++;
break;
case 1:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED2);
state++;
break;
case 2:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED3);
state++;
break;
case 3:
GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);
GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED4);
state = 0;
break;
default:
state = 0;
break;
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SystemInit();
SysTick_Config(SystemCoreClock/100);
/* Enable GPIOB, GPIOC and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE); //RCC_APB2Periph_AFIO
/* LEDs pins configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_LED_ALL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_LED_PORT, &GPIO_InitStructure);
for(;;)
{
}
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
static int count = 0;
count++;
if (count == 100)
{
LED_Spark();
count = 0;
}
}
It should be noted that if the SysTick_Config function is used to set the SysTick interrupt frequency, the clock source cannot be specified manually. The clock source used at this time is the core frequency.
SystemCoreClock is a global variable, and its value is the operating frequency of the kernel, but the prerequisite is to call the SystemInit() function to set the kernel frequency. If the kernel frequency is set by byte writing registers, the value of SystemCoreClock may not be correct.
Previous article:STM32 implements DAC output related settings
Next article:STM32 general timer---basic timing learning
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- F28335 control module
- EEWORLD University Hall----2019 DLP Innovation Conference
- ADI & Shijian will help you understand chemical analysis and analytical instrument application solutions. Answer questions and win prizes!
- Playing with Zynq Serial 6 - Building a virtual machine and Linux system environment
- [Raspberry Pi 3B+ Review] Open I2C peripheral interface & drive DS3231
- The generation, measurement and suppression of switching power supply ripples are all covered in one article!
- Circuit reference ground potential issues
- Recommended China chip + SI522 ultra-low power consumption 13.56M card reader chip, CI522 ultra-low cost 13.56M card reader chip
- It is said that the instructions executed by the ARM core are provided by SRAM. Are the instructions not read directly from FLASH?
- In-depth explanation of how to choose the inductor value for DC-DC boost converter