0% found this document useful (0 votes)
54 views1 page

Document 6

The document outlines a program to generate a 1-second delay using different timers with various clock sources: PLL, HSI, and HSE. It includes code snippets for toggling an LED on a microcontroller when the timer overflows, specifically using HSI as the clock source. Additionally, it demonstrates how to retrieve the timer's counter value and transmit it over UART while managing the overflow flag and preventing excessive UART messages.

Uploaded by

anuescapist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views1 page

Document 6

The document outlines a program to generate a 1-second delay using different timers with various clock sources: PLL, HSI, and HSE. It includes code snippets for toggling an LED on a microcontroller when the timer overflows, specifically using HSI as the clock source. Additionally, it demonstrates how to retrieve the timer's counter value and transmit it over UART while managing the overflow flag and preventing excessive UART messages.

Uploaded by

anuescapist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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
}

You might also like