Saturday 29 October 2011

7segment array


Hi Friends, In last tutorial we discussed about Multiplexing Seven Segment Displays. So you must be very much familier with the therory. 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-999 using three of these displays. We will write a function Print() that we can use on own later 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 painfull. In this sample project we will test our function by using it in a loop to print all numbers from 0-999.
for(i=0;i<1000;i++)
{ Print(i);
Wait();
}

The Circuit Diagram.

You can use a Home Made AVR Devboard or a low cost xBoard MINI to experiment with seven segment displays. In the picture I have used xBoard MINI. The project is designed with ATmega8 but can easily be done through ATmega16 or ATmega32. The crystal value is not so critical for this you can use 12 MHz or 16 MHz it would only change the refresh rate of displays.
Multiplexed Seven Segment Displays.

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
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:
SEVEN_SEGMENT_PORT=0b00000011;
break;
case 1:
SEVEN_SEGMENT_PORT=0b10011111;
break;
case 2:
SEVEN_SEGMENT_PORT=0b00100101;
break;
case 3:
SEVEN_SEGMENT_PORT=0b00001101;
break;
case 4:
SEVEN_SEGMENT_PORT=0b10011001;
break;
case 5:
SEVEN_SEGMENT_PORT=0b01001001;
break;
case 6:
SEVEN_SEGMENT_PORT=0b01000001;
break;
case 7:
SEVEN_SEGMENT_PORT=0b00011111;
break;
case 8:
SEVEN_SEGMENT_PORT=0b00000001;
break;
case 9:
SEVEN_SEGMENT_PORT=0b00001001;
break;
}
if(dp)
{
//if decimal point should be displayed
//make 0th bit Low
SEVEN_SEGMENT_PORT&=0b11111110;
}
}
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 separete digits
and writes them to the display array i.e. digits[]
*/
uint8_t i=0;
uint8_t j;
if(num>999) return;
while(num)
{
digits[i]=num%10;
i++;
num=num/10;
}
for(j=i;j<3;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[2,1,0] as out put
DDRB|=0b00000111;
PORTB=0b00000110;
//Port D
SEVEN_SEGMENT_DDR=0XFF;
//Turn off all segments
SEVEN_SEGMENT_PORT=0XFF;
//Enable Global Interrupts
sei();
//Infinite loop
//Print a number from 1 to 999
while(1)
{
for(i=0;i<1000;i++)
{
Print(i);
Wait();
ISR(TIM
} } }
ER0_OVF_vect) {
/*
This interrup service routine (ISR)
Updates the displays
*/
static uint8_t i=0;
if(i==2)
{
//If on last display then come
//back to first.
i=0;
}
else
{
//Goto Next display
i++;
}
//Acivate a display according to i
PORTB=~(1<<i);
//Write the digit[i] in the ith display.
SevenSegment(digits[i],0);
}
© 2011 Electroclub

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Raghu | Protected by - ElectroClub