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.
-
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
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
This is a popular solution!
Step by step
Solved in 3 steps