当前位置:网站首页>STM32 system timer flashing LED
STM32 system timer flashing LED
2022-07-07 04:50:00 【DS. Fu Hongxue】
SysTick Register introduction
SysTick Register summary
| Register name | Register description |
|---|---|
| CTRL | SysTick Control and status register |
| LOAD | SysTick Reload the load value register |
| VAL | SysTick Current value register |
| CALIB | SysTick Calibration value register |
①SysTick Control and status register
| Bit segment | name | type | reset value | describe |
|---|---|---|---|---|
| 16 | COUNTFLAG | R/W | 0 | If after last reading this register ,SysTick It has been counted 0, The bit is 1 |
| 2 | CLKSOURCE | R/W | 0 | Clock source selection bit ,0=AHB/8,1= Processor clock AHB |
| 1 | TICKINT | R/W | 0 | 1:SysTick Count down to 0 Time production SysTick Exception request ,0: Count to 0 No action |
| 0 | ENABLE | R/W | 0 | SysTick Enable bit of timer |
②SystTick Reload the load value register
| Bit segment | name | type | reset value | describe |
|---|---|---|---|---|
| 23:0 | RELOAD | R/W | 0 | When count down to 0 when , Values to be reloaded |
③SysTick Current value register
| Bit segment | name | type | reset value | describe |
|---|---|---|---|---|
| 23:0 | CURRENT | R/W | 0 | When reading, return the value of the current reciprocal count , Writing it clears it , At the same time, it will also clean up in SysTick Control and status register COUNTFLAG sign |
SysTick Timing experiments
Programming points
1) Set the value of the reload register .
2) Clear the value of the current value register .
3) Configure control and status registers .
bsp_led.c file
#include "./led/bsp_led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( LED1_GPIO_CLK | LED2_GPIO_CLK | LED3_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LED2_GPIO_PIN;
GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LED3_GPIO_PIN;
GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);
GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);
GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);
GPIO_SetBits(LED3_GPIO_PORT, LED3_GPIO_PIN);
}
bsp_led.h file
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
#define LED1_GPIO_PORT GPIOB
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOB
#define LED1_GPIO_PIN GPIO_Pin_5
#define LED2_GPIO_PORT GPIOB
#define LED2_GPIO_CLK RCC_APB2Periph_GPIOB
#define LED2_GPIO_PIN GPIO_Pin_0
#define LED3_GPIO_PORT GPIOB
#define LED3_GPIO_CLK RCC_APB2Periph_GPIOB
#define LED3_GPIO_PIN GPIO_Pin_1
#define ON 0
#define OFF 1
#define LED1(a) if (a) \ GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);\ else \ GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN)
#define LED2(a) if (a) \ GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PIN);\ else \ GPIO_ResetBits(LED2_GPIO_PORT,LED2_GPIO_PIN)
#define LED3(a) if (a) \ GPIO_SetBits(LED3_GPIO_PORT,LED3_GPIO_PIN);\ else \ GPIO_ResetBits(LED3_GPIO_PORT,LED3_GPIO_PIN)
#define digitalHi(p,i) {
p->BSRR=i;}
#define digitalLo(p,i) {
p->BRR=i;}
#define LED1_TOGGLE digitalToggle(LED1_GPIO_PORT,LED1_GPIO_PIN)
#define LED1_OFF digitalHi(LED1_GPIO_PORT,LED1_GPIO_PIN)
#define LED1_ON digitalLo(LED1_GPIO_PORT,LED1_GPIO_PIN)
#define LED2_TOGGLE digitalToggle(LED2_GPIO_PORT,LED2_GPIO_PIN)
#define LED2_OFF digitalHi(LED2_GPIO_PORT,LED2_GPIO_PIN)
#define LED2_ON digitalLo(LED2_GPIO_PORT,LED2_GPIO_PIN)
#define LED3_TOGGLE digitalToggle(LED3_GPIO_PORT,LED3_GPIO_PIN)
#define LED3_OFF digitalHi(LED3_GPIO_PORT,LED3_GPIO_PIN)
#define LED3_ON digitalLo(LED3_GPIO_PORT,LED3_GPIO_PIN)
void LED_GPIO_Config(void);
#endif /* __LED_H */
bsp_SysTick.h file
#ifndef __SYSTICK_H
#define __SYSTICK_H
#include "stm32f10x.h"
void SysTick_Init(void);
void Delay_us(__IO u32 nTime);
#define Delay_ms(x) Delay_us(100*x) // Company ms
void SysTick_Delay_Us( __IO uint32_t us);
void SysTick_Delay_Ms( __IO uint32_t ms);
#endif /* __SYSTICK_H */
bsp_SysTick.c file
#include "bsp_SysTick.h"
#include "core_cm3.h"
#include "misc.h"
static __IO u32 TimingDelay;
/* Start the system tick timer */
void SysTick_Init(void)
{
/* SystemCoreClock / 1000 1ms Break once * SystemCoreClock / 100000 10us Break once * SystemCoreClock / 1000000 1us Break once */
if (SysTick_Config(SystemCoreClock / 100000))
{
/* Capture error */
while (1);
}
}
/* Delay_us(1): Realization 1x10us = 10us Time delay */
void Delay_us(__IO u32 nTime)
{
TimingDelay = nTime;
// Enable tick timer
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
while(TimingDelay != 0);
}
// Get the beat program , stay SysTick Interrupt function SysTick_Handler() Call in
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
#if 0
// This firmware library function is in core_cm3.h in
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
// reload The register is 24bit, The maximum value is 2^24
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1);
// To configure reload Initial value
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;
// Configure interrupt priority as :1<<4-1 = 15, The priority is the lowest
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
// To configure counter The value of the counter
SysTick->VAL = 0;
// To configure systick The clock is 72M
// Interrupt enable
// Can make systick
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk|SysTick_CTRL_TICKINT_Msk|SysTick_CTRL_ENABLE_Msk;
return (0);
}
#endif
// couter Minus one is equal to 1/systick_clk
// When counter from reload The value of is reduced to 0 When , For a cycle , If interrupt is turned on, the interrupt service program is executed .
// meanwhile CTRL Of countflag Position meeting position 1
// The time of this cycle is :reload * (1/systick_clk)
void SysTick_Delay_Us( __IO uint32_t us)//SysTick Subtle level delay
{
uint32_t i;
SysTick_Config(SystemCoreClock/1000000);
for(i=0;i<us;i++)
{
// When the value of the counter is reduced to 0 When ,CRTL Bit of register 16 I'll set 1
while( !((SysTick->CTRL)&(1<<16)) );
}
// close SysTick Timer
SysTick->CTRL &=~SysTick_CTRL_ENABLE_Msk;
}
void SysTick_Delay_Ms( __IO uint32_t ms)//SysTick Millisecond delay
{
uint32_t i;
SysTick_Config(SystemCoreClock/1000);
for(i=0;i<ms;i++)
{
// When the value of the counter is reduced to 0 When ,CRTL Bit of register 16 I'll set 1
// Dangzhi 1 when , Reading this bit clears 0
while( !((SysTick->CTRL)&(1<<16)) );
}
// close SysTick Timer
SysTick->CTRL &=~ SysTick_CTRL_ENABLE_Msk;
}
main.c file
#include "stm32f10x.h"
#include "bsp_SysTick.h"
#include "bsp_led.h"
int main(void)
{
/* LED Port initialization */
LED_GPIO_Config();
/* To configure SysTick by 10us Break once */
SysTick_Init();
#if 0
for(;;)
{
LED1( ON );
Delay_us(100000); // 100000 * 10us = 1000ms
//Delay_ms(100);
LED1( OFF );
LED2( ON );
Delay_us(100000); // 100000 * 10us = 1000ms
//Delay_ms(100);
LED2( OFF );
LED3( ON );
Delay_us(100000); // 100000 * 10us = 1000ms
//Delay_ms(100);
LED3( OFF );
}
#else
for(;;)
{
LED1( ON );
SysTick_Delay_Ms( 1000 );
LED1( OFF );
LED2( ON );
SysTick_Delay_Ms( 1000 );
LED2( OFF );
LED3( ON );
SysTick_Delay_Ms( 1000 );
LED3( OFF );
}
#endif
}
Running results

边栏推荐
- You can't sell the used lithography machine to China! The United States unreasonably pressured the Dutch ASML, and domestic chips were suppressed again
- sscanf,sscanf_ S and its related usage "suggested collection"
- 窗口可不是什么便宜的东西
- A simple and beautiful regression table is produced in one line of code~
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- offer如何选择该考虑哪些因素
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
- AI 落地新题型 RPA + AI =?
- Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
- Run the command once per second in Bash- Run command every second in Bash?
猜你喜欢

窗口可不是什么便宜的东西

Digital chemical plant management system based on Virtual Simulation Technology

Vscode automatically adds a semicolon and jumps to the next line

acwing 843. N-queen problem

【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ

AI landing new question type RPA + AI =?

JS variable plus

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

Analyse approfondie de kubebuilder

In depth analysis of kubebuilder
随机推荐
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
JS form get form & get form elements
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
程序员上班摸鱼,这么玩才高端!
Intel David tuhy: the reason for the success of Intel aoten Technology
What work items do programmers hate most in their daily work?
Canteen user dish relationship system (C language course design)
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
抖音或将推出独立种草社区平台:会不会成为第二个小红书
What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
acwing 843. N-queen problem
JS also exports Excel
Common methods of list and map
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
JDBC link Oracle reference code
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
Analyse approfondie de kubebuilder
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together