Interfacing a Seven-Segment Display to a Microcontroller

 Difference between a micro-controller and a microprocessor

    The microcontroller contains a processing unit, a small amount of memory (RAM, ROM, EPROM, etc.), a few I/O ports for peripherals, a timer, etc. on a single chip. Therefore, we can think it as a tiny resemblance of a microcomputer.

    On the other hand, a microprocessor contains only a processing unit which is quite powerful in computation. A microprocessor cannot be used stand-alone which means it needs extra hardware chips such as memory, peripheral boards, system bus, etc. Microprocessors are entirely responsible for processing data based on instructions to produce information.

Parameter

Microcontroller

Microprocessor

Role

Acts as the heart of an embedded system

Acts as the heart of a computer system

Circuit Complexity

Less complex due to present-on-chip memory

More complex due to external connections

Memory and I/O components

Available

Connected externally

Efficiency

Efficient

Not efficient

Power consumption

Low

High

Zero status flag

Doesn’t have

Have

Number of registers

Have a greater number of registers

Have a smaller number of registers

Applications

Washing machines, Air conditioners, etc.

Personal Computers

GPIO pins available in Atmega328P

The Atmega328P microcontroller has a total of 23 General Purpose Input/Output (GPIO) pins, numbered from 0 to 22. These pins can be used for a variety of purposes, such as driving LEDs, reading sensors, and communicating with other devices. Additionally, some of these pins have specialized functions, such as providing hardware support for serial communication or PWM (Pulse Width Modulation) output. The specific functions of each pin can be configured using software, such as the Arduino IDE.

Configuring a GPIO pin in Atmega328P as a digital input

1. First select the pin you want to configure as an input.

2. Clear the corresponding bit in the Data Direction Register (DDR) for that pin to set the direction of the pin as input. That means to configure a pin as an input, the corresponding bit in the DDR must be 0.

3. To ensure a stable input signal when the pin is not connected to an external device, the pull-up resistor of the pin can be enabled. To achieve this, the corresponding bit in the Port register (PORT) must made to 1.

Reading the logic level of a digital input pin

To read the logic level of a digital input pin in Atmega328P, the ‘digitalRead()’ function must be used in your code. The number of the pin you want to read must be given as an argument to the function. Depending on the logic level of the pin, it will return HIGH or LOW.

Bootloader program

During a device start-up, data from an operating system must be loaded into the working memory. This is made possible by a bootloader. Therefore, after starting the device, a bootloader is generally launched by a bootable medium like a hard drive, a CD/DVD, or a USB stick.

In a microcontroller, a bootloader program is used to load and start the main program or firmware. It is stored in a separate memory section from the main program and is automatically executed when it is powered on or reset.

Delay function in Arduino IDE

The ‘delay()’ function in Arduino IDE is used to pause the execution of a program for a specified period of time. The user must provide the time period to delay in milliseconds as an argument.

This function causes the microcontroller to stop executing instructions for a specified amount of time. It is implemented using a hardware timer on the microcontroller.


Atmel code for ATMEGA328 0-9 Up counter with 7 segment display

#define F_CPU 8000000UL

#define BTN (PINK&0x01)

#include <avr/io.h>

#include <util/delay.h>

#include <avr/sfr_defs.h>

void decode(unsigned char count){

       //this is how decode for the 7 segment just lit the corresponding LEDs to the

       //number

       if(count ==1 ){ PORTF = 0B00001100; }

       else if(count == 2){ PORTF = 0B00110111; }

       else if(count == 3){ PORTF = 0B00011111; }

       else if(count == 4){ PORTF = 0B01001101; }

       else if(count == 5){ PORTF = 0B01011011; }

       else if(count == 6){ PORTF = 0B01111011; }

       else if(count == 7){ PORTF = 0B00001110; }

       else if(count == 8){ PORTF = 0B01111111; }

       else if(count == 9){ PORTF = 0B01011111; }

       else { PORTF = 0B01111110; }

}

int main(void)

{

       //port D is connected to 7 segment

       DDRF = 0B01111111;

       PORTF= 0B11111111;

       //port B is connected to push button

       DDRK = 0B00000000;

       //PORTB= 0x0F;

       // PORTF = 0B01111110;

       PORTF = 0B01111111;

 

       unsigned char x = 0;

       while (1)

       {

            if(BTN == 0)

            {

                        x++; //increment counter

                        if(x == 10){

                                    x=0; //to set counter to zero overflow on 10

                        }

                        decode(x); //decoding the number on 7 segment

                        _delay_ms(1000);

            }

       }

}

Atmel code for ATMEGA328 0-9 Up / Down counter with 7 segment display

#define F_CPU 8000000UL

#define BTN (PINK&0x01)

#define BTN2 (PINK&0x02)

#include <avr/io.h>

#include <util/delay.h>

#include <avr/sfr_defs.h>

void decode(unsigned char count){

       //this is how decode for the 7 segment just lit the corresponding LEDs to the

       //number

       if(count ==1 ){ PORTF = 0B00001100; }

       else if(count == 2){ PORTF = 0B00110111; }

       else if(count == 3){ PORTF = 0B00011111; }

       else if(count == 4){ PORTF = 0B01001101; }

       else if(count == 5){ PORTF = 0B01011011; }

       else if(count == 6){ PORTF = 0B01111011; }

 

       else if(count == 7){ PORTF = 0B00001110; }

       else if(count == 8){ PORTF = 0B01111111; }

       else if(count == 9){ PORTF = 0B01011111; }

       else { PORTF = 0B01111110; }

}

int main(void)

{

       //port D is connected to 7 segment

       DDRF = 0B01111111;

       PORTF= 0B11111111;

       //port B is connected to push button

       DDRK = 0B00000000;

       //PORTB= 0x0F;

       // PORTF = 0B01111110;

       PORTF = 0B01111111;

       unsigned char x = 0;

       while (1)

       {

            if(BTN == 0)

            {

                        x++; //increment counter

                        if(x == 10){

                                    x=0; //to set counter to zero overflow on 10

                        }

                        decode(x); //decoding the number on 7 segment

                        _delay_ms(1000);

            }

           

            if(BTN2 == 0)

            {

                         //Decrement counter

                        if(x == 0){

                                    x=9;

 

                        }

                        else

                                    x--;

                        decode(x); //decoding the number on 7 segment

                        _delay_ms(1000);

            }

       }

}


Reference

1.     “What's difference between Microcontroller (µC) and microprocessor (µP)?,” GeeksforGeeks, 02-Nov-2022. [Online]. Available: https://www.geeksforgeeks.org/whats-difference-between-microcontoller-%c2%b5c-and-microprocessor-%c2%b5p/. [Accessed: 20-Feb-2023].

2.     “Difference between microprocessor and Microcontroller,” Tutorials Point. [Online]. Available: https://www.tutorialspoint.com/difference-between-microprocessor-and-microcontroller. [Accessed: 20-Feb-2023].

3.     “What is a bootloader and how does it work?,” IONOS Digital Guide. [Online]. Available: https://www.ionos.com/digitalguide/server/configuration/what-is-a-bootloader/. [Accessed: 20-Feb-2023]. 


Comments