💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • LAB: GPIO Digital InOut
  • Introduction
  • Requirement
  • Problem 0: STM-Arduino
  • GPIO Digital In/Out
  • Example code
  • Problem 1: Create EC_HAL library
  • Procedure
  • Problem 2: Toggle LED with Button
  • Procedure
  • Configuration
  • Code
  • Discussion
  • Problem 3: Toggle LED with Button
  • Procedure
  • Configuration
  • Circuit Diagram
  • Code
  • Results
  • Discussion
  • Reference
  • Troubleshooting

Was this helpful?

  1. EC Course
  2. LAB

LAB: GPIO Digital InOut

LAB: GPIO Digital InOut

Date: 2024-09-1

Author/Partner:

Github: repository link

Demo Video: Youtube link

PDF version:

Introduction

In this lab, you are required to create a simple program that toggle multiple LEDs with a push-button input. Create HAL drivers for GPIO digital in and out control and use your library.

You must submit

  • LAB Report (*.pdf)

  • Zip source files(main*.c, ecRCC.h, ecGPIO.h etc...).

    • Only the source files. Do not submit project files

Requirement

Hardware

  • MCU

    • NUCLEO-F411RE

  • Actuator/Sensor/Others:

    • LEDs x 3

    • Resistor 330 ohm x 3, breadboard

Software

  • Keil uVision, CMSIS, EC_HAL library


Problem 0: STM-Arduino

We are going to create a simple program that turns LED(LD2) on and off by pressing the user button(BT1), using Arduino Syntax

GPIO Digital In/Out

Create a new project under the directory \repos\EC\LAB\

  • The project folder name is “\LAB_GPIO_DIO_LED”.

Configures the specified pin to behave either as an input or an output.

pinMode(pin, mode)
  • pin: the pin number to set the model of.

  • mode: INPUT, OUTPUT or INPUT_PULLUP.

Look up for pinMode() function in arduino reference for detail description.

Example code

Open Arduino IDE and Create a new program named as ‘TU_arduino_GPIO_LED_button.ino’.

const int btnPin = 3;
const int ledPin = 13;

int btnState = HIGH;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT);
}

void loop() {
  btnState = digitalRead(btnPin);

  if (btnState == HIGH) 
    digitalWrite(ledPin, LOW);
  
  else 
    digitalWrite(ledPin, HIGH);
  
}

The user button pin is PC13, but this pin cannot be used in arduino. So, you should connect PC13 to pinName D3 by using wire.

Click on upload button. Push the reset button(black) and check the performance.

The LED(LD2) should be turned on when the button is pressed.


Problem 1: Create EC_HAL library

Procedure

Create the library directory \repos\EC\lib\.

DO NOT make duplicates of library files under each project folders

Create your own library for Digital_In and Out : ecGPIO2.h, ecGPIO2.c

  • Use the provided ecRCC2.h and ecRCC2.c

  • Modify ecGPIO2.c, ecGPIO2.h

ecRCC2.h (provided)

void RCC_HSI_init(void);  
void RCC_GPIOA_enable(void);   
void RCC_GPIOB_enable(void); 
void RCC_GPIOC_enable(void);

ecGPIO2.h

void GPIO_init(PinName_t pinName, int mode);
void GPIO_write(PinName_t pinName, int Output);
int  GPIO_read(PinName_t pinName);
void GPIO_mode(PinName_t pinName, int mode);
void GPIO_ospeed(PinName_t pinName, int speed);
void GPIO_otype(PinName_t pinName, int type);
void GPIO_pupd(PinName_t pinName, int pupd);

/*
// Version 1
void GPIO_init(GPIO_TypeDef *Port, int pin,  int mode);  
void GPIO_write(GPIO_TypeDef *Port, int pin,  int output);  
int  GPIO_read(GPIO_TypeDef *Port, int pin);  
void GPIO_mode(GPIO_TypeDef* Port, int pin, int mode);  
void GPIO_ospeed(GPIO_TypeDef* Port, int pin,  int speed);  
void GPIO_otype(GPIO_TypeDef* Port, int pin,  int type);  
void GPIO_pupd(GPIO_TypeDef* Port, int pin,  int pupd);
*/
  • Example code in ecGPIO2.c

/* ecGPIO2.c  */

// Input(00), Output(01), AlterFunc(10), Analog(11, reset)
void GPIO_mode(PinName_t pinName, uint32_t mode){
 	GPIO_TypeDef *port;
	unsigned int pin;
	ecPinmap(pinName, &port, &pin);
    
	port->MODER &= ~(3UL<<(2*pin));     
	port->MODER |= mode<<(2*pin);    
}


/*
// Version 1
// Input(00), Output(01), AlterFunc(10), Analog(11)
void GPIO_mode(GPIO_TypeDef *Port, int pin, int mode){
   Port->MODER &= ~(3UL<<(2*pin));     
   Port->MODER |= mode<<(2*pin);    
}
*/

Problem 2: Toggle LED with Button

Procedure

  1. Create a new project under the directory \repos\EC\LAB\

  • The project name is “LAB_GPIO_DIO_LED”.

  • Name the source file as “LAB_GPIO_DIO_LED.c”

2. Include your library ecGPIO2.h, ecGPIO2.c in \repos\EC\lib\.

You MUST write your name in the top of the source file, inside the comment section.

3. Toggle the LED by pushing the button.

  • Push button (LED ON), Push Button (LED OFF) and repeat

Configuration

Button (B1)
LED

Digital In

Digital Out

GPIOC, Pin 13

GPIOA, Pin 5

PULL-UP

Open-Drain, Pull-up, Medium Speed

Code

Your code goes here:

Explain your source code with necessary comments.

Sample Code

#include "ecRCC2.h"
#include "ecGPIO2.h"

#define LED_PIN PA_5
#define BUTTON_PIN PC_13

// Initialiization 
void setup(void) {
	RCC_HSI_init();
	// initialize the pushbutton pin as an input:
	GPIO_init(BUTTON_PIN, INPUT);  
	// initialize the LED pin as an output:
	GPIO_init(LED_PIN, OUTPUT);    
}
	
int main(void) { 
 	setup();
	int buttonState=0;
	
	while(1){
		// check if the pushbutton is pressed. Turn LED on/off accordingly:
		buttonState = GPIO_read(BUTTON_PIN);
		if(buttonState)	GPIO_write(LED_PIN, LOW);
		else 		GPIO_write(LED_PIN, HIGH);
	}
}

Discussion

  1. Find out a typical solution for software debouncing and hardware debouncing.

  2. What method of debouncing did this NUCLEO board use for the push-button(B1)?

Problem 3: Toggle LED with Button

Procedure

  1. Create a new project under the directory \repos\EC\LAB\

  • The project name is “LAB_GPIO_DIO_multiLED”.

  • Name the source file as “LAB_GPIO_DIO_multiLED.c”

You MUST write your name in the top of the source file, inside the comment section.

  1. Include your library ecGPIO2.h, ecGPIO2.c in \repos\lib\.

  2. Connect 4 LEDs externally with necessary load resistors.

  • As Button B1 is Pressed, light one LED at a time, in sequence.

  • Example: LED0--> LED1--> …LED3--> …LED0….

Configuration

Button
LED

Digital In

Digital Out

GPIOC, Pin 13

PA5, PA6, PA7, PB6

PULL-UP

Push-Pull, Pull-up, Medium Speed

Circuit Diagram

Circuit diagram

You need to modify the circuit diagram

Code

Your code goes here

Explain your source code with necessary comments.

// YOUR MAIN CODE ONLY

Results

Experiment images and results

Show experiment images /results

Discussion

  1. Find out a typical solution for software debouncing and hardware debouncing. What method of debouncing did this NUCLEO board use for the push-button(B1)?

Answer discussion questions

Reference

Complete list of all references used (github, blog, paper, etc)

Troubleshooting

(Option) You can write a Troubleshooting section

PreviousLAB: Portable Fan with mbedNextLAB: GPIO Digital InOut 7-segment

Last updated 8 months ago

Was this helpful?

Write the following source code: .

Save your header library files in this directory.

Use the .

image

Add

source code
See here for detail.
Download library files from here
example code provided here
demo video link