# Tutorial: 7-Segment Display

##

## Overview

Display decimal number (0\~9) on a 7-segment display

* Inputs: 4-bit numbers (D C B A) // (00001111)
* Output:
  * 7-segment decoder: 7-bit numbers ( a to g)
  * 7-segment display: decimal number 0\~9

#### Hardware Specification

* 7-segment display: common anode (5101ASR)
* 7-segment decoder: [74LS47N (datasheet download)](https://pdf1.alldatasheet.com/datasheet-pdf/download/5724/MOTOROLA/SN74LS47N.html)

Hardware

### 7-segment display (5101ASR)

Detail information about 7 segment display - [click here](https://www.electronics-tutorials.ws/combination/comb_6.html)

* Common anode: (common pin is connected to VCC)
* Giving ‘LOW’ to the pin -> LED ON
* Needs a load resistor for each pin

![7-segment circuit](https://user-images.githubusercontent.com/91526930/192131110-2fc8e880-10ef-4034-a4a2-9de59ecd42c1.png)

![image](https://user-images.githubusercontent.com/91526930/192942501-63b87284-7c94-4863-8200-106baa02b907.png)

![image](https://github.com/user-attachments/assets/5ca0e7ba-2620-4615-9aea-07aaf7e2aadb)

Please check the difference between the common cathode and common anode.

> We will use common anode.

###

### BCD 7-segment decoder

Model: [74LS47N (datasheet download)](https://pdf1.alldatasheet.com/datasheet-pdf/download/5724/MOTOROLA/SN74LS47N.html)

* All output pins are active low

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-e085887f7f0a2157f8ee0ffac998a5b54c9f513f%2Fimage.png?alt=media" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-a73c1f8fd4f215fa87864e7e990beabd61a5d5f6%2Fimage.png?alt=media" alt="" width="375"><figcaption></figcaption></figure>

###

### Array resistor (B331J)

![array resistor](https://user-images.githubusercontent.com/91526930/192131231-c6ae1c48-a236-43f8-9577-010ccd46eccc.png)

## Circuit Configuration

### Connecting with BCD 7-segment decoder

![image](https://github.com/user-attachments/assets/9f83a231-a63f-415f-b22c-07ebabcc86b6)

![image](https://github.com/user-attachments/assets/26b5a3a5-b0f0-406b-801a-4c0fd7cc930b)

### Connecting 7-segment without decoder

![image](https://github.com/user-attachments/assets/5ca0e7ba-2620-4615-9aea-07aaf7e2aadb)

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-78438cf7617d6bccdb8bd849b8850f209d1f07f9%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

![circuit on breadbord](https://user-images.githubusercontent.com/91526930/192194707-c62df336-9869-4de1-9d72-cb2355166989.png)

## Code

Tutorial code : "TU\_GPIO\_LED\_7segment\_student.c" [here](https://github.com/ykkimhgu/EC-student/tree/main/tutorial/tutorial-student)

* Creating 7-segment decoder (without using decoder chip)

If you want to use 7-segment decoder chip: [read about 7-segment and Decoder](https://ykkim.gitbook.io/ec/stm32-m4-programming/hardware/electronic-chips#7-segment-and-decoder)

```cpp
#include "stm32f4xx.h"
#include "ecRCC.h"
#include "ecGPIO.h"

void setup(void);
	
int main(void) {	
	// Initialiization --------------------------------------------------------
	setup();
	
	// Inifinite Loop ----------------------------------------------------------
	while(1){
		GPIO_write(PA_5, LOW);
		GPIO_write(PA_6, LOW);
		GPIO_write(PA_7, HIGH);
		GPIO_write(PB_6, HIGH);
		GPIO_write(PC_7, HIGH);
		GPIO_write(PA_9, LOW);
		GPIO_write(PA_8, LOW);
		GPIO_write(PB_10, LOW);
	}
}

void setup(void){
	RCC_HSI_init();
	GPIO_init(PA_5, OUTPUT);
	GPIO_init(PA_6, OUTPUT);
	GPIO_init(PA_7, OUTPUT);
	GPIO_init(PB_6, OUTPUT);
	GPIO_init(PC_7, OUTPUT);
	GPIO_init(PA_9, OUTPUT);
	GPIO_init(PA_8, OUTPUT);
	GPIO_init(PB_10, OUTPUT);
}
```
