STM32CUBEMX TIMER 学习笔记
来源:互联网 发布:windows pe下载浏览器 编辑:程序博客网 时间:2024/06/06 07:39
原文地址 http://microtechnics.ru/en/stm32cube-timer-and-interrupts/
Previous post has been devoted to GPIO configuration, we learned how to turn on/off LEDs with the help of STM32Cube. So, today we will also toggle the LED, but we’ll do it through interrupt handler. Let’s configure one of STM32 timers to generate an interrupt every 500 ms.
Primarily we should create a new project, choose the MCU which we would like to use and set up the project as usual. After that we can proceed to the timer configuration. I’ll use TIM3 unit:
To enable the timer clock we should select the clock source for it (for example, internal clock). Furthermore, I’ve activated PD12, because one of the LEDs on my development board is connected to this pin. So, I have to set its mode to “General output”.
We’ve already enabled the timer unit, but it also requires some extra configuration. In order to do this we should open Configuration tab and double-click on TIM3 button:
TIM3 is connected to APB1 bus which default frequency is 16 MHz (in one of the future posts I’ll describe how to set different frequencies of all busses). Let’s set the prescaler value to 16000 (in counter settings we should enter (PSC – 1) value). Thus, to calculate timer frequency we should divide APB1 frequency by 16000 prescaler:
MHz Hz
Furthermore, we should set the proper value of counter period. If we set it to 499 (as shown at the screenshot), we’ll get the following value of timer period:
s
And the final step of TIM3 configuration is enabling its interrupt. This can be done at the “Nvic Settings” tab. Finally, let’s start code generation! As the result we get the new project with timer initialization function and interrupt handler (it is located in a file called stm32f4xx_it.c).
What should also be done is starting the timer counting (HAL_TIM_Base_Start_IT() function) and adding some extra code into the interrupt handler in order to change the output state of PD12 (LED toggling):
int main(void){ /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM3_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Base_Start_IT(&htim3); /* USER CODE END 2 */ /* USER CODE BEGIN 3 */ /* Infinite loop */ while (1) { } /* USER CODE END 3 */ }
void TIM3_IRQHandler(void){ /* USER CODE BEGIN TIM3_IRQn 0 */ /* USER CODE END TIM3_IRQn 0 */ HAL_TIM_IRQHandler(&htim3); /* USER CODE BEGIN TIM3_IRQn 1 */ HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); /* USER CODE END TIM3_IRQn 1 */}
Now we can program the MCU and see the green led toggling every 500 ms! 2 0
- STM32CUBEMX TIMER 学习笔记
- java Timer学习笔记
- java Timer学习笔记
- Timer学习笔记
- STM32CubeMX+FreeRTOS实验---使用os timer
- STM32CubeMX学习:基本定时器
- Python学习笔记(5)Timer
- STM32CubeMX
- STM32CubeMx
- 学习STM32CubeMX与HAL库吐槽
- QT学习笔记之十一 Timer
- java Timer的学习笔记(一)
- Linux 驱动学习笔记2 -- Timer
- 初探boost之timer库学习笔记
- java Timer的学习笔记(一)
- Linux 驱动学习笔记2 -- Timer
- QtQuick学习笔记之QML定时器Timer
- libev 学习笔记之timer实现原理
- CSDN一直都很不错,加油了2
- Java transient关键字使用小记
- 一些常用操作
- TensorFlow 辨异 —— tf.placeholder 与 tf.Variable
- 发工资咯:) 杭电2021
- STM32CUBEMX TIMER 学习笔记
- Android设计模式(八)-责任链模式
- 蓝桥杯-杨辉三角形(java)
- ReentrantLock源码分析与理解
- 随便看看
- 聊聊并发(一)——深入分析Volatile的实现原理
- 多个线程之间是如何进行通信的呢?
- 绝对值排序 杭电2020
- JAVA学习笔记_判断二位数组是否存在连续4个数的值相同