PROGRAMS TO BE EXECUTED (C Language) - 4 | 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.

Blinking On-board LED

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Today, we will learn how to blink an LED using the STM32 microcontroller. The LED is connected to pin PA5. Can anyone tell me what GPIO stands for?

Student 1
Student 1

General Purpose Input/Output!

Teacher
Teacher

Correct! GPIO pins can be configured to either read input or output signals. Now, the first step is to enable the clock for GPIOA. Why do you think this is necessary?

Student 2
Student 2

I think we need to power the GPIO functionality to make it work?

Teacher
Teacher

Exactly! If we don’t enable the clock, the GPIO pins will not function. Now, let's discuss the configuration of PA5 for output. Do you remember how we change a GPIO pin to output mode?

Student 3
Student 3

We modify the MODER register, right?

Teacher
Teacher

Yes! The MODER register allows us to set the pin mode. We will set it to '01' for general purpose output. Now, does anyone remember the function of the output type register?

Student 4
Student 4

It determines if the output is push-pull or open-drain.

Teacher
Teacher

Correct! In most cases, push-pull is the default and sufficient. Let’s conclude this session. We learned how to enable clocks and configure GPIO for output, which are essential steps for interfacing with hardware.

Reading Pushbutton Input

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

In today's session, we will focus on reading a pushbutton state and controlling an LED accordingly. The button is connected to PC13. What is the importance of using pull-up resistors in this scenario?

Student 1
Student 1

It helps ensure the pin is high when the button is not pressed, avoiding floating values.

Teacher
Teacher

Exactly! This setup gives us a stable high signal until the button is pressed. Now, how do we check if the button is pressed?

Student 2
Student 2

We read the IDR register for that pin?

Teacher
Teacher

Correct! The IDR register lets us read the current state of input pins. If the bit is low during our check, the button is pressed. Let’s summarize: pull-up resistors ensure stable input, and we check the state through IDR. Anyone have questions?

Delay Generation using a Timer

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Welcome back! Today, we will look at generating accurate delays using a timer, specifically TIM2. Why would we prefer using a timer for delays instead of just a software loop?

Student 3
Student 3

Timers provide more accuracy and allow the CPU to perform other tasks in the meantime.

Teacher
Teacher

Exactly! By using the prescaler and ARR registers, we configure how fast the timer counts. Can someone tell me how to set the prescaler for a 1 kHz timer clock?

Student 4
Student 4

We would set the prescaler to divide the higher clock frequency down to 1 kHz.

Teacher
Teacher

Correct! For example, with a 42 MHz clock and a prescaler of 41999, we achieve 1 kHz. To create a 500 ms delay, what should the auto-reload value be?

Student 2
Student 2

It would be 499 since we count from 0 to 499.

Teacher
Teacher

Precisely! By polling the update interrupt flag, we can detect when the delay is complete. Summarizing, the timer allows for efficient delay management crucial for embedded systems.

Introduction & Overview

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

Quick Overview

This section provides C language programs for ARM microcontrollers focusing on GPIO operations such as LED control and button reading using direct register access.

Standard

The section details three C programs that demonstrate how to interface with hardware components on an ARM microcontroller, specifically the STM32F401RE Nucleo board. The programs illustrate LED blinking, reading a user button input, and generating delays using timers. Each program emphasizes the use of direct register manipulation for clear understanding of peripheral configuration.

Detailed

Detailed Summary

This section explores practical programming for ARM microcontrollers, particularly the STM32F401RE Nucleo board, using the C programming language. It presents three key programs each demonstrating different GPIO operations and timer functionalities:

  1. Blinking On-board LED: This program configures the GPIO for the on-board LED connected to PA5. It uses a simple delay loop to blink the LED at approximately 500 milliseconds intervals. The code explains setting up the GPIO mode and output type for the LED, which exemplifies how to manipulate pin states using direct register access.
  2. Reading Pushbutton Input: The second program connects to the user button located at PC13. It configures the GPIO for input with a pull-up resistor to ensure reliable button state reading. The LED state reflects the button's state, demonstrating direct input control and feedback. The explanation reinforces how to enable clocks, configure modes, and read inputs while discussing debounce handling if necessary.
  3. Delay Generation using a Timer: Lastly, the section covers timer use to create a precise delay before toggling the LED state. This program employs the TIM2 peripheral on the STM32 to implement accurate timing through the configuration of the prescaler and auto-reload registers, showcasing the advantages of hardware timers over software delays. Together, these programs provide foundational knowledge on hardware interaction using C on ARM microcontrollers, emphasizing efficient, low-level programming techniques.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Introduction to Programs

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

These programs will use direct register access for clarity of understanding how peripherals are configured at the lowest level. This is sometimes called "bare-metal" programming.

Assumption: Using an STM32F401RE Nucleo board.
- On-board LED: Connected to PA5 (GPIO Port A, Pin 5).
- On-board User Button: Connected to PC13 (GPIO Port C, Pin 13).
- System Clock Frequency: Assuming default 16 MHz HSI (internal RC oscillator) or configured to 84 MHz (common for STM32F4). We'll assume 84 MHz system clock for timer example for a practical number.

Detailed Explanation

This section outlines the programs that will be executed on the STM32F401RE Nucleo board. The idea is to write low-level code (bare-metal programming) that directly interacts with the hardware using register manipulation. For example, specific GPIO pins are designated for LED and button functionality. The section further assumes a common system clock frequency of 84 MHz for timing calculations, which is a standard operating frequency for tasks that require time-sensitive operations.

Examples & Analogies

Think of programming as writing a recipe where each instruction directly relates to an action to be taken in the kitchen (hardware). Just like you might specify exact temperatures and times for baking, in this case, a precise system clock is set for the microcontroller to ensure timing is accurate for tasks like blinking an LED or reading a button press.

Program 1: Blinking On-board LED

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.

// Include the appropriate device header file for STM32F4xx series
#include "stm32f4xx.h"
// Simple software delay function
void delay_ms(unsigned int 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) {
    // Enable Clock for GPIOA
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
    // Configure PA5 as General Purpose Output
    GPIOA->MODER &= ~(GPIO_MODER_MODER5_Msk);
    GPIOA->MODER |= GPIO_MODER_MODER5_0;
    // Turn LED ON (Set PA5 high)
    GPIOA->ODR |= (1U << 5);
    delay_ms(500);
    // Turn LED OFF (Set PA5 low)
    GPIOA->ODR &= ~(1U << 5);
    delay_ms(500);
}

Detailed Explanation

This program is designed to continuously blink an LED on the STM32 board connected to pin PA5. It includes a function to create a delay, utilizing the system clock frequency to approximate a 1 millisecond delay for each cycle. In the main function, the first step is to enable the clock for GPIOA, configure PA5 as an output pin, and then enter a loop where the LED is turned on and off with appropriate delays.

Examples & Analogies

Imagine you are trying to signal someone by turning on and off a flashlight. In this program, the LED works like the flashlight, and the delay function is your count, allowing you to control how long the flashlight is on or off - giving the signal a rhythm.

Program 2: Reading Pushbutton Input and Controlling LED

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Objective: To read the state of the on-board User Button (PC13) and control the on-board LED (PA5). When the button is pressed, the LED should turn ON; when released, it should turn OFF. (Assuming the button is active-low: connected to GND when pressed, pulled high when released).

#include "stm32f4xx.h"
#define LED_PIN_NUM 5
#define BUTTON_PIN_NUM 13
int main(void) {
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable GPIOA clock
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable GPIOC clock
    GPIOA->MODER &= ~(GPIO_MODER_MODER5_Msk);
    GPIOA->MODER |= GPIO_MODER_MODER5_0;
    GPIOC->MODER &= ~(GPIO_MODER_MODER13_Msk); // Input mode
    GPIOC->PUPDR |= GPIO_PUPDR_PUPDR13_0; // Pull-up
    while (1) {
        if ((GPIOC->IDR & (1U << BUTTON_PIN_NUM)) == 0) {
            GPIOA->ODR |= (1U << LED_PIN_NUM);
        } else {
            GPIOA->ODR &= ~(1U << LED_PIN_NUM);
        }
    }
}

Detailed Explanation

This program reads the state of a user button and controls the LED based on whether the button is pressed or not. It begins similarly to Program 1 by enabling the GPIO clocks and configuring the necessary pins. The input for the button is set up with an internal pull-up resistor, which ensures that the pin reads HIGH when the button is not pressed and LOW when pressed (active-low configuration). The main loop continuously checks the button state and turns the LED on or off accordingly.

Examples & Analogies

Consider this program similar to a light switch controlling a light bulb. The button is the switch; when pressed (closed), current flows, and the light turns on. When released (open), the light turns off. Here, the microcontroller's GPIO pins act as your electrical wires linking your switch (button) and the light (LED).

Program 3: Delay Generation using a Timer (Polling Method)

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Objective: To generate a precise delay of 500 milliseconds using a general-purpose timer (e.g., TIM2) by polling its update flag, and use this delay to blink the on-board LED (PA5).

#include "stm32f4xx.h"
#define LED_PIN_NUM 5
void timer_delay_ms(unsigned int ms) {
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // Enable TIM2
    TIM2->PSC = 41999; // Set prescaler
    TIM2->ARR = ms - 1; // Set ARR
    TIM2->EGR |= TIM_EGR_UG; // Update event
    while (!(TIM2->SR & TIM_SR_UIF)); // Wait for UIF
    TIM2->SR &= ~TIM_SR_UIF; // Clear UIF
    TIM2->CR1 &= ~TIM_CR1_CEN; // Stop timer
}
int main(void) {
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
    GPIOA->MODER &= ~(GPIO_MODER_MODER5_Msk);
    GPIOA->MODER |= GPIO_MODER_MODER5_0;
    while (1) {
        GPIOA->ODR |= (1U << LED_PIN_NUM);
        timer_delay_ms(500);
        GPIOA->ODR &= ~(1U << LED_PIN_NUM);
        timer_delay_ms(500);
    }
}

Detailed Explanation

This program demonstrates how to generate delays using a hardware timer instead of software delay loops. It sets up a timer to count up for 500 milliseconds and uses polling to check if the timer has completed counting. The main loop functions similarly to the previous programs, blinking the LED on and off with precise timing dictated by the timer rather than CPU cycles.

Examples & Analogies

Imagine using a stopwatch instead of counting individually to time an event. The timer acts as your stopwatch, providing a reliable measurement of time to ensure the LED blinks with precision, unlike arbitrary counting where errors could occur over time. Using hardware for timing is much like trusting a stopwatch for accuracy rather than relying on a person to count seconds.

Definitions & Key Concepts

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

Key Concepts

  • GPIO Configuration: Configuring GPIO pins involves setting them to input or output modes for interaction with peripherals.

  • Timer Functionality: Using a timer improves delay accuracy and system resource efficiency compared to software loops.

  • Direct Register Access: Manipulating registers directly provides clear control over hardware settings and operations.

Examples & Real-Life Applications

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

Examples

  • Flashing the LED at 1-second intervals using a timer to manage delays accurately.

  • Reading and understanding button states to control outputs in a reactive manner.

Memory Aids

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

🎵 Rhymes Time

  • Timing is key, with timers we see, GPIO helps, blinking with glee.

📖 Fascinating Stories

  • Imagine a teacher named Mr. Timer who always reminds the students to check their clocks to set precise times for big games. One day, they learned to use prescalers to slow down their timer so they could catch every moment.

🧠 Other Memory Gems

  • TIPS (Timers, Input, Prescalers, States) help remember the essential components when discussing microcontrollers.

🎯 Super Acronyms

GPI (General Purpose Input) helps remember GPIO's function in interfacing.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: GPIO

    Definition:

    General Purpose Input/Output, a type of pin on a microcontroller that can be configured for input or output.

  • Term: Prescaler

    Definition:

    A register that divides the timer clock frequency to configure the timer's counting speed.

  • Term: ARR

    Definition:

    Auto-Reload Register, used to set the maximum count value for the timer.

  • Term: IDR

    Definition:

    Input Data Register, used to read the current state of input pins.

  • Term: ODR

    Definition:

    Output Data Register, used to set the state of output pins.