TIMER
Write a program to generate a delay of 1s using different timers with
different clock sources.
a)Use PLL as clock source
b)Use HSI as clock source
c)Use HSE as clock source
Toggle on board LED for testing the timer and use polling method
HSI AS CLOCK SOURCE
A) if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
{
// Timer overflowed, clear the flag
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
// Toggle the LED
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Assuming the
LED is connected to pin PA5
// Optionally, add a small delay here if needed
}
B)char buffer[20]; // Buffer to hold the string representation of the
counter
uint32_t timer_counter_value = 0;
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
{
// Clear the overflow flag
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
// Get the current counter value of the timer
timer_counter_value = __HAL_TIM_GET_COUNTER(&htim3);
// Toggle the LED (PA5) each time the timer overflows
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// Prepare the string with counter value
sprintf(buffer, "Counter: %lu\r\n", timer_counter_value);
// Transmit the counter value over UART
HAL_UART_Transmit(&hlpuart1, (uint8_t*)buffer, strlen(buffer),
HAL_MAX_DELAY);
// Optionally add a small delay to prevent flooding UART with
messages too quickly
HAL_Delay(200); // Delay for 200ms
}