This tutorial is about interfacing 7 segment led display with stm32 microcontroller using keil arm mdk 5 with stm32 HAL libraries for code compilation. Stm32cubemx is used to initialize the stm32f103c8t6 microcontroller peripherals/variables pins, operating frequency, oscillator selection etc. Stm32cubemx initialized settings are then imported to keil mdk arm 5 project. Stm32cubemx automatically generates code for the ide we want to work in and in this project i am going to use keil arm mdk 5 ide. If you are new and don’t know about the stm32cubemx and keil uvision arm ide. I suggest you to learn more about the Stm32cubemx and keil uvision mdk arm ide used together stm32 HAL libraries for stm32 microcontroller code generation. Just click the below button to take the tutorial
Coming to the main project. In this tutorial i am going to interface a seven segment display with stm32f103 microcontroller. I will print numbers from 0 to 9 and characters from A to F on a single seven segment display. Seven segment display is composed of led’s arranged in a matrix form. Switching led’s on and off in a dedicated pattern prints the desired number or character on a 7 segment display. There are two types of seven segment displays common anode and common cathode. Common anode seven segment display has all led’s anode pin’s connected to a single point. Where as in common cathode all the cathode pin’s are connected to single point. To learn more about the difference between types, sizes and special 7 segment displays take the below tutorial. Click the below button to take the tutorial.
I am going to use common anode 7 segment display in this project. Stm32 microcontrollers are not easy to use. They do not come in DIP package. So its not possible to make a diy project with them on a bread board. We need to design a special pcb for working with stm32 microcontrollers which is still a time consuming work. Luckily their are number of pre assembled stm32 getting started boards available in market. They are not only cheap but are also easy to work with. I am also going to use a pre assembled stm32 board housing a stm32f103c8t6 microcontroller on it.
First an initialization is done in the stm32cubemx for stm32f103c8t6 microcontroller. 7 stm32 microcontoller pins are initialed as output which are going to be interfaced with the seven segment display. Pins are named same as seven segment display pins. Port-A pins of stm32 microcontroller are used in the project. Port-A of stm32 microcontroller is a 16-bit wide port. I am going to use its first 7 pins from 0 to 6 in the project. Stm32cubemx configuration for declaring pins as output is shown at the right side.
|
Project circuit diagram
The board which i am using in the project is in the below circuit diagram. Though in the board not all the GPIO pins of stm32f103 microcontroller are exposed but still it has pretty fair amount of GPIO pins to work with. Port-A of stm32f103 has 16 GPIO pins but only 12 are exposed on the board. Luckily port-a pins from 0 to 6 are present on the board and they are enough for our project need. I connected
- Port-A pin#0 is connected to pin-a of 7 segment display
- Port-A pin#1 is connected to pin-b of 7 segment display
- Port-A pin#2 is connected to pin-c of 7 segment display
- Port-A pin#3 is connected to pin-d of 7 segment display
- Port-A pin#4 is connected to pin-e of 7 segment display
- Port-A pin#5 is connected to pin-f of 7 segment display
- Port-A pin#6 is connected to pin-g of 7 segment display
Since i am using common anode 7 segment display so the 3.3 volt output pin of stm32 is connected to com pin of seven segment display. I uploaded the code in microcontroller using stlink v2 debugger.
Coming to the project code. Code is written in keil uvision mdk arm ide. Stm32 HAL libraries are used in the project. Stm32cubemx code configurator is used to configure the stm32 microcontroller oscillator, gpios and timers. Only the part in while 1 loop is important here. I wrote the code only in this loop. Remaining code is generated by the stm32cubemx ide.
ODR register of Stm32 is used to write to ports of stm32 microcontroller. Its a 16 bit register. To access this register the syntax of the code for HAL libraries is below. Since i am using Port-A of stm32 in the project so the syntax becomes
GPIOA->ODR = 0x003F; //Displaying 0
The above code writes a 16-bit word to Port-A of stm32 microcontoller. The 16 bit word is 0x003F. Its a 16 bit hexadecimal number. If we translate it to its equivalent binary the command translates to 0000 0000 0011 1111. Each binary bit is written to individual pin of port-a of stm32. The least bit starts from right and goes to the left highest. We are concerned with only first 7 pins of stm32 since we are using them in the project. So 011 1111 are written to first seven bits of stm32 microcontroller port-a. The highest port-a pin(6) will be written 0 and all the other remains high. Pin-6 of port-a is connected to seven segment display pin-g. What happens is when this word is written to port-a of stm32. The seven segment pin-g will turn off and all the other becomes on, making 0 on seven segment display. I hope this makes sense to you. All the other characters are displayed in the same form. Between each character a delay of 1 second is inserted to clearly see the character/number on seven segment display.
Future Work
You can interface a series of seven segment display’s with the single port of stm32 and control them with individual pins of stm32 for displaying numbers from 0 to 100. You can make a clock with seven segment display. Special led bars can be used to make special characters diagrams etc.
Future Work
You can interface a series of seven segment display’s with the single port of stm32 and control them with individual pins of stm32 for displaying numbers from 0 to 100. You can make a clock with seven segment display. Special led bars can be used to make special characters diagrams etc.
You may also like:
Filed Under: Electronic Projects, STM32
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.