EGR 226 HOMEWORK #3   Generate a code (in C) for the STM32F446RE microcontroller on the Nucleo-64 dev board with the following features The program is to blink the LED attached to pin PA5 Uses the SysTick to keep time. The period of the LED blink is to be 1s, 0.5s, and 0.25s. The period is selectable with the button attached to pin PC13 such that it starts at 1s.  When the button is pressed the blink will have 0.5s.  Then 0.25s.  Another press will return the period to 1s.    Problem #1: Do not include the "stm32f4xx.h" header.  Generate the code without using a header and directly addessing each of the registers using their memory locations.   Problem #2: Do not include the "stm32f4xx.h" header. Generate the same program using preprosessor directives (#define's) to more readably set the registers.   Problem #3: Include the "stm32f4xx.h" header.Generate the same program using the register definitions included in "stm32f4xx.h" header.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
  1. EGR 226 HOMEWORK #3

     

    Generate a code (in C) for the STM32F446RE microcontroller on the Nucleo-64 dev board with the following features

    • The program is to blink the LED attached to pin PA5
    • Uses the SysTick to keep time.
    • The period of the LED blink is to be 1s, 0.5s, and 0.25s.
    • The period is selectable with the button attached to pin PC13 such that it starts at 1s.  When the button is pressed the blink will have 0.5s.  Then 0.25s.  Another press will return the period to 1s. 

     

    Problem #1:

    Do not include the "stm32f4xx.h" header.  Generate the code without using a header and directly addessing each of the registers using their memory locations.

     

    Problem #2:

    Do not include the "stm32f4xx.h" header. Generate the same program using preprosessor directives (#define's) to more readably set the registers.

     

    Problem #3:

    Include the "stm32f4xx.h" header.Generate the same program using the register definitions included in "stm32f4xx.h" header.

     

    Submit the three programs

Expert Solution
Step 1

Problem#1:

#include <stdint.h>

// Memory locations of peripherals
#define RCC_AHB1ENR (*((volatile uint32_t*)0x40023830))
#define RCC_APB2ENR (*((volatile uint32_t*)0x40023844))
#define GPIOA_MODER (*((volatile uint32_t*)0x40020000))
#define GPIOA_ODR (*((volatile uint32_t*)0x40020014))
#define GPIOC_MODER (*((volatile uint32_t*)0x40020800))
#define GPIOC_IDR (*((volatile uint32_t*)0x40020810))

// Bitmasks for peripherals
#define LED_PIN (1 << 5)
#define LED_PORT (1 << 10)
#define BUTTON_PIN (1 << 13)
#define BUTTON_PORT (1 << 26)
#define RCC_AHB1ENR_GPIOAEN (1 << 0)
#define RCC_AHB1ENR_GPIOCEN (1 << 2)
#define RCC_APB2ENR_SYSCFGEN (1 << 14)

// Delay function using SysTick timer
void delay(uint32_t ticks) {
    uint32_t tickStart = (*((volatile uint32_t*)0xE000E018));
    while (((*((volatile uint32_t*)0xE000E018)) - tickStart) < ticks) {
    }
}

int main(void) {
    // Enable clock for GPIOA and GPIOC
    RCC_AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN;
    RCC_APB2ENR |= RCC_APB2ENR_SYSCFGEN;

    // Configure LED as output
    GPIOA_MODER |= LED_PORT;

    // Configure button as input
    GPIOC_MODER &= ~BUTTON_PORT;

    uint32_t period = 8000000; // 1s delay at 8 MHz

    while (1) {
        GPIOA_ODR ^= LED_PIN; // Toggle LED
        delay(period / 2);

        if (!(GPIOC_IDR & BUTTON_PIN)) { // Check button press
            if (period == 8000000) {
                period = 4000000; // 0.5s delay
            } else if (period == 4000000) {
                period = 2000000; // 0.25s delay
            } else {
                period = 8000000; // 1s delay
            }
            while (!(GPIOC_IDR & BUTTON_PIN)) {
            }
        }
    }

    return 0;
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education