当前位置:网站首页>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
边栏推荐
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- 在三线城市、在县城,很难毕业就拿到10K
- Leetcode daily question (2212. maximum points in an archery competition)
- Vector processor 9_ Basic multilevel interconnection network
- [CSDN]C1训练题解析_第四部分_Web进阶
- 单片机学到什么程度能找到工作,这个标准不好量化
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- [combinatorics] Introduction to Combinatorics (context of combinatorics | skills of combinatorics | thought of combinatorics 1: one-to-one correspondence)
- Leetcode daily question (745. prefix and suffix search)
- 顺利毕业[3]-博客系统 更新中。。。
猜你喜欢

Education is a pass and ticket. With it, you can step into a higher-level environment

【22毕业季】我是毕业生yo~

Oracle数据库 SQL语句执行计划、语句跟踪与优化实例

You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

Shell logic case

单片机学到什么程度能找到工作,这个标准不好量化

STM32 interrupt priority management

Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program

UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
随机推荐
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
GPIO port details, Hal library operation keys
[CSDN]C1訓練題解析_第三部分_JS基礎
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
Introduction to chromium embedded framework (CEF)
Leetcode daily question (2305. fair distribution of cookies)
There is no specific definition of embedded system
Leetcode daily question (2109. adding spaces to a string)
UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding
SSB Introduction (PbCH and DMRs need to be supplemented)
Leetcode daily question (968. binary tree cameras)
Process communication - semaphore
Schematic diagram and connection method of six pin self-locking switch
Development of fire power monitoring system
Leetcode daily question (2090. K radius subarray averages)
[combinatorics] Introduction to Combinatorics (context of combinatorics | skills of combinatorics | thought of combinatorics 1: one-to-one correspondence)
要選擇那種語言為單片機編寫程序呢
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
[CSDN]C1训练题解析_第四部分_Web进阶