>Use Cases>
>N32G031_LED_BLINK

Blink an LED on N32G031

N32G031_LED_BLINK

Introduction

The LED Blink example is the foundation of embedded systems. It demonstrates how to initialize the MCU clock, configure GPIO pin registers as digital outputs, and toggle voltage levels with deterministic delay cycles.

Target MCU

Nations N32G031 Cortex-M0

Peripheral

GPIO Port A (PA1)

Frequency

0.5 Hz (1000ms ON / 1000ms OFF)

Video Demonstration

Wiring & Hardware Connection

Connect the LED and current-limiting resistor to the development board header as shown in the table below:

ComponentPinN32G031 Board PinNotes & Safety
LED (Red)Anode (+)PA1Connect via 220Ω – 330Ω resistor
LED (Red)Cathode (-)GNDCommon system ground

Behaviour & Execution Logic

The firmware executes a periodic loop configured through the N32G031 Standard Peripheral Library. It initializes the GPIOA peripheral clock, configures pin PA1 as a push-pull digital output, and toggles the pin voltage using hardware register operations and software delay cycles.

Stage 1: Initialization

Peripheral Clock & GPIO Init

Enables GPIOA clock via APB2 peripheral and configures PA1 as general-purpose push-pull output.

Stage 2: Infinite Loop

Toggle High/Low with Delay

Executes GPIO_SetBits for 1000ms ON time followed by GPIO_ResetBits for 1000ms OFF time cyclically.

main.c
#include "n32g031.h"
#include "n32g031_gpio.h"
#include "n32g031_rcc.h"

/* Simple delay function (approx. 1000 = 1 second) */
void Delay_ms_Simple(uint32_t ms) {
  for(volatile uint32_t i = 0; i < ms * 1200; i++) {
      __NOP();
  }
}

int main(void) {
  /* 1. Enable peripheral clock for GPIOA */
  RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);

  /* 2. Configure pin PA1 (Red LED) as Push-Pull Output */
  GPIO_InitType GPIO_InitStructure;
  GPIO_InitStruct(&GPIO_InitStructure);
  GPIO_InitStructure.Pin = GPIO_PIN_1; 
  GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP; 
  GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

  while(1) {
      /* -------- LED Blink Sequence -------- */
      GPIO_SetBits(GPIOA, GPIO_PIN_1);     // Turn ON Red LED
      Delay_ms_Simple(1000);               // Delay 1 second

      GPIO_ResetBits(GPIOA, GPIO_PIN_1);   // Turn OFF Red LED
      Delay_ms_Simple(1000);               // Delay 1 second
  }
}

Learning & AI Extension Ideas

Based on code analysis and execution structure, learners can explore several AI-assisted extensions:

1

Digital Outputs

Understand the concepts of HIGH (ON) and LOW (OFF) states in digital electronics and GPIO register operation.

2

Change the blink timing

Encourage students to modify delay times. Changing to 100ms mimics a strobe effect; 100ms ON / 2000ms OFF teaches duty cycle.

3

Blink in a heartbeat rhythm

Prompt AI to write a sequence of delays mimicking a human heartbeat (ba-bum... ba-bum...) rhythm.

4

Non-Blocking SysTick

Prompt AI to rewrite the blink logic using ARM SysTick interrupts for non-blocking multi-tasking execution.

Build and Flash Guide

1

Open Project: Open this project folder directly in the IDE.

2

Build & Flash: Simply click the Build and Flash buttons on the interface.