Document2
Document2
delay.
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.
// 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:
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)
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"
// 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"
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).
· 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:
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.
Expected Outcome:
Send "ON" or "OFF" from the terminal to toggle the onboard LED.
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.
Expected Outcome:
Send "ON" or "OFF" from the terminal to toggle the onboard LED.
uint8_t received_data;
printf("Enter '0' to blink the LED, or '1' to turn on the LED: ");
scanf("%c", &received_data);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,
GPIO_PIN_RESET); // Delay to create blink effect
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,
GPIO_PIN_SET); // Turn LED on
TIMER
Write a program to generate a delay of 1s using different timers with different clock sources.
Toggle on board LED for testing the timer and use polling method
A) if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
uint32_t timer_counter_value = 0;
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
timer_counter_value = __HAL_TIM_GET_COUNTER(&htim3);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// Optionally add a small delay to prevent flooding UART with messages too quickly
HAL_Delay(200); // Delay for 200ms