We have four 7-segment displays connected to the same port on the 8051. Because the circuit is connected in this way we have to multiplex the output.
1. switch on display 1 and switch off the other 3 displays (2,3,4)
2. switch on display 2 and switch off the other 3 displays (1,3,4)
3. switch on display 3 and switch off the other 3 displays (1,2,4)
4. switch on display 4 and switch off the other 3 displays (1,2,3)
If we do this fast enough, we will not be able to notice that one of the 7-segment displays is on and other 7-segment displays are off.
The next question is how we are supposed to turn the 7-segment displays off and on. You will notice that there are two pins on each of the the 7-segment displays which correspond to common anode. If we set these pins to +5V the 7-segment display is on. If we set these pins to 0V the 7-segment display if off. The four common anode pins corresponding to the ports (P2.7, P2.6, P2.5, P2.4). If we want to turn the first display on we simply set P2.7 to 1. If we want to turn the first display off we set P2.7 to 0. The same applies for the other displays connected to P2.6, P2.5, P2.4. The problem we then encounter is that the 8051 cannot drive components. Althougth we set a particular pin to 1, the 8051 does not provide the +5V needed by the 7-segment display. To remedy this we must use transistors. The schematic below shows how to connect these.
main.c
#include
extern unsigned char digit1;
extern unsigned char digit2;
extern unsigned char digit3;
extern unsigned char digit4;
extern void timer_enable(void);
void main(void)
{
timer_enable();
while(1){
digit1=0x00;
digit2=0x01;
digit3=0x06;
digit4=0x03;
}
}
seven.c
#include
void display_digit(unsigned char);
#define dis_a P0_2
#define dis_b P0_3
#define dis_c P0_4
#define dis_d P0_6
#define dis_e P0_5
#define dis_f P0_1
#define dis_g P0_0
#define disp1 P2_7
#define disp2 P2_6
#define disp3 P2_5
#define disp4 P2_4
unsigned char count_val;
unsigned char digit1;
unsigned char digit2;
unsigned char digit3;
unsigned char digit4;
void timer_enable(void)
{
/*------------------------------------
Timer 0 routine for Display.
--------------------------------------*/
count_val=0x00; /*Clear Count value*/
TMOD = (TMOD & 0x0F) | 0x20; /* Set Mode (8-bit timer with reload) */
TH1 = 0x00; /* Reload TL1 to count 100 clocks */
TL1 = 0xff;
ET1 = 1; /* Enable Timer 1 Interrupts */
TR1 = 1; /* Start Timer 1 Running */
EA = 1; /* Global Interrupt Enable */
/*--------------------------------------*/
}
void timer1_ISR (void) interrupt 3
{
disp1=0;
disp2=0;
disp3=0;
disp4=0;
if (count_val==1){
disp1=1;
disp2=0;
disp3=0;
disp4=0;
display_digit(digit1);
}
else if (count_val==2){
disp1=0;
disp2=1;
disp3=0;
disp4=0;
display_digit(digit2);
}
else if (count_val==0x03){
disp1=0;
disp2=0;
disp3=1;
disp4=0;
display_digit(digit3);
}
else if (count_val==0x04){
disp1=0;
disp2=0;
disp3=0;
disp4=1;
display_digit(digit4);
count_val=0x00;
}
count_val++;
}
/*---------------------------------------------------
Display the data passed to this function
----------------------------------------------------*/
void display_digit(unsigned char digg)
{
if (digg==0x00)
{
dis_a=0;
dis_b=0;
dis_c=0;
dis_d=0;
dis_e=0;
dis_f=0;
dis_g=1;
}
else if (digg==1)
{
dis_a=1;
dis_b=0;
dis_c=0;
dis_d=1;
dis_e=1;
dis_f=1;
dis_g=1;
}
else if (digg==2)
{
dis_a=0;
dis_b=0;
dis_c=1;
dis_d=0;
dis_e=0;
dis_f=1;
dis_g=0;
}
else if (digg==3)
{
dis_a=0;
dis_b=0;
dis_c=0;
dis_d=0;
dis_e=1;
dis_f=1;
dis_g=0;
}
else if (digg==4)
{
dis_a=1;
dis_b=0;
dis_c=0;
dis_d=1;
dis_e=1;
dis_f=0;
dis_g=0;
}
else if (digg==5)
{
dis_a=0;
dis_b=1;
dis_c=0;
dis_d=0;
dis_e=1;
dis_f=0;
dis_g=0;
}
else if (digg==6)
{
dis_a=0;
dis_b=1;
dis_c=0;
dis_d=0;
dis_e=0;
dis_f=0;
dis_g=0;
}
else if (digg==7)
{
dis_a=0;
dis_b=0;
dis_c=0;
dis_d=1;
dis_e=1;
dis_f=1;
dis_g=1;
}
else if (digg==8)
{
dis_a=0;
dis_b=0;
dis_c=0;
dis_d=0;
dis_e=0;
dis_f=0;
dis_g=0;
}
else if (digg==9)
{
dis_a=0;
dis_b=0;
dis_c=0;
dis_d=0;
dis_e=1;
dis_f=0;
dis_g=0;
}
else if (digg==0x0A) /*For symbol -*/
{
dis_a=1;
dis_b=1;
dis_c=1;
dis_d=1;
dis_e=1;
dis_f=1;
dis_g=0;
}
}
0 comments:
Post a Comment