PICsters, Here is some code I wrote that allows you to increment a multi-digit decimal counter. Unlike some versions, which use one byte per digit, this one packs two digits per byte. This is useful if you need to conserve registers. This is a snippit of actual code that I have running. I have left out the usual initialization stuff, since the code shown does not assume the use of any external output device. You can, of course, use it with 7 segment LEDs or LCD displays, or simply as an internal decimal counter. Maybe it is something you might want to file away for later, so if you need to conserve register space, you already have some de-bugged code handy. Fr. Tom McGahee ****************** ;MULTI-DIGIT DECADE COUNTER ; by Fr. Tom McGahee ;DEFINE RAM USEAGE and any CONSTANTS SOME_REG EQU H'0F' ;Can be a shared register (we on ly need 1 bit) CONSTANT OVERFLOW=H'01' ; This is the definition for that bit. DDCTR EQU H'10' ;Can be a shared register. Tempo rary counter. ;The following each contain a DO UBLE digit ;Thus all 3 contain 6 digits 0 - 999,999 DIGIT3 EQU H'11' ;contains MSB (6,5) DIGIT2 EQU H'12' ;contains (4,3) DIGIT1 EQU H'13' ;contains LSB (2,1) ;The order has been chosen so that when viewed in MPLAB Simulator ; the counting will appear "normal". ; Note that when viewed in the simulator the change from 9 to 0 and a ; carry will at first be displayed as "A", since the intermediate ; results are stored in the actual digit to conserve register usage. org 0 ;Set code origin start goto main ;We have to get past interrupt v ector at 0004 org 5 ;get past interrupt vector ;Ready now to begin main user program. ;***** MAIN: ;This is a short progarm that exercises the counter. CLRF DIGIT1 ;Clear all digits initially. CLRF DIGIT2 CLRF DIGIT3 ;3 double digits in this example... BCF SOME_REG,OVERFLOW ;Clear Overflow flag initially. MAIN_LOOP: CALL INC_DIGIT_SET BTFSS SOME_REG,OVERFLOW GOTO MAINLOOP ;LOOP UNTIL OVERFLOW LP: NOP ;This NOP helps me to trap ;breakpoints a bit more easily GOTO LP ;ENDLESS LOOP ;***** ;MULTI-DIGIT DECIMAL COUNTER ;***** The code is set up so that it can handle ANY even number of digits INC_DIGIT_SET: BCF SOME_REG,OVERFLOW ;Reset Overflow flag MOVLW H'03' ;Do 3 double decimal digits max (6 total ) ;Change this to reflect the # of double digits desired. ;You are limited only by the amount of available RAM, MOVWF DDCTR ;Keep counter in DDCTR MOVLW DIGIT1 ;Start with DIGIT1 (D3-D2-D1) MOVWF FSR ;Use Indirect Addressing! INC_RDIGIT: ;This part increments the Right side... INCF INDF,W ;So we increment Indirect & place in W MOVWF INDF ;Save updated RDIGIT in case not 10. ANDLW H'0F' ;Mask & get RDIGIT *only* SUBLW H'0A' ;Subtract 10 from it BTFSS STATUS,Z ;If it resulted in 0 we need to do more. .. RETURN ; otherwise we is all done. INC_LDIGIT: ;This part handles the Carry problem... MOVF INDF,W ;First recover the L and R DIGIT parts ANDLW H'F0' ;Keep the L part and reset R part to zer o ADDLW H'10' ;Increment just the L DIGIT part MOVWF INDF ;Save what we got so far SUBLW H'A0' ;Subtract 10 from the L DIGIT part BTFSS STATUS,Z ;If it resulted in 0 we need to do more. .. RETURN ; otherwise we is all done INC_NEXT_RDIGIT: ;This gets ready to Carry on! MOVF INDF,W ;First recover both digit parts ANDLW H'0F' ;Set L DIGIT part to 0 MOVWF INDF ;Save present L R DIGIT pieces DECF FSR,F ;Point (Indirect) to NEXT DIGIT! ;* see note below DECF DDCTR,F ;Decrement DDCTR BTFSS STATUS,Z ;Check to see if it went to zero GOTO INC_RDIGIT ;If it did NOT, then keep carrying on! BSF SOME_REG,OVERFLOW ; otherwise report the OVERFLOW RETURN ; and we is (finally) all done. *********** End of program sample * Note: Change the DECF FSR,F instruction to INCF FSR,F if you order the digits as follows in RAM: DIGIT1 EQU H'11' ;contains MSB (2,1) DIGIT2 EQU H'12' ;contains (4,3) DIGIT3 EQU H'13' ;contains MSB (6,5) Instead of the ordering used in the above example, which is: DIGIT3 EQU H'11' ;contains MSB (6,5) DIGIT2 EQU H'12' ;contains (4,3) DIGIT1 EQU H'13' ;contains LSB (2,1) The ordering used in the sample program was chosen so that in the Simulator you could easily watch the digits counting up in the way you would expect to "see" them count.