This document provides an overview of a training on timers and PWM for bare metal embedded systems. The goals are to learn clock control, generate 1Hz signals using delays and timers, and create PWM signals at predefined duty cycles. It explains clock control diagrams and settings, and provides code examples to generate a 1Hz signal using delays, use Timer4 to toggle a pin in an interrupt handler, and set up PWM on Timer4 Channel 3. Finally, it lists an individual task to control 4 LEDs with PWM using different channels and buttons to fade the LEDs in/out.
2. Contents
Assignments and Goals
Theory
Practice
Individual task
19.10.2020 15:13 Bare Metal Training 03 2
3. Assignments and Goals
Learn about clock control
Create firmware that generate 1Hz signal
Create firmware that works with Timers to
generate signals
Create firmware that generate PWM signal
with predefined duty cycle
19.10.2020 15:13 Bare Metal Training 03 3
7. Create FW that generates 1Hz signal
19.10.2020 15:13 Bare Metal Training 03 7
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_Delay(500);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
8. Create FW that generates 1Hz signal
19.10.2020 15:13 Bare Metal Training 03 8
11. Create FW that work with Timer4
19.10.2020 15:13 Bare Metal Training 03 11
12. Create FW that work with Timer4
19.10.2020 15:13 Bare Metal Training 03 12
13. Create FW that work with Timer4
19.10.2020 15:13 Bare Metal Training 03 13
In main()
{
HAL_TIM_Base_Start_IT(&htim4);
while (1) {}
}
void TIM4_IRQHandler(void)
{ /* USER CODE BEGIN TIM4_IRQn 0 */
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
/* USER CODE END TIM4_IRQn 0 */
HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
/* USER CODE END TIM4_IRQn 1 */
}
14. Create FW that work with Timer4
19.10.2020 15:13 Bare Metal Training 03 14
15. Create FW that work with PWM
19.10.2020 15:13 Bare Metal Training 03 15
16. Create FW that work with PWM
19.10.2020 15:13 Bare Metal Training 03 16
In main()
{
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
TIM4->CCR3=100;
while (1)
{}
}
17. Create FW that work with PWM
19.10.2020 15:13 Bare Metal Training 03 17
18. Individual task
Enable 4 PWMs to control LEDs
(PD15, PD14, PD13, PD12)
Blink LED without interrupts and main loop
(by timer HW)
Fade In/Out LEDs with help of buttons
(or automatically by algorithm)
19.10.2020 15:13 Bare Metal Training 03 18