Monday, June 1, 2015

4-digits 7-Segment Display Interfacing with AVR microcontroller

  This code demonstrate How to interface 7-Segment display with AVR microcontroller. In this tutorial we are using 4-Digits of 7-Segment to display numbers, integer values, etc. 7-Segment display are commonly used for displaying voltage,current, temperature. This code is modular and simple to use customize or your application.

Components Required:
1. 7-Segment Display Qty-4
2. ATmega8 Microcontroller
3. BC548 Transistor Qty-4
4. 1KOhm Resistors
5. 330Ohm Resistors

Circuit Diagram:
7 segment display pinout

7 segment display interfacing Circuit Diagram


AVR Studio C Code:

//=========================================================================
/*
 * 7 Segment Display With Decoder ATmega8 Interfacing
 *
 * Created: 02-02-2015 19:16:01
 *  Author: Komal Manoj Thakur
 */ 
//=========================================================================

#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#define F_CPU 1000000UL
#include <util/delay.h>

#define Display_Port PORTB
#define Display_PortDir DDRB

int i, value;
char digit[4];
//=========================================================================
//   Main Function
//=========================================================================
int main(void)
{
 Display_PortDir=0xFF;
 
 TCCR0=0x04; //Set prescaling to 4, so Timer clock = Fosc/256
 TCNT0=0x91; //Timer 0 is 8-Bit Timer ADJUST TO REDUCE FLICKER
 TIMSK = 0x01; //Timer 0 Overflow flag interrupt enable
 sei();   //SREG= 0x80; Global Interrupt enable
  
 //digit[0]='5';  //Numbers to be displayed on display
 //digit[1]='6';
 //digit[2]='7';
 //digit[3]='8';
 
 value=1234;    //To display integer value on display
 sprintf(digit,"%04d",value);
 
    while(1)
    {
        //TODO:: Please write your application code 

  _delay_ms(1000);
    }
}


//=========================================================================
//                 Display Scanning Timer Interrupt
//=========================================================================
 SIGNAL(TIMER0_OVF_vect)
{
 i++;
 if(i==1)
 { Display_Port = (0x80 | (0x0F & digit[0])); }
 if(i==2)
 { Display_Port = (0x40 | (0x0F & digit[1])); }
 if(i==3)
 { Display_Port = (0x20 | (0x0F & digit[2])); }
 if(i==4)
 { Display_Port = (0x10 | (0x0F & digit[3])); i=0;}
 
 
 TCNT0=0x91;  //Reload the count ADJUST TO REDUCE FLICKER
}
//=========================================================================

Comment if you face any problem


2 comments:

  1. sir i have purchased your book aurdino displaying interface in that i tried i have copied the 7 segment display code but it ishowing an error like timer1 was not declared in
    the scope please help me

    ReplyDelete
    Replies
    1. You need to add Timer1 Library in your arduino Library folder

      Delete