Multiplexed Seven Segment Displays
Multiplexed Seven Segment Displays
Multiplexed Seven Segment Displays
0digg
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();
}
We will assemble the project in a breadboard as shown below.
The Code
/*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>
Note:
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
SEVEN_SEGMENT_PORT=0b11111101;
}
}
void Wait()
{
uint8_t i;
for(i=0;i<10;i++)
{
_delay_loop_2(0);
}
}
*/
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);
//Initialize Counter
TCNT0=0;
PORTB=0b00000110;
//Port D
SEVEN_SEGMENT_DDR=0XFF;
SEVEN_SEGMENT_PORT=0XFF;
//Infinite loop
//Print a number from 1 to 999
while(1)
{
for(i=0;i<1000;i++)
{
Print(i);
Wait();
}
}
}
ISR(TIMER0_OVF_vect)
{
/*
*/
static uint8_t i=0;
if(i==2)
{
//If on last display then come
//back to first.
i=0;
}
else
{
//Goto Next display
i++;
}
PORTB=~(1<<i);