Question: LAB 1 - STM 3 2 : Digital IO - LED and Pushbutton and TIMER Introduction This lab uses a STM 3 2 G 0

LAB 1- STM32: Digital IO - LED and Pushbutton and TIMER Introduction
This lab uses a STM32G071RBT6
Objectives
To install and setup STM32CubeIDE on your machine, which is essential to complete the lab tasks and the MCU project
To configure the GPIO registers within the STM32 to blink an LED that is controlled by reading an input from a push-button
To explore some basic debugging strategies in embedded systems
Activity #1: Preparation
Read through section 7 of the STM32G0 datasheet.
Follow the guide "Getting Started on STM32 CubeIDE" and follow the steps to blink the LED at 1Hz.
If the on-board LED is blinking at 1Hz, your system is ready for the lab.
Activity #2: Control an LED using a pushbutton
Modify the code to blink the onboard LED at 1Hz (or ON for 0.5 sec and OFF for 0.5 sec) while the user pushbutton is pressed.
While the button is released, turn off the LED.
In your setup code, configure the port and pin connected to the user button (built-in push button) as an input
Determine which port and pin the button is connected to in section 6.11 of the STM32 Nucleo-64 board's user manual
Enable the clock to that port
Configure the pin to be an input
Determine if you should configure a pull-up or pull-down resistor using GPIOx_PUPDR
In your main loop, read the value of the corresponding bit in GPIOx_IDR.
To check a bit, you can use the following:
if ((GPIOx_IDR >> n) & 1)
{
// Runs if the nth bit of GPIOx_IDR is set
}
Activity #3: Using the BSR
An alternative way of setting the output of a pin to be HIGH or LOW is to use the bit set reset register. Refer to section 7.3.5 and 7.4.7 of the datasheet.
In your code, instead of using the output data register (ODR), use the bit set reset register (BSRR) to turn on and off the LED.
Activity #4: Delay Function using TIM2 in Count up Overflow Mode
Configure TIM2 to overflow every 1ms in count up mode to create your own HAL delay function. This is done by enabling TIM2 and setting the prescaler (PSC) and auto-reload value (ARR). It is recommended to read up on Chapter 22.3.2 of the datasheet to understand the principles of different counter modes.
The instructions to do so are as follows.
Enable TIM2 in the Reset and Clock Control.
Set TIM2 in count up mode.
Set the Prescaler (PSC) and Auto-Reload Register (ARR) values.
Enable TIM2 on the Control Register.
Use TIM2 to generate your own HAL delay function.
To set the PSC and ARR values, the following relationship needs to be used.
Fevents = FCLK /(PSC +1)(ARR +1)
Where the Fclk is the frequency of the system clock defined by the APB Timer Clocks in CubeIDEs clock configuration. The overflow value is set by the ARR.
The delay function should have a similar format to what is shown. Call this function in
the main while loop and set a delay value (in milliseconds).
void my_delay_ms(uint32_t ms)
{
// Read from TIM2 counter and increment a variable.
// Clear the status register and repeat until the millis_val.
TIM2->SR &= ~(10);
}
Appendices
Appendix 1: Setting up Timer 2(TIM2)
Enable the TIM2 register in the APB peripheral clock enable register 1(RCC_APBENR1). The relevant information can be found in Chapter 5.4.15 of the datasheet. To better understand how count up timers work, refer to Chapter 22.3.1 and 22.3.2 in the
Datasheet.
The STM32 architecture has multiple timer signals that you can configure. By enabling
the clock for RCC_APBENR1, additional registers need to be set based on the purpose of the timer. These can be found in Chapter 22.4 of the datasheet. To configure the TIM2 Registers, the following procedure should be followed.
The timer must be set to count up mode. This is done by configuring control register 1 and setting the Direction register. Refer to Chapter 22.4.1 in the datasheet.
Set the prescaler (PSC) and Auto-Reload Values (ARR). The PSC and ARR values are set based on the required time interval and have the following relationship.
Fevents = FCLK /(PSC +1)(ARR +1)
Where Fclk comes from the ABP peripheral clock, event frequency is how
frequent the timer increments. The prescaler is used to divide the clock signal
while the ARR is the maximum value to set for timer overflow. PSC and ARR
register values, in Chapters 22.4.14 and 22.4.15 of the datasheet.
Once these are set, the timer can then be enabled in Control Register 1 and setting the counter enable register (CEN).The count up diagram found in Chapter 22.3.2 in the datasheet explains the timer operations. To generate the delay needed in the timer, an update event needs to occur, i.e. when a counter overflows. This triggers an interrupt flag which needs to be read and reset. : Compared to AVR, this is an indicator this is an overflow has occured.
LAB 1 - STM 3 2 : Digital IO - LED and Pushbutton

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!