Searching \ for 'BCD counter' in subject line. ()
Help us get a faster server
FAQ page: techref.massmind.org/techref/timers.htm?key=count
Search entire site for: 'BCD counter'.

Truncated match.
'BCD counter'
1998\04\14@043914 by

Dear all,

Here's some code I wrote for a 4 digit BCD counter, it was for a 16c57, it
works, but can anyone suggest how I could improve/compact it?

The count is held in Lendecim1 and Lendecim2, thus:

Lendecim2       Lendecim1
MSD | NMSD     NLSD | LSD

leninc    bsf       FSR,RP0
movlw     01
btfsc     STATUS,DC
movlw     06
btfsc     STATUS,C
incf      Lendecim2,F
btfsc     STATUS,DC
goto      lover1
subwf     Lendecim1,F
goto      Finish

lover1    movlw     60
btfsc     STATUS,C
subwf     Lendecim1,F
goto      Finish
movlw     06
btfsc     STATUS,DC
goto      lover3
subwf     Lendecim2,F
goto      Finish
lover3    movlw     60
btfss     STATUS,C
subwf     Lendecim2,F

Finish

I used a BCD counter as it seemed easier to convert for 7 seg displays.
Has anyone tried it the other way? (ie binary counter with 7 seg
conversion)

Paul Britton

Paul BRITTON wrote:
>
> Dear all,
>
> Here's some code I wrote for a 4 digit BCD counter, it was for a 16c57, it
> works, but can anyone suggest how I could improve/compact it?
>
> The count is held in Lendecim1 and Lendecim2, thus:

<snip>

Different, but half as big:

increment_bcd:

movlw  0x66+1       ;6's for generating a DC if LSD is 9
;or C if MSD is 9, and 1 because we
skpnz
goto  increment_high ;must of had 0x99 for bcd_lo

skpdc               ;Was the LSD < 9?
xorlw 6            ; yeah, remove 6 from it
xorlw  0x07         ;subtract 6 from MSN

subwf  bcd_lo,F     ;remove the upper 6 (and maybe lower 6)

goto   done

increment_high:

skpnz
goto  done         ;must of had 0x9999 for the input.

skpdc               ;Was the LSD < 9?
xorlw 6            ; yeah, remove 6 from it
xorlw  0x07         ;subtract 6 from MSN

subwf  bcd_hi,F

done:

;Z will be set if a roll over occurred

Scott

Scott D wrote:
>
>Different, but half as big:
>
snip

;written by Dwayne Reid
;2 digit BCD UP counters: units in lower nibble, tens in upper
;can increment by 1...9 without error, intermediate results kept in w
incf        CNTR1,W         ;inc counter --> w
addlw       6               ;now 10?  if so, wrap to 0, inc nxt digit
skpdc                       ;did wrap occur?
addlw       60              ;did upper digit wrap?
skpc
movwf       CNTR1           ;new count  NOTE: z=1 if wrapped to 00
return

;written by Dwayne Reid
;2 digit DOWN counter: units in lower nibble, tens in upper
;can decrement by 1...9 without error, intermediate results kept in w
movfw       CNTR2
addlw       -1              ;dec counter --> w, update flags
skpc                        ;tens underflow?
skpdc                       ;units undeflow?
movwf       CNTR2           ;new count; z=1 if decremented to 00
;need to test for underflow (to 99) if cascading digit pairs
return

I have used both pieces in several projects over the past few years.

dwayne

Dwayne Reid   <dwaynerplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, Alberta, CANADA
(403) 489-3199 voice     (403) 487-6397 fax

Try mine (untested):
Note: Can also be used with 5X devices and 1 cycle less.

INCR:
MOVLW   67
MOVLW   -60
SKPDC
MOVLW  -66
SKPC
RETURN

;To cascade test carry or zero flag for overflow

DECR:
MOVLW   0xFF
MOVLW   -6
SKPC
MOVLW  -66
SKPDC
RETURN

;DC and C are set if there's underflow

Reggie

---------
{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...