当前位置:网站首页>STM32 general timer 1s delay to realize LED flashing
STM32 general timer 1s delay to realize LED flashing
2022-07-03 09:53:00 【Fake iron man】
stm32 There are many timers , Each timer has different functions , Today I learned how to use general timer to realize 1s Time delay , send LED Light flashing , It is summarized as follows :
Step summary :
Enable timer clock -> Configure timer structure -> Clear the timer flag -> Start timer interrupt -> Enable timer -> Write interrupt service function
Timer.c:
#include "Timer.h"
void TimerBase_Config(void)
{
// Turn on the clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Define structure variables
TIM_TimeBaseInitTypeDef TimerBaseInitStructure;
NVIC_InitTypeDef NVICInitStructure;
// Configure timer structure
TimerBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TimerBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TimerBaseInitStructure.TIM_Period = 10000;
TimerBaseInitStructure.TIM_Prescaler = 7200;
TIM_TimeBaseInit(TIM2, &TimerBaseInitStructure);
// Clear flag bit
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
// Start timer interrupt
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
// Enable timer
TIM_Cmd(TIM2,ENABLE);
// To configure NVIC Structure
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVICInitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVICInitStructure.NVIC_IRQChannelCmd = ENABLE;
NVICInitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVICInitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVICInitStructure);
}
stm32f103x_it.c:
void TIM2_IRQHandler(void)
{
static uint16_t temp;
if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)
{
if(temp ++ %2 == 1)
{
GPIO_ResetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN);
}
else
{
GPIO_SetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN);
}
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
main.c:
#include "stm32f10x.h"
#include "led.h"
#include "Timer.h"
int main(void)
{
TimerBase_Config();
LED_GPIO_Confing();
GPIO_SetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN);
while(1)
{
}
}
matters needing attention :
Be sure to clear the flag bit , In particular, the clear flag bit in the interrupt service function , Bloggers have been checking for a long time because of this
边栏推荐
- Nr-prach: access scenario and access process
- Successful graduation [3]- blog system update...
- MySQL data manipulation language DML common commands
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- Exception handling of arm
- 1300. sum of varied array closed to target
- 【順利畢業】[1]-遊覽 [學生管理信息系統]
- Development of electrical fire system
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
猜你喜欢
万字手撕七大排序(代码+动图演示)
Vector processor 9_ Basic multilevel interconnection network
Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
Uniapp realizes global sharing of wechat applet and custom sharing button style
內存數據庫究竟是如何發揮內存優勢的?
Hal library sets STM32 clock
[male nanny style] teach you to open the first wechat applet
UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
CEF下载,编译工程
【順利畢業】[1]-遊覽 [學生管理信息系統]
随机推荐
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
CEF download, compile project
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
In third tier cities and counties, it is difficult to get 10K after graduation
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
单片机学到什么程度能找到工作,这个标准不好量化
Flink learning notes (IX) status programming
Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
The cyclic shift of PUCCH in NR channel is generated by MATLAB
Leetcode daily question (2109. adding spaces to a string)
Education is a pass and ticket. With it, you can step into a higher-level environment
Intelligent home design and development
Process communication - semaphore
Convert IP address to int
NR PUCCH format0 sequence generation and detection mechanism
Raspberry pie installation SciPy
[CSDN]C1訓練題解析_第三部分_JS基礎
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
【顺利毕业】[1]-游览 [学生管理信息系统]