Multiplexing is very essential part while working with 8051 because of its limited number of ports. which offer 32 pins for connecting external devices. 32 seems large but when you working with seven segment, keypad, LCD, ADC etc. or if you project have many parts to operate simultaneously then you definitely need to multiplex some of ports.
But 7 is displayed on all four 7-segment. To avoid this we need to turn off all three seven segment and giving power to only first display. Then 7 is displayed only on first seven segment. now if I turn on second display and turn off all other three and send 6 to it then it is displayed only to second. By doing same with other two and repeating this at high speed it seems that they all are glowing simultaneously like a counter.
here i giving code in assembly. this code is written by me and i try to make it small as possible while writing this before some days. so it become little bit complex. but don't worry i will explain every thing...
this is a four digit counter so i need four register to hold count, each for one digit. I use R0,R1,R2 and R3 for digit 0,1,2,3;
eg. to show [ 8721 ]={ [digit_3=8],[digit_2=7],[digit_1=2],[digit_0=1]}
step 1: display each digit (value of registers) by multiplexing seven segment.
step 2:check digit_0 is greater than 10 or not
step 3: if not then i increment it (R0) by one else follow step 4
step 4: if greater than 10 then increment digit_1 (R1) and reset R0 to zero and again start form step_1
do the same job with all other digits.
now lets start the coding..
Here i use data pointer to display each number form 0 to 9. for this i define each no in hex form which is stored at memory location 200h see img. it is similar to array in C or other languages
this is code which send number on port to display on seven segment
i hope you know how DPTR work if not then understand it by this example
Let you want to display 5 on seven segment. for this i put 200h in DPTR and 5 in register A then after executing
movc a,@a+dptr. It put the value stored at memory location 205h (@a+dptr=5+200) in register A which is 12H(see in image).
and mov PORT,A send this to port where seven segment connected. then A is cleared for next execution. so you just put number in A this routine display it to 7-seg.
so i call this routine each time when i need to display register value
< I do same for all digits. and the rest of program is very simple. this is full code.
This is our full work ..
Get fully working circuit:
hope you understand this code and like it.
please share and give +1 if you like this tutorial. leave comment if facing any problem regrading this tutorial or assembly.
thank you for visiting......
Circuit Image
what due mean by multiplexing in 8051:
It is very simple to know that microcontroller can do one task at a time. suppose you connect four seven segment display at same port, (for example at port 1). And you display number 7 on first seven segment.But 7 is displayed on all four 7-segment. To avoid this we need to turn off all three seven segment and giving power to only first display. Then 7 is displayed only on first seven segment. now if I turn on second display and turn off all other three and send 6 to it then it is displayed only to second. By doing same with other two and repeating this at high speed it seems that they all are glowing simultaneously like a counter.
look at this video in which i give long delay to see what happen exactly
So in multiplexing we send data to only one device and keep all other disable (turn off).
here i giving code in assembly. this code is written by me and i try to make it small as possible while writing this before some days. so it become little bit complex. but don't worry i will explain every thing...
How counter work:
Every one know how a counter work but here we are working with 8051 in assembly So it is better to understand concept behind it before written any code.this is a four digit counter so i need four register to hold count, each for one digit. I use R0,R1,R2 and R3 for digit 0,1,2,3;
eg. to show [ 8721 ]={ [digit_3=8],[digit_2=7],[digit_1=2],[digit_0=1]}
step 1: display each digit (value of registers) by multiplexing seven segment.
step 2:check digit_0 is greater than 10 or not
step 3: if not then i increment it (R0) by one else follow step 4
step 4: if greater than 10 then increment digit_1 (R1) and reset R0 to zero and again start form step_1
do the same job with all other digits.
now lets start the coding..
Display Function:
first i need a subroutine (function) to display digits (value of registers)Here i use data pointer to display each number form 0 to 9. for this i define each no in hex form which is stored at memory location 200h see img. it is similar to array in C or other languages
this is code which send number on port to display on seven segment
send_num: MOVC A,@A+DPTR MOV PORT,A CLR A ACALL delay RET
i hope you know how DPTR work if not then understand it by this example
Let you want to display 5 on seven segment. for this i put 200h in DPTR and 5 in register A then after executing
movc a,@a+dptr. It put the value stored at memory location 205h (@a+dptr=5+200) in register A which is 12H(see in image).
and mov PORT,A send this to port where seven segment connected. then A is cleared for next execution. so you just put number in A this routine display it to 7-seg.
so i call this routine each time when i need to display register value
Display a Digit:
Now display a digit (value of registers ) become very easy i just need to put register in reg. A and then call send_num.mov p2,#00h ;turn off all 7-segment SETB S1 ;turn on firts 7-segment MOV A,R0 ACALL send_num
< I do same for all digits. and the rest of program is very simple. this is full code.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MULTIPLEXING 4 SEVEN SEGMENT +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $mod51 S1 EQU P2.0 S2 EQU P2.1 S3 EQU P2.2 S4 EQU P2.3 port EQU P1 MAIN: MOV DPTR,#num MOV R3,#00H digit_3: MOV R2,#00H digit_2: MOV R1,#00H digit_1: MOV R0,#00H digit_0: MOV P2,#00H SETB S1 ;send first digit MOV A,R0 ACALL send_num MOV P2,#00H ;send second digit SETB S2 MOV A,R1 ACALL send_num MOV P2,#00H ;send third digit SETB S3 MOV A,R2 ACALL send_num MOV P2,#00H ;send forth digit SETB S4 MOV A,R3 ACALL send_num INC R0 CJNE R0 ,#0aH,digit_0 ;check if less then ;10 or not INC R1 CJNE R1 ,#0AH,digit_1 INC R2 CJNE R2 ,#0AH,digit_2 INC R3 CJNE R3,#0AH, digit_3 LJMP MAIN ;start again send_num: MOVC A,@A+DPTR MOV PORT,A CLR A ACALL delay RET delay: mov r5,#020h loop: mov r4,#0ffh djnz r4,$ djnz r5,loop ret ORG 200H num: DB 0C0h, 0f9h,24h,0b0h,99h,12h,02h,0f8h,00h,10h END
This is our full work ..
Get fully working circuit:
hope you understand this code and like it.
please share and give +1 if you like this tutorial. leave comment if facing any problem regrading this tutorial or assembly.
thank you for visiting......
Circuit Image
i post a comment to the youtube link. i'm posting my question here too:
ReplyDeletei read your code but i did not understand how you defined 1 second to the 8051. what kind of quartz did you use to implement this circuit?
thank you :)
what about transistors' connection
ReplyDelete@ali efe: in this example i use 12MHz crystal but i did not use 1 second delay. it is just a example to understand how to multiplex.
ReplyDeleteif you are trying to make a clock and 1 sec delay than i think you need to use timer and interrupt for accuracy.
@Ilker: i add circuit,hope it works fine for you
ReplyDeleteI can't download the circuit. I have liked your page but i am still unable to download the file. Please HELP ME...
ReplyDelete@zain: link added to circuit
ReplyDeletehow can i work a jaw with the help of microcontroller ??...for pic n place ...
ReplyDeleteMan,can you post the circuit again ? Tks
ReplyDelete