💾
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 7-segment
  • Introduction
  • Exercise
  • Problem 0: Connection of 7-Segment Display and Decoder
  • Procedure
  • Problem 1: Display a Number with Button Press
  • Procedure
  • Configuration
  • Results
  • Discussion
  • Problem 2: Program BCD-7-segment decoder
  • Procedure
  • Configuration
  • Code
  • Connection Diagram
  • Results
  • Discussion
  • Reference
  • Troubleshooting

Was this helpful?

  1. EC Course
  2. LAB

LAB: GPIO Digital InOut 7-segment

LAB: GPIO Digital InOut 7-segment

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 to control a 7-segment display to show a decimal number (0~9) that increases by pressing a push-button.

You must submit

  • LAB Report (*.pdf)

  • Zip source files(LAB***.c, ecRCC2.h, ecGPIO2.h etc...).

    • Only the source files. Do not submit project files

Requirement

Hardware

  • MCU

    • NUCLEO-F411RE

  • Actuator/Sensor/Others:

    • 7-segment display(5101ASR)

    • Array resistor (330 ohm)

    • decoder chip(74LS47)

    • breadboard

Software

  • Keil uVision, CMSIS, EC_HAL library

Exercise

Fill in the table

Port/Pin

Description

Register setting

Port A Pin 5

Clear Pin5 mode

GPIOA->MODER &=~(3<<(5*2))

Port A Pin 5

Set Pin5 mode = Output

GPIOA->MODER |=____________

Port A Pin 6

Clear Pin6 mode

GPIOA->MODER &=~___________

Port A Pin 6

Set Pin6 mode = Output

GPIOA->MODER |=____________

Port A Pin Y

Clear PinY mode

GPIOA->MODER &=~___________

Port A Pin Y

Set PinY mode = Output

GPIOA->MODER |=____________

Port A Pin 5~9

Clear Pin5~9 mode

GPIOA->MODER &=~___________

Set Pin5~9 mode = Output

GPIOA->MODER |=____________

Port X Pin Y

Clear Pin Y mode

GPIOX->MODER &=~___________

Set Pin Y mode = Output

GPIOX->MODER |=____________

Port A Pin5

Set Pin5 otype=push-pull

GPIOA->OTYPER =____________

Port A PinY

Set PinY otype=push-pull

GPIOA-> OTYPER =____________

Port A Pin5

Set Pin5 ospeed=Fast

GPIOA->OSPEEDR =____________

Port A PinY

Set PinY ospeed=Fast

GPIOA-> OSPEEDR =____________

Port A Pin 5

Set Pin5 PUPD=no pullup/down

GPIOA->OTYPER =____________

Port A Pin Y

Set PinY PUPD=no pullup/down

GPIOA-> OTYPER =____________


Problem 0: Connection of 7-Segment Display and Decoder

Procedure

Review 7-segment Decoder and Display from Digital Logic lecture.

1. 7-segment display connection

First, connect the common anode 7-segment display with the given array resistors.

Then, apply VCC and GND to the 7-segment display.

Check that all LEDs of 7-segment work properly

  • Give 'H' signal to each 7-segment pin of 'a'~'g' . Observe if that LED is turned ON or OFF

  • Example: Connect VCC to all 'a'~'g' pins

2. BCD 7-segment decoder connection

The popular BCD 7-segment decoder chip is 74LS47. With the BCD chip, you need only 4 DOUT pins of MCU to display from 0 to 9.

Connect the decoder chip (74LS47) on the bread board.

Then, Check that the decoder chip works properly

  • Supply a combination of VCC/GND to the pins of 'A'~'D' of the decoder.

  • Check if the 7-segment LED display shows the correct number

Connection Diagram

Circuit diagram

You need to include the circuit diagram in the report

Discussion

  1. Draw the truth table for the BCD 7-segment decoder with the 4-bit input.

Answer discussion questions

** YOUR Truth-table  goes here**
  1. What are the common cathode and common anode of 7-segment display?

Answer discussion questions

  1. Does the LED of a 7-segment display (common anode) pin turn ON when 'HIGH' is given to the LED pin from the MCU?

Answer discussion questions


Problem 1: Display a Number with Button Press

Procedure

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

  • The project name is “LAB_GPIO_7segment”.

  • Create a new source file named as “LAB_GPIO_7segment.c”

  • You MUST write your name on the source file inside the comment section.

  1. Include your updated library in \repos\EC\lib\ to your project.

  • ecGPIO.h, ecGPIO.c

  • ecRCC.h, ecRCC.c

  1. Declare and Define the following functions in your library

void sevensegment_display_init(PinNames_t pinNameA, PinNames_t pinNameB, PinNames_t pinNameC, PinNames_t pinNameD); 
void sevensegment_display(uint8_t  num);
  • num: 0 to 9 only (unsigned)

  1. Configure and connect the MCU to the circuit

  2. First, check that every number, 0 to 9, can be displayed properly

  3. Then, create a code that increase the displayed number from 0 to 9 with each button press.

    • After the number '9', it should start from '0' again.

Configuration

Configure the MCU

Digital In for Button (B1)
Digital Out for 7-Segment

Digital In

Digital Out

PC13

PA7, PB6, PC7, PA9

PULL-UP

Push-Pull, No Pull-up-Pull-down, Medium Speed

Code

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

#define LED_PIN PA_5
#define BUTTON_PIN PC_13

void setup(void);
	
int main(void) { 
	// Initialiization --------------------------------------------------------
	setup();
	unsigned int cnt = 0;
	
	// Inifinite Loop ----------------------------------------------------------
	while(1){
		sevensegment_display(cnt % 10);
		if(GPIO_read(BUTTON_PIN) == 0) cnt++; 
        if (cnt > 9) cnt = 0;
		for(int i = 0; i < 500000;i++){}  // delay_ms(500);
	}
}


// Initialiization 
void setup(void)
{
	RCC_HSI_init();	
	GPIO_init(BUTTON_PIN, INPUT);  // calls RCC_GPIOC_enable()
	sevensegment_display_init(PA_7, PB_6, PC_7, PA_9);  // Decoder input A,B,C,D
}

Explain your source code with necessary comments.

// YOUR MAIN CODE ONLY
// YOUR CODE

Results

Experiment images and results

Show experiment images /results

Discussion

Analyze the result and explain any other necessary discussion.


Problem 2: Program BCD-7-segment decoder

Instead of using the decoder chip, we are going to make the 7-segment decoder with the MCU programming.

Do not use the 7-segment decoder for this problem

Procedure

  1. Use the same project and source file.

  2. Include your updated library in \repos\EC\lib\ to your project.

  • ecGPIO.h, ecGPIO.c

  • ecRCC.h, ecRCC.c

  1. Declare and Define the following functions in your library

void sevensegment_decoder_init(void); 
void sevensegment_decoder(uint8_t  num);
  • num: 0 to 9 only (unsigned)

  1. Configure and connect the MCU to the circuit

  2. First, check that every number, 0 to 9, can be displayed properly

  3. Then, create a code that increases the displayed number from 0 to 9 with each button press.

    • After the number '9', it should start from '0' again.

Configuration

Configure the MCU

Digital In for Button (B1)
Digital Out for 7-Segment

Digital In

Digital Out

PC13

PA5, PA6, PA7, PB6, PC7, PA9, PA8, PB10 ('a'~'h', respectively)

PULL-UP

Push-Pull, No Pull-up-Pull-down, Medium Speed

Code

Explain your source code with the necessary comments.

// YOUR MAIN CODE ONLY
// YOUR CODE

Connection Diagram

Circuit diagram

You need to include the circuit diagram

Results

Experiment images and results

Show experiment images /results

Discussion

Analyze the result and explain any other necessary discussion.


Reference

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


Troubleshooting

(Option) You can write Troubleshooting section

PreviousLAB: GPIO Digital InOutNextLAB: EXTI & SysTick

Last updated 8 months ago

Was this helpful?

Read here:

Read here:

image

Refer to the

.

Your code goes here:

Add

Your code goes here:

image

Add

7-segment tutorial
sample code
Sample Code
ADD Code LINK such as github
demo video link
ADD Code LINK such as github
Example Code for MCU configuration
Example code of 7-segment decoder control
demo video link
How to connect 7-segment decoder to MCU