>Use Cases>
>N32G031_LED_SEQUENCE

Sequence onboard LEDs

N32G031_LED_SEQUENCE

Introduction

An introductory project designed to teach basic GPIO (General Purpose Input/Output) control using the N32G031 microcontroller. This project expands on the basic "Hello World" by controlling the on-board LEDs sequentially across pins PB1, PB3, PB6, and PB7. It serves as an excellent tool for understanding sequential logic and timing functions without the need for any external wiring. This project is fully optimized for cross-platform workflows using UnityMbed.

Target MCU

Nations N32G031 Cortex-M0

Active Pins

PB1, PB3, PB6, PB7

Mode

Onboard Sequential Chaser

Video Demonstration

Wiring & Hardware Connection

No External Wiring Required: This project utilizes the LEDs already integrated on the UnityMbed Starter Kit board labeled PB7, PB6, PB3, and PB1.

DevicePinN32G031 Board PinNotes & Remarks
Onboard LED 1LED 1PB1Onboard SMD LED 1
Onboard LED 2LED 2PB3Onboard SMD LED 2
Onboard LED 3LED 3PB6Onboard SMD LED 3
Onboard LED 4LED 4PB7Onboard SMD LED 4

Behaviour & Execution Logic

Once powered on and flashed with the code, the microcontroller will execute the following loop continuously to create a "chasing" effect:

Stage 1: Register Setup

Clock & Output PMODE

Configures GPIOB PMODE and POTYPE registers for PB1, PB3, PB6, and PB7 as fast push-pull outputs.

Stage 2: Sequential Chaser

PBSC Bit Set/Reset Pipeline

Sequentially turns ON/OFF PB1 -> PB3 -> PB6 -> PB7 using PBSC register manipulation.

main.c
// UnityMbed — blink LED on PB1, PB3, PB6, PB7 @ N32G031
#include "n32g031.h"

static void delay(volatile uint32_t n) { while (n--) __NOP(); }

int main(void) {
  /* Enable GPIOB clock via APB2 bit 3 */
  RCC->APB2PCLKEN |= (1u << 3);

  /* Configure PB1, PB3, PB6, PB7 as push-pull output */
  // Clear PMODE bits for PB1[3:2], PB3[7:6], PB6[13:12], PB7[15:14]
  GPIOB->PMODE &= ~((0x3U << 2) | (0x3U << 6) | (0x3U << 12) | (0x3U << 14));
  // Set PMODE to 01 (General purpose output)
  GPIOB->PMODE |=  ((0x1U << 2) | (0x1U << 6) | (0x1U << 12) | (0x1U << 14)); 
  
  // Clear POTYPE bits to 0 (Push-pull) for PB1, PB3, PB6, PB7
  GPIOB->POTYPE &= ~((1U << 1) | (1U << 3) | (1U << 6) | (1U << 7));                                 
  
  // Turn off all LEDs initially (Write 1 to upper 16 bits of PBSC to clear)
  GPIOB->PBSC = (1U << (1 + 16)) | (1U << (3 + 16)) | (1U << (6 + 16)) | (1U << (7 + 16));

  while (1) {
      // PB1 ON
      GPIOB->PBSC = (1U << 1);
      delay(400000);
      GPIOB->PBSC = (1U << (1 + 16)); // PB1 OFF
      
      // PB3 ON
      GPIOB->PBSC = (1U << 3);
      delay(400000);
      GPIOB->PBSC = (1U << (3 + 16)); // PB3 OFF
      
      // PB6 ON
      GPIOB->PBSC = (1U << 6);
      delay(400000);
      GPIOB->PBSC = (1U << (6 + 16)); // PB6 OFF
      
      // PB7 ON
      GPIOB->PBSC = (1U << 7);
      delay(400000);
      GPIOB->PBSC = (1U << (7 + 16)); // PB7 OFF
  }
}

Learning & AI Extension Ideas

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

1

Multi-Pin Control

Understand HIGH/LOW states and learn to manipulate multiple GPIO pins simultaneously via bitwise operations.

2

Change the chase speed

Modify delay(400000) in code. Changing to 50000 creates high-speed fluid scanning effects.

3

Bounce the sequence back and forth

Prompt AI to modify the loop sequence to make the LEDs bounce back and forth (PB1 -> PB3 -> PB6 -> PB7 -> PB6 -> PB3 -> PB1).

4

Blink LEDs in pairs

Ask AI to write logic turning ON PB1 and PB7 simultaneously, followed by PB3 and PB6 in symmetric harmony.

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.