Hi Friends,

In last tutorial we discussed about Multiplexing Seven Segment Displays. So you must be very much familiar with the theory. Now let us write the code and design a small project that will make you expert in using these displays in your own projects. We will make a system that can display any number between
0-9999 using four of these displays. We will design a function Print() that we can use on latter projects to easily write integers onto displays. Once we have successfully tested this function we can add to to any project without any problem. This concept of code reuse will make bigger project both easy to
make and far less painful. In this sample project we will test our function by using it in a loop to print all numbers from 0-9999.

   for(i=0;i<10000;i++)
   {
  	    Print(i);
       Wait();
   }

Things Required

S. No. Item Image
1

28 PIN AVR Dev Board

Contains the core AVR circuit including 5v regulator, reset, ISP.

AVR Development Board
2

Seven Segment Display Module

Four common anode displays multiplexed with driver transistors and current limiting resistors.

Seven Segment Board
3

Single Pin Female to Female Burg Wires

Used to interconnect the two boards

 

Burg Wires
4

USB AVR Programmer

To upload the program to the development board.

USB AVR Programmer

These are all the things required to get started with seven segment displays. The development board and the programmer can be used with many other projects to! While the seven segment board is handy for all projects requiring numeric displays.

Making !

Connect the seven segment board with the development board using the cables as per the table below.

  Seven Segment Board 28 PIN AVR Dev Board
  A PD0
  B PD1
  C PD2
  D PD3
  E PD4
  F PD5
  G PD6
  DO (DOT) PD7
  C1 PB3
  C2 PB2
  C3 PB1
  C4 PB0
  GND GND

 

seven segment board and 28pin avr dev board

Fig. Seven Segment Display Setup

The Circuit Diagram.

seven segment multiplexed display schematic

Fig – Multiplexed Seven Segment Displays.

The Code


/*

Description:   Program to demonstrate the use of Seven

            Segment Displays In Multiplexed Mode.
________________________________________________________

Author:     Avinash Gupta
Date:    11 Oct 08
Updated: 
Web:     www.eXtremeElectronics.co.in

   
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>


#define SEVEN_SEGMENT_PORT PORTD
#define SEVEN_SEGMENT_DDR  DDRD

volatile uint8_t digits[3];


void SevenSegment(uint8_t n,uint8_t dp)
{
/*
This function writes a digits given by n to the display
the decimal point is displayed if dp=1

Note:

n must be less than 9
*/
   if(n<10)
   {
      switch (n)
      {
         case 0:
                          //.gfedcba
         SEVEN_SEGMENT_PORT=0b00111111;
         break;

         case 1:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b00000110;
         break;

         case 2:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01011011;
         break;

         case 3:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01001111;
         break;

         case 4:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01100110;
         break;

         case 5:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01101101;
         break;

         case 6:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01111101;
         break;

         case 7:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b00000111;
         break;

         case 8:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01111111;
         break;

         case 9:
                        //.gfedcba
         SEVEN_SEGMENT_PORT=0b01101111;
         break;
      }
      if(dp)
      {
         //if decimal point should be displayed

         //make 7th bit high
         SEVEN_SEGMENT_PORT|=0b10000000;
      }
   }
   else
   {
      //This symbol on display tells that n was greater than 9
      //so display can't handle it

      SEVEN_SEGMENT_PORT=0b11111101;
   }
}

void Wait()
{
   uint8_t i;
   for(i=0;i<10;i++)
   {
      _delay_loop_2(0);
   }
}

void Print(uint16_t num)
{
   /* 

   
   This function breaks apart a given integer into separate digits
   and writes them to the display array i.e. digits[]
   
   */
   uint8_t i=0;
   uint8_t j;

   if(num>9999) return;


   while(num)
   {
      digits[i]=num%10;
      i++;

      num=num/10;
   }
   for(j=i;j<4;j++) digits[j]=0;
}


void main()
{
   uint16_t i;

   // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02);

   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

   //Initialize Counter

   TCNT0=0;

   //Port B[3,2,1,0] as out put
   DDRB|=0b00001111;

   PORTB=0b00000001;

   //Port D
   SEVEN_SEGMENT_DDR=0XFF;

   //Turn off all segments
   SEVEN_SEGMENT_PORT=0X00;

   //Enable Global Interrupts
   sei();

   //Infinite loop
   //Print a number from 1 to 9999
   while(1)
   {
      for(i=0;i<10000;i++)
      {
         Print(i);
         Wait();
      }

   }
}

ISR(TIMER0_OVF_vect)
{
   /*

   This interrupt service routine (ISR)
   Updates the displays

   */
   static uint8_t i=0;

   if(i==3)
   {
      //If on last display then come
      //back to first.
      i=0;
   }
   else
   {
      //Goto Next display
      i++;
   }

   //Activate a display according to i
   PORTB&=(0b11110000);
   PORTB|=(1<<i);

   //Write the digit[i] in the ith display.
   SevenSegment(digits[i],0);
}

Downloads