# Interfacing Two multiplexed seven segment with 8051

#### bobparihar

Joined Jul 31, 2014
93
Having problem in Interfacing Two multiplexed seven segment with 8051
my code is

Rich (BB code):
#include<reg51.h>
sbit a=P3^0;
sbit b=P3^1;
void main()
{
unsigned char ar[]={0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int i,j,k;
while(1)
{
for(i=0;i<100;i++)
{
a=0;
b=1;
P2=ar[i/10];

for(k=0;k<35000;k++);     // delay

a=1;
b=0;
P2=ar[i%10];
for(k=0;k<35000;k++);
}

}
}
can't figure out what is the problem here, counting up to 99 is happening but seven segment is getting on and off... sorry for the bad english

Last edited by a moderator:

#### shteii01

Joined Feb 19, 2010
4,644

#### bobparihar

Joined Jul 31, 2014
93
Having problem in Interfacing Two multiplexed seven segment with 8051
my code is

Rich (BB code):
#include<reg51.h>
sbit a=P3^0;                           // to  on off seven segment
sbit b=P3^1;                                                       //  "  "     "       "       " (same  as above)
void main()
{
unsigned char ar[]={0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int i,j,k;
while(1)
{
for(i=0;i<100;i++)
{
a=0;                                       // on the 7 seg at tenth place
b=1;                                           // off the 7 seg at ones place
P2=ar[i/10];                  // sending 0-9 to port 2 to 7 seg
for(k=0;k<35000;k++);                      // delay

a=1;                                                          // off 7 seg at tenth place
b=0;                                                    // on seven seg at ones place
P2=ar[i%10];
for(k=0;k<35000;k++);           // delay
}
}
}

Last edited by a moderator:

#### ErnieM

Joined Apr 24, 2011
8,334
Hello bobparihar, welcome to the forums!

First off, when you post code could you please use the code tags? It's the # button above the place you type. Code tags format your code as in the first post. It makes it more readable that way.

I read your code, it looks reasonable and I do not see any obvious mistakes.

Can you describe "but seven segment is getting on and off" in some more detail? Both or just one segment? Every number or just as certain points?

Can you see all the numbers 0 1 2 3 4 5 6 7 8 and 9?

#### bobparihar

Joined Jul 31, 2014
93
Hello bobparihar, welcome to the forums!

First off, when you post code could you please use the code tags? It's the # button above the place you type. Code tags format your code as in the first post. It makes it more readable that way.

I read your code, it looks reasonable and I do not see any obvious mistakes.

Can you describe "but seven segment is getting on and off" in some more detail? Both or just one segment? Every number or just as certain points?

Can you see all the numbers 0 1 2 3 4 5 6 7 8 and 9?
Dear ErnieM thanks for your concern to solve my problem
" seven segment is getting on and off" means first the seven segments at the tenth

place is showing 0, and then its goes to off and the seven segments at the ones

place is showing 0 then its goes to off. and so on the numbers from 0 to 99 is showing.

I did achieve the counting but not in the desired manner, the display is not continues

#### bobparihar

Joined Jul 31, 2014
93
" seven segment is getting on and off" means first the seven segments at the tenth

place is showing 0, and then its goes to off and the seven segments at the ones

place is showing 0 then its goes to off. and so on the numbers from 0 to 99 is showing.

I did achieve the counting but not in the desired manner, the display is not continues

#### ErnieM

Joined Apr 24, 2011
8,334
Hi bobparihar, Im glad you bumped your post. This time when I read it I saw what you are talking about. Last time I thought you meant something that I could not see in the code, this time I think I get it.

place is showing 0, and then its goes to off and the seven segments at the ones

Exactly, that is what your code is telling the display to do.
Lets look at you display code:
Rich (BB code):
for(i=0;i<100;i++)
{
a=0;                         // on the 7 seg at tenth place
b=1;                         // off the 7 seg at ones place
P2=ar[i/10];               // sending 0-9 to port 2 to 7 seg
for(k=0;k<35000;k++); // delay

a=1;                         // off 7 seg at tenth place
b=0;                         // on seven seg at ones place
P2=ar[i%10];
for(k=0;k<35000;k++); // delay
}
That says display the lower digit, then wait some. Next display the higher digit, then wait some more. Then increment the number and do it again.

That is just what you see, but not what you want.

To get what you want you want to use the human phenomena of http://en.wikipedia.org/wiki/Persistence_of_vision]Persistence of vision[/url] where if you see something that changes fast you dont see the change.

What you want is to speed up the display code above until both numbers seem to be always on by making the delays much shorter. While you work that out take out the for(i=0;i<100;i++) loop and just assign I to some (any) fixed value. You can just drop in another statement such as i = 16; just after the for statement to do that.

When you are happy with how one number displays wrap the display code with a counter, and run and rerun the display code a number of times. That number is equal to how long you want each digit to display divided by twice the digit delay; twice because you have two delays, one for each digit.

You dont have to be exact with that number as you may want to experiment with different values.
Good luck and let us know how it goes!
sample of code to try:
Rich (BB code):
for(i=0;i<100;i++)
{
i = 16;                   // fixed value for test
a=0;                      // on the 7 seg at tenth place
b=1;                      // off the 7 seg at ones place
P2=ar[i/10];            // sending 0-9 to port 2 to 7 seg
for(k=0;k<200;k++); // much shorter delay

a=1;                      // off 7 seg at tenth place
b=0;                      // on seven seg at ones place
P2=ar[i%10];
for(k=0;k<200;k++); // much shorter delay
}

#### bobparihar

Joined Jul 31, 2014
93
dear erniem if i use for(i=0;i<200;i++); as a shorter delay.. then i would get 0 to 9 at the ones place in just a second..and it will b not visible to me that number is incremanting at the ones place.. though tenth place digit works fine... but there is always 8 at the ones place.. because 0 to 9 is incrementing too fast.

#### ErnieM

Joined Apr 24, 2011
8,334
If you left in the increment that is what you will see if the two delays are still too long.

Remove the increment (or just block it as I did in line 3)

Run the code again. If you do not see two solid numbers of same brightness that do not blink then shorten the delay and run the code again.

You need to get the delay short enough so your displays can multiplex. After you get that complete then worry about incrementing or counting.