0% found this document useful (0 votes)
1 views

Document2

The document describes multiple experiments involving LED control using GPIO pins and UART communication on an STM32 microcontroller. It includes experiments for alternating LED patterns, button-controlled LEDs, cycling through LED patterns, and controlling LEDs based on UART input. Additionally, it outlines the use of timers for generating delays and transmitting messages via LPUART1.

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)
1 views

Document2

The document describes multiple experiments involving LED control using GPIO pins and UART communication on an STM32 microcontroller. It includes experiments for alternating LED patterns, button-controlled LEDs, cycling through LED patterns, and controlling LEDs based on UART input. Additionally, it outlines the use of timers for generating delays and transmitting messages via LPUART1.

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
You are on page 1/ 7

Experiment 1: Alternating LEDs (Two LEDs),Turn on PA5, then PB0, and vice versa, with a

delay.

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn ON PA5


HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Turn OFF PB0
HAL_Delay(500); // Wait for 500ms

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn OFF PA5


HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Turn ON PB0
HAL_Delay(500);

EXPERIMENT 2:Create a pattern where the LEDs blink in a sequence, alternating between
turning on and off at different intervals, which will create the appearance of a "dancing" light
effect.

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn on LED1 (PA5)


HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //
Turn off LED2 (PB0)
HAL_Delay(100); // Delay for 100ms (quick blink)

// Turn off LED1 (PA5) and turn on LED2 (PB0)


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //
Turn off LED1 (PA5)
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); //
Turn on LED2 (PB0)
HAL_Delay(100); // Delay for 100ms

// Turn on LED1 (PA5) again


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); //
Turn on LED1 (PA5)
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //
Turn off LED2 (PB0)
HAL_Delay(100); // Delay for 100ms

// Turn off both LEDs (PA5 and PB0) for a brief pause
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //
Turn off LED1 (PA5)
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //
Turn off LED2 (PB0)
HAL_Delay(200); // Longer pause before next "dance move"
Experiment 3: Button-Controlled LED,Control the state of PA5 LED using a button connected to
PC13.

i=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);
if(i==1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}
Experiment 4: LEDs ON or OFF Based on Button Press,

· If the button is pressed, PA5 and PB0 LEDs should both turn ON.
· If the button is released, both LEDs should turn OFF.

Experiment3: Cycle LED Patterns with Each Button Press .Change the LED pattern each time
the button (connected to PC13) is pressed. The patterns could include:

· Pattern 1: Both LEDs OFF


· Pattern 2: PA5 ON, PB0 OFF
· Pattern 3: PA5 OFF, PB0 ON
· Pattern 4: Both LEDs ON

void CycleLEDPatterns(void)
{
static uint8_t pattern = 0;
int i;
i=HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);
if (i==1) // Button Pressed
{
if(pattern == 0)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
else if(pattern == 1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
else if(pattern == 2)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
else if(pattern == 3)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
pattern = (pattern + 1) % 4; // Cycle through 4 patterns
HAL_Delay(200); // Debounce delay
}
}
CycleLEDPatterns();
Experiment dutycycle:
void SetLEDsWithDifferentDutyCycles(uint8_t dutyCycleLED1, uint8_t
dutyCycleLED2)
{
uint32_t totalPeriod = 1000; // Total period in milliseconds (1 second)

// Calculate on and off times for LED1


uint32_t onTimeLED1 = (totalPeriod * dutyCycleLED1) / 100;
uint32_t offTimeLED1 = totalPeriod - onTimeLED1;

// Calculate on and off times for LED2


uint32_t onTimeLED2 = (totalPeriod * dutyCycleLED2) / 100;
uint32_t offTimeLED2 = totalPeriod - onTimeLED2;

while (1)
{
// Control LED1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn LED1 ON
HAL_Delay(onTimeLED1); // Keep LED1 ON for the calculated
"onTimeLED1"

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn LED1


OFF
HAL_Delay(offTimeLED1); // Keep LED1 OFF for the calculated
"offTimeLED1"

// Control LED2
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Turn LED2 ON
HAL_Delay(onTimeLED2); // Keep LED2 ON for the calculated
"onTimeLED2"

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Turn LED2


OFF
HAL_Delay(offTimeLED2); // Keep LED2 OFF for the calculated
"offTimeLED2"
}
}

EXPERIMENTS BASED ON LPUART1

1) Print "Hello World" via LPUART1

Objective: Transmit "Hello World" to a terminal application (e.g., PuTTY) using LPUART1.

Steps:

· Set up LPUART1 in STM32CubeMX (asynchronous, 9600 baud rate, 8 data bits, 1 stop
bit, no parity).

· Initialize LPUART1 in your code with HAL functions (HAL_LPUART_Init()).

· Use HAL_LPUART_Transmit() to send the string "Hello World!" from STM32 to your
PC's terminal.

Expected Outcome:
The terminal connected to the STM32 will display "Hello World!".

PROCEDURE:

Define the variable:


char msg[] = "Hello, World!";

Use the following instruction to print the word

HAL_UART_Transmit(&huart2, (uint8_t*)msg, sizeof(msg)-1, HAL_MAX_DELAY);

2. Control Built-In LED with UART2 Input

Objective: Turn on/off the built-in LED on STM32G474RE based on commands received via
UART2.

Steps:

· Configure UART2 for serial communication (9600 baud, 8 data bits, 1 stop bit).

· Implement a simple command interpreter where input strings like "ON" will turn the LED
on, and "OFF" will turn it off.

· Use HAL_GPIO_WritePin() to control the LED on your STM32.

Expected Outcome:
Send "ON" or "OFF" from the terminal to toggle the onboard LED.

Hint: use HAL_UART_Receive(&huart2, &received_data, 1, HAL_MAX_DELAY)

2. Control Built-In LED with UART2 Input

Objective: Turn on/off the built-in LED on STM32G474RE based on commands received via
UART2.

Steps:

· Configure UART2 for serial communication (9600 baud, 8 data bits, 1 stop bit).

· Implement a simple command interpreter where input strings like "ON" will turn the LED
on, and "OFF" will turn it off.

· Use HAL_GPIO_WritePin() to control the LED on your STM32.

Expected Outcome:
Send "ON" or "OFF" from the terminal to toggle the onboard LED.

Hint: use HAL_UART_Receive(&huart2, &received_data, 1, HAL_MAX_DELAY)

uint8_t received_data;

printf("Enter '0' to blink the LED, or '1' to turn on the LED: ");
scanf("%c", &received_data);

if (HAL_UART_Receive(&huart2, &received_data, 1, HAL_MAX_DELAY) ==


HAL_OK)

if (received_data == '0') // If the received character is '0'

// Blink the LED

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,
GPIO_PIN_RESET); // Delay to create blink effect

else if (received_data == '1') // If the received character is '1'

// Turn on the LED

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,
GPIO_PIN_SET); // Turn LED on

}// Delay for 1 second

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