Searching \ for 'BCD counter' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/timers.htm?key=count
Search entire site for: 'BCD counter'.

Truncated match.
PICList Thread
'BCD counter'
1998\04\14@043914 by Paul BRITTON

flavicon
face
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
         addwf     Lendecim1,F
         btfsc     STATUS,DC
         goto      ladj1
         movlw     06
         addwf     Lendecim1,F
         btfsc     STATUS,C
         incf      Lendecim2,F
         btfsc     STATUS,DC
         goto      lover1
         subwf     Lendecim1,F
         goto      Finish
ladj1     movlw     06
         addwf     Lendecim1,F

lover1    movlw     60
         addwf     Lendecim1,F
         btfsc     STATUS,C
         goto      ladj2
         subwf     Lendecim1,F
         goto      Finish
ladj2     incf      Lendecim2,F
         movlw     06
         addwf     Lendecim2,F
         btfsc     STATUS,DC
         goto      lover3
         subwf     Lendecim2,F
         goto      Finish
lover3    movlw     60
         addwf     Lendecim2,F
         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

1998\04\14@161958 by Scott Dattalo

face
flavicon
face
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
 addwf  bcd_lo,F     ;are incrementing.
 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)
                     ;that was added above

 goto   done

increment_high:

 addwf  bcd_hi,F
 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

1998\04\14@225807 by Dwayne Reid

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

How about this . . .

;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     -6              ;no, restore
   addlw       60              ;did upper digit wrap?
   skpc
     addlw     -60             ;no, restore
   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?
     addlw     -60             ;adjust
   skpdc                       ;units undeflow?
     addlw     -6              ;adjust
   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   <spam_OUTdwaynerTakeThisOuTspamplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, Alberta, CANADA
(403) 489-3199 voice     (403) 487-6397 fax

1998\04\15@052439 by Regulus Berdin

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

INCR:
       MOVLW   67
       ADDWF   CNT,F
       MOVLW   -60
       SKPDC
        MOVLW  -66
       SKPC
        ADDWF  CNT,F
       RETURN

;To cascade test carry or zero flag for overflow

DECR:
       MOVLW   0xFF
       ADDWF   CNT,F
       MOVLW   -6
       SKPC
        MOVLW  -66
       SKPDC
        ADDWF  CNT,F
       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...