8051_Up-Down Counter
This project shows an Up-Down counter using 8051. The counter is designed to count upto ‘FF’. To simulate circuit in this project, initially activate Mixed Mode simulator from the Schematic Editor window. Simulation can be performed by selecting Run Transient analysis (Oscillograph) from Simulation menu.
The circuit contains an 8051 chip,
Interrupt Pulse Generator
, Binary to 7 Segment Decoder and Seven Segment LED Display. The Interrupt Pulse Generator is connected to the INT0 pin of 8051. In this circuit Port P2 is set as the output port. The counter is internally set through the code to count in the ascending mode. But when an interrupt pulse is received at the INT0 pin, the counter counts in the descending mode upto zero and continues with the ascending mode. The count sequence appears at the LED display in
7-segment form.
The source code written either in C or Assembly language can be viewed from the
code editor window
The program is as shown:
main:
setb EA ; EA is set to enable all interrupts
setb EX0 ; EX0 is set to enable External Interrupt 0
mov a, #0x00 ; Accumulator is loaded with value ‘00’
mov r0,#0xFF ; An internal counter is set to count upto ‘FF’
mov TMOD,#0x00 ; 13-bit timer mode is set.
mov TH0, #0xff ; To get the required time delay, set T0 as ‘FFDF’
mov TL0, #0xdf
loop2:
mov p2,a ; Value in Accumulator is copied to Port P2
clr p3.0 ; P3.0 is cleared to enable decoder
setb TR0 ; TR0 is set to value ‘1’ to start the Timer 0
delay:
jnb TF0, delay ; Checking whether TF0 is set to value ‘1’
inc a ; Incrementing the content of Accumulator
djnz r0,loop2 ; Check whether r0 is zero. If not, jump to loop2
clr TR0 ; Set value of TR0 to’0’ to stop the Timer 0
clr TF0 ; Set value of TF0 to ‘0’
reti ; Return function
; function int_t0
_int_t0:
push acc ; Pushing the contents of Accumulator, register B, Data Pointer and Program Status Word to Stack
push b
push dpl
push dph
push psw
mov psw,#0x00 ; The value ‘00’ is copied to PSW.
mov TH1, #0xff ; To get the required time delay, set T1 as ‘FFDF’
mov TL1, #0xdf
loop3:
mov p2,a ; The content of Accumulator is copied to Port P2
clr p3.0 ; P3.0 is cleared to enable decoder
del:
setb TR1 ; TR1 is set to value ‘1’ to start Timer 1
delay2:
jnb TF1,delay2 ; Checking whether TF1 is set to value ‘1’
clr TR1 ; Set value of TR1 to’0’ to stop Timer 1
clr TF1 ; Set value of TF1 to’0’
dec a ; Decrement Accumulator content
jnz loop3 ; Check whether Accumulator content is zero. If not, jump to loop3
pop psw ; Popping the contents of Accumulator, register B, Data Pointer and Program Status Word from Stack
pop dph
pop dpl
pop b
pop acc
sjmp main ; Jump to main
The source code in the
code editor window
has to be
compiled
after making any modifications
(editing).
Also the code can be
debugged
during
simulation.