8 Bit Up Down Counter
This project shows the application of PIC microcontroller as an up/down counter, designed to count from 0 to FF and vice versa. 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 from Simulation menu.
This circuit uses a PIC Microcontroller, Binary to Seven Segment Decoder/Driver, Seven Segment LED Display. In this circuit, Port A of PIC Microcontroller is assigned as the Input Port while Port B is assigned as Output Port. Port A is used for selecting whether the counter works as Up Counter or as Down Counter. When the LSB of Port A is high then the circuit works as an Up Counter else as Down Counter. Binary to Seven Segment Decoder decodes the data from Port B to 7-segment format, which are displayed in 7-segment displays. The count sequence appears at the LED display in 7-segment form.
The source code written in Assembly language can be viewed from the
code editor window
The program is as shown:
include "p16c5x.inc"
startup code
org 0
start nop
org 10
clrf FSR ; clearing FSR register
clrf INDF ; clearing INDF register
clrw ; clearing W register
comf INDF,0 ; complimenting INDF register
tris PORTA ; setting PORTA in input mode
btfss PORTA,0 ; 0 for Down counting & 1 for Up counting
false call d_cnt ; call D_CNT subroutine
true call up_cnt ; call UP_CNT subroutine
; Up counting starts here
up_cnt
clrf fsr ; clearing FSR register
clrf indf ; clearing INDF register
clrw ; clearing W register
tris portb ; setting PORTB in output mode
movlw 0x00 ; moving literal constant 0x00 to W
movwf 08h ; moving the content of W to 08H
rep1
movwf portb ; moving the content of W through PORTB
incf 08h,1 ; incrementing the content of 08H
movf 08H,0 ; moving the content of 08H to W
goto rep1 ; repeating the loop REP1
; Down counting starts here
d_cnt clrf fsr ; clearing FSR register
clrf indf ; clearing INDF register
clrw ; clearing W register
tris PORTB ; setting PORTB in output mode
movlw 0xff ; setting the start value
movwf 08H ; moving the content of W to 08H
rep2
movwf PORTB ; moving the content of W through PORTB
decf 08H,1 ; decrementing the value of 08H
movf 08H,0 ; moving the content of 08H to W
goto REP2 ; repeating the loop REP2
retlw start
end ; directive 'end of program'
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.