Program 1: Blinking On-board LED - 4.1 | EXPERIMENT NO. 9 TITLE: Introduction to ARM Microcontrollers - Basic I/O and Peripherals | Microcontroller Lab
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Understanding GPIO Basics

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Today, we'll discuss GPIO and how we can use it to control an LED. Can anyone tell me what GPIO is?

Student 1
Student 1

GPIO stands for General Purpose Input/Output, right? It’s used for reading inputs and controlling outputs.

Teacher
Teacher

Exactly! GPIO pins can be configured either as inputs or outputs. In our experiment, we will configure a GPIO pin to drive an LED. What mode do we need for the LED?

Student 2
Student 2

We need to set it as a general-purpose output.

Teacher
Teacher

Great! Let's remember that we use the MODE register to set these configurations. Now, who remembers why we use GPIO instead of other types of interfaces?

Student 3
Student 3

It's flexible and allows for easy interfacing with many devices!

Teacher
Teacher

Perfect! GPIO allows us to control various peripherals easily. Remember, we configure them through the corresponding registers.

Teacher
Teacher

To summarize, GPIO is our gateway to interact with the outside world using inputs and outputs?

All Students
All Students

Yes, that’s correct!

Enabling GPIO Clocks

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Now that we know what GPIO is, let’s discuss how to enable the GPIO clock. Why is it important to turn the clock on?

Student 4
Student 4

If we don't enable it, the GPIO pins won't work, right?

Teacher
Teacher

Exactly! The microcontroller needs to power the GPIO peripheral in order to control it. So which register do we use?

Student 2
Student 2

We use the RCC AHB1ENR register to enable the clock for GPIOA.

Teacher
Teacher

Correct! We modify this register to enable GPIOA. Remember, without setting this bit, our LED won’t even turn on.

Teacher
Teacher

In summary, we enable the clock through specific bits in the RCC register. Can anyone tell me which bit we need to set for GPIOA?

Student 1
Student 1

It's bit 0 for GPIOA!

Creating a Delay Function

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Next, let's create a delay function to control how fast our LED blinks. Why do we need a delay?

Student 3
Student 3

To control the blinking speed! Otherwise, it would just turn on and off too quickly to see.

Teacher
Teacher

Exactly! We will create a software delay. This delay is based on how fast our microcontroller operates. What is the general calculation for cycles needed?

Student 4
Student 4

If the CPU speed is 84 MHz, we’ll calculate cycles per millisecond to repeat it for the desired amount of time.

Teacher
Teacher

Well said! The formula is pivotal in our delay function. Always remember, this creates a blocking delay.

Teacher
Teacher

To summarize, we’ve learned that a proper delay is crucial for visible actions and how to numerically calculate it.

Implementing the LED Blinking Logic

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Finally, let’s put it all together in our main function to blink the LED. What do you think the main loop should contain?

Student 1
Student 1

It should continuously turn the LED on and off using the delay we just made!

Student 2
Student 2

We’ll also need to make sure to set and clear the appropriate bit in the ODR for the LED.

Teacher
Teacher

Exactly, creating this loop will allow our LED to blink. Each iteration will involve turning it on, waiting, and turning it off again. What should the iteration be based on?

Student 3
Student 3

The delay times for each on and off state!

Teacher
Teacher

Great! To summarize, we will continuously toggle our LED in an infinite loop by checking the ODR register and using the delay function. This reinforces our understanding of GPIO in embedded applications.

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section covers the implementation of an embedded program to blink an on-board LED using an ARM microcontroller, specifically focusing on basic General Purpose Input/Output (GPIO) and timer functionalities.

Standard

In this section, students learn to program a blinking LED using an ARM Cortex-M microcontroller like STM32. The exercise highlights GPIO configurations for output, the use of a simple software delay, and reinforces understanding of register-level programming in an embedded environment.

Detailed

Program 1: Blinking On-board LED

This program introduces students to the practical aspects of GPIO operations using ARM Cortex-M microcontrollers. The objective is to create a continuous blinking effect for an on-board LED connected to pin PA5 of the STM32F401RE board. The approach involves enabling the clock for the GPIO peripherals and configuring the respective mode for the LED as a general-purpose output. The code also includes a simple delay function implemented in software to achieve an ON/OFF cycle for the LED every 500 milliseconds.

Key Steps Covered:

  1. Clock Activation for GPIO: The program begins by enabling the peripheral clock for GPIOA using the RCC peripheral.
  2. GPIO Configuration: It explains how to configure PA5 as a general-purpose output and how to set it to push-pull mode, making it ready for driving the LED.
  3. Software Delay Function: A delay function is defined based on the CPU frequency, enabling timed intervals for the LED blinking.
  4. LED Toggling Mechanism: Within an infinite loop, the program toggles the state of PA5 to produce the blinking effect.

Each of these steps is essential in understanding how embedded systems interface with hardware through low-level programming, emphasizing the ARM Cortex-M architecture's simplicity and efficiency.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Objective

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Objective: To continuously blink the on-board LED (PA5) with a 500 ms ON/500 ms OFF period using a simple software delay.

Detailed Explanation

The aim of this program is to make an LED connected to pin PA5 blink on and off. This is done by turning the LED on for 500 milliseconds, then turning it off for another 500 milliseconds. The LED’s state alternates every 500 milliseconds, creating a visible blinking effect.

Examples & Analogies

Think of a traffic light that turns green (on) for a specific duration before turning red (off). Similarly, in this program, the LED is like a traffic light where we control when it turns on and when it turns off.

C Code Structure

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

// Include the appropriate device header file for STM32F4xx series

include "stm32f4xx.h"

// Simple software delay function
void delay_ms(unsigned int ms) {
// This delay is approximate and depends on CPU clock frequency.
// For an 84MHz system, roughly 84000 cycles per ms.
unsigned long i;
unsigned long cycles_per_ms = 84000; // For 84 MHz, approx 84000 cycles for 1ms
for (i = 0; i < ms * cycles_per_ms; i++);
}

int main(void) {
// 1. Enable Clock for GPIOA
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
// ...
while (1) {
// Turn LED ON
GPIOA->ODR |= (1U << 5);
delay_ms(500); // Delay for 500ms
// Turn LED OFF
GPIOA->ODR &= ~(1U << 5);
delay_ms(500); // Delay for 500ms
}
}

Detailed Explanation

The code begins with including the STM32F4xx header file, which provides access to all the register definitions. A function 'delay_ms' is defined to create a delay based on a while loop; this function is executed by the main program. In 'main', the GPIO clock is enabled for PA5, then it’s configured as a general-purpose output through specific register manipulations. The program then enters an infinite loop where it turns on the LED, invokes a software delay, turns off the LED, and waits again.

Examples & Analogies

Imagine a chef who periodically checks the oven timer while cooking. Each time they set a timer (delay), they prepare food (turning on the LED) until the timer goes off (delay ends). The LED and the timer are akin to cooking with attention to timing.

Enabling GPIOA and Configuring PA5

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

// 1. Enable Clock for GPIOA
// RCC (Reset and Clock Control) AHB1ENR (AHB1 Peripheral Clock Enable Register)
// Bit 0 enables GPIOA clock (RCC_AHB1ENR_GPIOAEN)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
// 2. Configure PA5 as General Purpose Output mode
// Clear bits 11:10
GPIOA->MODER &= ~(GPIO_MODER_MODER5_Msk); // Clear existing mode bits for PA5
// Set bits 11:10 to 01
GPIOA->MODER |= GPIO_MODER_MODER5_0;

Detailed Explanation

To use pin PA5 for controlling the LED, we first enable the clock for GPIO port A. This is essential; if the clock isn’t enabled, the port won’t work. Following this, we set PA5 to output mode by modifying the GPIOA_MODER register. The value in the register indicates the pin's purpose, which we set to 'general-purpose output'. This involves clearing the bits corresponding to PA5 and then setting the necessary bits to configure it as an output.

Examples & Analogies

It's like opening a door to a room (enabling the GPIOA clock) before you can enter (control the LED). Only after the door is opened can you adjust the lighting (configure PA5 to output).

Toggle Logic with Delay

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

// 4. Toggle LED On and Off
while (1) {
// Turn LED ON (Set PA5 high)
GPIOA->ODR |= (1U << 5); // Set bit 5 (PA5) to 1
// Delay for 500ms
delay_ms(500);
// Turn LED OFF (Set PA5 low)
GPIOA->ODR &= ~(1U << 5); // Clear bit 5 (PA5) to 0
// Delay for 500ms
delay_ms(500);
}

Detailed Explanation

Inside an infinite loop, the code first sets PA5 to a high state, turning the LED on. After that, the software delay function is called to maintain this state for 500 milliseconds. Then, the LED is turned off by clearing the same bit, and the process repeats. This looping continues indefinitely, producing a consistent blinking effect.

Examples & Analogies

Consider a flash from a camera: At the press of a button, the light turns on for a brief moment (like turning the LED on) before going dark again (turning it off). The camera flash repeats this cycle, just like our LED.

Expected Results

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Simulation/Expected Results:
● Hardware: The on-board LED connected to PA5 on your STM32 Nucleo/Discovery board should blink ON and OFF with approximately 0.5-second intervals.
● Keil/CubeIDE Debugger: During a debug session, open the Peripherals view (or Memory view at GPIOA base address 0x40020000). Observe the state of bit 5 in GPIOA->ODR toggling between 0 and 1.

Detailed Explanation

When the program is run on the hardware, the LED should visibly blink on and off at regular intervals, approximately half a second apart. During debugging, one can monitor the register corresponding to the port to see this change in state, confirming that the microcontroller is functioning as intended.

Examples & Analogies

This effect is like the blinking of a neon sign; if you were monitoring it, you'd see it alternately light up and turn off at regular intervals, confirming that the sequence is being executed correctly.

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • Blinking LED Program: An embedded system application that uses GPIO to control an LED.

  • Software Delay: A routine allowing controlled timing within an application.

  • GPIO Configuration: Setting up microcontroller pins for different input/output functionalities.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • Blinking LED using GPIO interfaces demonstrates basic input/output operations.

  • Software delay allows the user to control the speed of LED blinking, enhancing user control.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • To blink an LED with grace, enable the clock, and set the pace.

📖 Fascinating Stories

  • Imagine a traffic light; the green LED blinks to signal cars to go, just as we control the GPIO for our LED.

🧠 Other Memory Gems

  • Remember 'GLOW' to configure GPIO: Get the clock, Lock the mode, Output set, Wait for blink.

🎯 Super Acronyms

GPA - GPIO Pin Activation

  • Enable the clock (G)
  • set the mode (P)
  • activate (A).

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: GPIO

    Definition:

    General Purpose Input/Output; pins on a microcontroller used for input and output.

  • Term: RCC

    Definition:

    Reset and Clock Control; a peripheral in microcontrollers for managing clocks.

  • Term: PA5

    Definition:

    The designation for Pin 5 of GPIO Port A, where the LED is connected.

  • Term: ODR

    Definition:

    Output Data Register; used to write data to output pins of the GPIO.

  • Term: Delay Function

    Definition:

    A software routine designed to create a timed delay in program execution.