7-Segment Display Interfacing with AVR ATmega16/ATmega32

7-segment displays comprise 8 LED elements – 7 linear segments that form numerals, plus a circular dot. Numbers 0-9 and some letters (A, C, D, E, F, H, L, O, P, U) are represented using combinations of the 7 lines. The dot creates decimals.

Each element connects to a dedicated pin, allowing individual control. Pins receive high or low signals to turn elements on/off based on the desired character.

7-segment displays come in common anode or common cathode configurations, impacting signal requirements. With common anode, segments are activated by a low signal. Common cathode uses high signals.

For more details on 7-segment displays, including interfacing and character mapping, please refer to the dedicated topic in the sensors and modules section. Proper understanding of the display type and driving signals is crucial for successful implementation in code.

Connection Diagram of 7-segment with ATmega16/32

Interfacing 7-Segment LED Display With AVR ATmega16/ATmega32

7-segment Display Code for ATmega16/32

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#define LED_Direction DDRA /* define LED Direction */
#define LED_PORT PORTA /* define LED port */
int main(void)
{
LED_Direction |= 0xff;/* define LED port direction is output */
LED_PORT = 0xff;
char array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
/* write hex value for CA display from 0 to 9 */
    while(1)
    {
for(int i=0;i<10;i++)
{
LED_PORT = array[i]; /* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
    }
}

Connection Diagram of 7-segment using Driver IC SN7446AN with ATmega16/32

7-segment LED Display Code using driver IC SN7446AN for ATmega16/32

/*
 * ATmega16_7_Segment_Project_File.c
 * http://www.electronicwings.com
 */
/*
 * ATmega16_BCD_to_7_Segment.c
 *
 * http://www.electronicwings.com
 */
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#define LED_direction DDRA /* define LED Direction */
#define LED_PORT PORTA /* define LED PORT */
int main(void)
{
LED_direction |= 0xff;/* define LED port direction is output */
LED_PORT = 0xff;
char array[]={0,1,2,3,4,5,6,7,8,9};
/* write BCD value for CA display from 0 to 9 */
while(1)
{
for(int i=0;i<10;i++)
{
LED_PORT = array[i];/* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
}
}

7-segment Display Multiplexing Connection With ATmega16/32

7-segment Display Multiplexing Code for ATmega16/32

/*
 * ATmega16_7seg_multiple.c
 *
 * http://www.electronicwings.com
 */
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#define LED_Directions DDRA /* define LED Direction */
#define LED_Direction DDRB /* define LED Direction */
#define LED_PORT1 PORTA /* define LED port */
#define LED_PORT2 PORTB /* define LED port */
char array[]={0,1,2,3,4,5,6,7,8,9};
int k,j,i,factor;
int brightvalue=0;
void set_brightness()
{
int brightness = brightvalue;
while (0 < brightness)
{
_delay_us(1);
–brightness;
}
}
ISR(TIMER0_OVF_vect)
{
LED_PORT2 = 0x01;
LED_PORT1 = array[k];
set_brightness();
LED_PORT2 = 0x02;
LED_PORT1 = array[j];
set_brightness();
LED_PORT2 = 0x04;
LED_PORT1 = array[i];
set_brightness();
}
void SevenSeg_SetNumber(int num)
{
k=num%10;
num = num/10;
j=num%10;
num = num/10;
i=num%10;
num = num/10;
}
void sevseg_refreshDisplay(char refvalue)
{
TIMSK=(1<<TOIE0);/* Enable Timer0 overflow interrupts */
TCNT0 = refvalue;/* load TCNT0, count for 10ms*/
TCCR0 = (1<<CS02) | (1<<CS00);  /* start timer0 with /1024 prescaler*/
}
int main(void)
{
sei();
LED_Directions = 0xff;/* define port direction is output */
LED_Direction = 0xff;/* define port direction is output */
LED_PORT1 = 0xff;
LED_PORT2 = 0xff;
sevseg_refreshDisplay(0xC2);/* set refresh rate of display (for 10ms set 0xC2) */
brightvalue=1000;/* set brightness level of 7 segment display */
while(1)
{
SevenSeg_SetNumber(456);/* set value to display */
_delay_ms(1000);/* wait for 1 second */
SevenSeg_SetNumber(789);
  _delay_ms(1000);
}
}

About The Author

Ibrar Ayyub

Ibrar Ayyub is an experienced technical writer with a Master's degree in computer science from BZU Multan University. He has written for various industries, mainly home automation, and engineering. He has a clear and simple writing style and is skilled in using infographics and diagrams. He is a great researcher and is able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top