UP DOWN Counter PIC16F84A

Thread Starter

Jerry Jacob Morala

Joined May 22, 2017
3
Hello, I just want to ask how to set the limit of this up down counter to something I desired(e.g 0-150 instead of 0-999)
I do not own the code but simply using it to understand how it works for future works. I am using Proteus to simulate and test the program
Program Code:
Code:
Start:
LSB EQU H'0021'
MSB EQU H'0022'
NUM1 EQU H'0023'
NUM2 EQU H'0024'
NUM3 EQU H'0025'
NUM4 EQU H'0026'
NUM5 EQU H'0027'
VALUE EQU H'0028'

ONE EQU H'0029'  ; Counter value - 3 digits unpacked BCD
TENS EQU H'002A'
HUNDRED EQU H'002B'

FIGURE EQU H'002D'
FIGURE1 EQU H'002E'
TEMP EQU H'002F'

;--------------------------  PROGRAM STARTS  -------------------
LED:
CLRF MSB
CLRF LSB
BSF STATUS,5
MOVLW B'11110000'
MOVWF TRISA
MOVLW B'10000000' 
MOVWF TRISB
BCF STATUS,5
CLRF PORTB
CLRF PORTA
CLRF ONE
CLRF TENS
CLRF HUNDRED
CLRF NUM1
CLRF NUM2
CLRF NUM1
CLRF NUM4 
GOTO MAIN

;--------------- INCREMENT COUNTER  -------
ADD:
INCF ONE,F
MOVLW .10
SUBWF ONE,W
BTFSS STATUS,Z
GOTO LCDB

CLRF ONE
INCF TENS,F
MOVLW .10
SUBWF TENS,W
BTFSS STATUS,Z
GOTO LCDB

CLRF TENS
INCF HUNDRED,F
MOVLW .10
SUBWF HUNDRED,W
BTFSS STATUS,Z
GOTO LCDB
CLRF HUNDRED

LCDB:  ; Display incremented result
CALL SCREEN
BTFSS PORTB,7
GOTO LCDB
GOTO MAIN

;--------------- DECREMENT COUNTER  ---------
COVER:
MOVLW .1
SUBWF ONE,F
BTFSC STATUS,C
GOTO LCDA
CLRF ONE
MOVLW .1
SUBWF TENS,F
BTFSC STATUS,C
GOTO ONE9

CLRF TENS
MOVLW .1
SUBWF HUNDRED,F
BTFSC STATUS,C
GOTO TENS9

CLRF HUNDRED
MOVLW .1

HUNDRED9:
MOVLW .9
MOVWF HUNDRED

TENS9:
MOVLW .9
MOVWF TENS

ONE9:
MOVLW .9
MOVWF ONE

LCDA:  ; Display decremented result
CALL SCREEN
BTFSS PORTA,4
GOTO LCDA
GOTO MAIN

;--------------- MAIN LOOP  ---------------
MAIN:
CALL SCREEN
BTFSS PORTA,4
GOTO COVER
BTFSS PORTB,7
GOTO ADD
GOTO MAIN

;------------------  DISPLAY COUNTER VALUE  ------------------
SCREEN:
MOVLW .5 ;refresh all four
MOVWF FIGURE ;digits five times
CLRF PORTB ;active high segments off
MOVLW .255 ;selects active low digit
MOVWF PORTA ;lines off

SHOW:
BCF PORTA,0 ;select 4th digit(RA0)
BSF PORTA,1 ;''
BSF PORTA,2 ;''
BSF PORTA,3 ;''

;---------------------------
;FOR ONES DIGIT
MOVF ONE,W ;ONES, 0...9
CALL TABLE ;get segment data
MOVWF PORTB ;display new digit
CALL DELAY ;delay 850-uS
CALL DELAY ;delay 850-uS

CLRF PORTB ;Blank or clear the display
BSF PORTA,0 ;select the 3rd digit (RA1)
BCF PORTA,1 ; ''
BSF PORTA,2 ; ''
BSF PORTA,3 ; ''
;---------------------
;FOR TENS DIGIT
MOVF TENS,W ;TENS 0...9
CALL TABLE ;get segment data
MOVWF PORTB ;display new digit
CALL DELAY ;delay 850-uS

CLRF PORTB ;clear the display
BSF PORTA,0 ;select the second digit (RA2)
BSF PORTA,1 ; ''
BCF PORTA,2 ; ''
BSF PORTA,3 ; ''

;-------------------------
;THIS IS FOR THE HUNDREDS DIGIT
MOVF HUNDRED,W ;HUNDREDS 0...9
CALL TABLE ;get segment data
MOVWF PORTB ;display new digit
CALL DELAY ;delay 850-uS

CLRF PORTB ;clear the display
BSF PORTA,0 ;select the second digit (RA3)
BSF PORTA,1 ; ''
BSF PORTA,2 ; ''
BCF PORTA,3 ; ''
RETURN

;-----------------------
;This is our delay function with a fix time of 850-uS(microseconds)
DELAY
MOVLW .5
MOVWF MSB
D11 
MOVLW .55
MOVWF LSB
D22
DECFSZ LSB,F
GOTO D22
DECFSZ MSB,F
GOTO D11
RETURN

;-------------------
TABLE
ADDWF PCL,F
RETLW h'3F'
RETLW h'06'
RETLW h'5B'
RETLW h'4F'
RETLW h'66'
RETLW h'6D'
RETLW h'7D'
RETLW h'07'
RETLW h'7F'
RETLW h'6F'
RETLW h'77'
RETLW h'7C'
RETLW h'39'
RETLW h'5E'
RETLW h'79'
RETLW h'71'
RETLW h'80'

;-------------------------------------------------------------------
END
Mod edit: code tags
 

Attachments

Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Welcome to AAC!
I cleaned up your source a bit and used the CODE tags function (from the toolbar in the edit window) to make it more understandable.

From the code you can see that your counter value is maintained as 3 unpacked BCD digits in 3 RAM locations. To put limits on the count you can inspect the value of the counter digits in the ADD and COVER routines then act accordingly.

For example, ADD increments the counter. It rolls over normally 999->000. To stop it at 150, inspect the three digits for 0x01, 0x05, 0x00 when you enter the ADD routine each time. If all are equal to those respective digits, the counter is at its limit so instead of executing the normal code, clear the 3 digits to 000. Decrement works in a similar way except you test for 000 going in and if so, load the counter to 150 instead of decrementing.

There are a few other things to consider in something like this but those are the basics.

Good luck!
 

Thread Starter

Jerry Jacob Morala

Joined May 22, 2017
3
Welcome to AAC!
I cleaned up your source a bit and used the CODE tags function (from the toolbar in the edit window) to make it more understandable.

From the code you can see that your counter value is maintained as 3 unpacked BCD digits in 3 RAM locations. To put limits on the count you can inspect the value of the counter digits in the ADD and COVER routines then act accordingly.

For example, ADD increments the counter. It rolls over normally 999->000. To stop it at 150, inspect the three digits for 0x01, 0x05, 0x00 when you enter the ADD routine each time. If all are equal to those respective digits, the counter is at its limit so instead of executing the normal code, clear the 3 digits to 000. Decrement works in a similar way except you test for 000 going in and if so, load the counter to 150 instead of decrementing.

There are a few other things to consider in something like this but those are the basics.

Good luck!
Hello John,
Thanks for helping me out, I'll try to do this in our school since I don't have proteus to simulate the program at home.
 

Thread Starter

Jerry Jacob Morala

Joined May 22, 2017
3
Welcome to AAC!
I cleaned up your source a bit and used the CODE tags function (from the toolbar in the edit window) to make it more understandable.

From the code you can see that your counter value is maintained as 3 unpacked BCD digits in 3 RAM locations. To put limits on the count you can inspect the value of the counter digits in the ADD and COVER routines then act accordingly.

For example, ADD increments the counter. It rolls over normally 999->000. To stop it at 150, inspect the three digits for 0x01, 0x05, 0x00 when you enter the ADD routine each time. If all are equal to those respective digits, the counter is at its limit so instead of executing the normal code, clear the 3 digits to 000. Decrement works in a similar way except you test for 000 going in and if so, load the counter to 150 instead of decrementing.

There are a few other things to consider in something like this but those are the basics.

Good luck!
Hello John, I seem to be having a hard time trying to figure what you told me. Sorry for the trouble...
 

JohnInTX

Joined Jun 26, 2012
4,787
What have you tried?
Start in the ADD routine and test HUNDRED for 01h, TENS for 05h and ONE for 00h. If ALL match, the counter is at 150 so instead of falling through to the increment code, GOTO new code that clears those 3 bytes.
Code:
ADD:
 movlw 01h
 subwf HUNDRED,W
 btfss STATUS,Z
 goto IncrementCounter ; HUNDRED is not 1, just increment normally

; HUNDRED == 1, continue testing for next digits
;  then test TENS and ONE.. if all match, code will be here
 clrf HUNDRED ; count is 150, clear counter
 clrf TENS
 clrf ONE
 goto LCDB

IncrementCounter:
; normal increment code here..

LCDB:
; display
 
Top