Searching \ for 'clock code challenge:' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/timers.htm?key=clock
Search entire site for: 'clock code challenge:'.

Truncated match.
PICList Thread
'clock code challenge:'
1997\09\10@191852 by Shane Nelson

flavicon
face
Hi,

After having seen the amazing results from other code challenges
I thought I would try putting one out there myself.

The goal is to have a routine that increments and keeps track of
time in a BCD format.

Points are scored as follows:
       1 point for each ROM location used.
       3 for each RAM.

My code scores 43 points. Best score is the lowest.
Haven't tested it yet so I dont know if it works or not.


ICKIncClock
       incfsz  ICKseconds,f            ;do seconds
       goto    ICKDone
       movlw   d'195'
       movwf   ICKseconds

       incf    ICKmin,f                ;do minutes
       movlw   b'00001111'
       andwf   ICKmin,w                ;mask upper nibble.
       xorlw   d'10'                   ;minLSD at 10?
       SKPZ
       goto    ICKDone
       movlw   b'11110000'
       andwf   ICKmin,f                ;set lower nibble to
zero.

       movlw   b'00010000'
       addwf   ICKmin,f                ;inc upper nibble
       movlw   d'60'
       xorwf   ICKmin,w                ;ran out of minutes?
       SKPZ
       goto    ICKDone
       clrf    ICKmin

       incf    ICKhours                ;now do hours
       movlw   b'00001111'
       andwf   ICKhours,w              ;mask upper nibble
       xorlw   d'10'
       SKPZ
       goto    ICKDone
       movlw   b'11110000'
       andwf   ICKhours,f              ;clear LSD
       movlw   b'00010000'
       addwf   ICKhours,f              ;inc upper nibble
       movlw   d'24'
       xorwf   ICKhours,w              ;at the last hour?
       SKPNZ
       clrf    ICKhours

ICKDone
       retlw   0

1997\09\11@002049 by TONY NIXON 54964

flavicon
picon face
This code uses 3 RAM, 23 ROM, and FSR

        list p=16c84

indirect  equ 0h
status    equ 3h
fsr       equ 4h
carry     equ 0h
z         equ 2h
secs      equ 0eh
mins      equ 0fh
hours     equ 10h
         ;
         ;
         ; ------
         org 0h              ; program start
         ; ------
         ;
start     clrf secs
         clrf mins
         clrf hours
loop      call inc_time
         goto loop

inc_time  movlw secs          ; set indirect address for time regs
         movwf fsr
tm_loop   incf indirect       ; add 1 to addressed time reg
         movlw b'00001111'   ; test if low nib > 9
         andwf indirect,w
         xorlw b'00001010'
         btfsc status,z
         goto cl_nibs        ; yes

         btfss fsr,4         ; test if hours addressed
         return              ; no, exit

         movlw 0x24          ; yes, prepare for > 24 test
         goto test_24

cl_nibs   movlw b'00000110'   ; clear low nib and inc hi nib
         addwf indirect
         movlw 0x60          ; prepare for secs, mins > 60 test
test_24   xorwf indirect,w
         btfss status,z
         return              ; all (>) tests fail, exit

         clrf indirect       ; reset hour or min or sec
         btfsc fsr,4         ; fsr bit 4 = 1 if hours addressed
         return              ; yes, exit

         incf fsr            ; increment pointer for mins or hours
         goto tm_loop        ; check data again

         end


Regards

Tony


Just when I thought I knew it all,
I learned that I didn't.

1997\09\11@030038 by fastfwd

face
flavicon
face
Shane Nelson <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

> The goal is to have a routine that increments and keeps track of
> time in a BCD format.
>
> Points are scored as follows:
>         1 point for each ROM location used.
>         3 for each RAM.
>
> My code scores 43 points. Best score is the lowest.
> Haven't tested it yet so I dont know if it works or not.

Shane:

Your code doesn't work; neither does Tony Nixon's.

This code DOES work; it increments the time, stored in packed-BCD
format (two decimal digits per byte) in registers HOURS, MINUTES, and
SECONDS.

It uses those three registers plus the FSR, and 24 words of ROM
(including the RETURN).  It'll run on any of the PICs, including the
12-bit parts.

SECONDS, MINUTES, and HOURS must be stored in consecutive registers,
in that order... But they can be on any register-page.

By my count, this routine scores 4 * 3 + 24 = 36 points.  I expect,
of course, that John, Scott or Dmitri will be able to do better.

TICK:   MOVLW   SECONDS
       MOVWF   FSR
       CALL    SUB1
       CALL    SUB0
       CALL    SUB0
       ADDWF   INDF,W
       SKPNC
       CLRF    INDF
       RETURN

SUB0:   INCF    FSR
       SKPNC
SUB1:   INCF    INDF

       MOVLW   6
       ADDWF   INDF,W
       XORWF   INDF,W
       ANDLW   11110000B
       MOVLW   6
       SKPNZ
       ADDWF   INDF

       MOVLW   0100H - 060H
       ADDWF   INDF,W
       SKPNC
       CLRF    INDF

       RETLW   0100H - 024H

-Andy

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

=== For PICLIST help (including "unsubscribe" instructions),
=== put the single word "help" in the body of a message and
=== send it to: listservspamKILLspammitvma.mit.edu

1997\09\11@205731 by sdattalo

face
flavicon
face
Andrew Warren wrote:  (and then I modified)
>
> TICK:   MOVLW   SECONDS
>         MOVWF   FSR
>         CALL    SUB1
>         CALL    SUB0
>         CALL    SUB0
>         ADDWF   INDF,W
         goto    SUB2

SUB0      INCF    FSR,F
         SKPNC
SUB1       INCF   INDF,F
         MOVLW   6
         ADDWF   INDF,W
         SKPDC
          RETLW  0100H - 24H

         MOVWF   INDF
         ADDLW   0xa0
SUB2      SKPNC
           MOVWF   INDF
         RETLW   0



19 + 3*4 = 31 points
With the added benefit that upon returning from 'TICK', the
carry flage indicates if 24 hours has rolled over.


The only requirement is that the RAM must be initialized before the
first call.

I think this works, but my brain's starting to hurt. Maybe after
you get some sleep maybe you can check it out Andy. I think I
checked all the possible combinations, but there are surprisingly
quite a few.

Scott
--
                                __o
 I buy pizza instead of gas.   `\<
                             (*)/(*)

1997\09\11@232636 by fastfwd

face
flavicon
face
Scott Dattalo <.....sdattaloKILLspamspam.....unix.SRI.COM> wrote:

> Andrew Warren wrote:  (and then I modified)
>
> [subroutine snipped]
>
> 19 + 3*4 = 31 points
> With the added benefit that upon returning from 'TICK', the
> carry flage indicates if 24 hours has rolled over.
> ....
> I think this works, but my brain's starting to hurt. Maybe after you
> get some sleep maybe you can check it out Andy. I think I checked
> all the possible combinations, but there are surprisingly quite a
> few.

Scott:

Your version works fine... But using your ideas [duh... how come _I_
never remember the DC flag?], the code can be made shorter still:

TICK:   MOVLW   SECONDS
       MOVWF   FSR
       CALL    SUB1
       CALL    SUB0
       CALL    SUB0
       GOTO    SUB2

SUB0:   INCF    FSR
       SKPNC
SUB1:   INCF    INDF

       MOVLW   6
       ADDWF   INDF
       SKPDC
       SUBWF   INDF

       MOVLW   0100H - 060H
SUB2:   ADDWF   INDF,W
       SKPNC
       CLRF    INDF

       RETLW   0100H - 024H

As with your version, the carry flag is set if the "hours" register
has rolled over.

18 instructions, so the score for this one is 4*3 + 18 = 30.

-Andy

=== Andrew Warren - EraseMEfastfwdspam_OUTspamTakeThisOuTix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

=== For PICLIST help (including "unsubscribe" instructions),
=== put the single word "help" in the body of a message and
=== send it to: listservspamspam_OUTmitvma.mit.edu

1997\09\12@072803 by Pedro J Barrios

picon face
Hi:

Would you please explain me what is being done in these instructions:

{Quote hidden}

Thanks a lot.

Pedro

1997\09\12@084419 by Tom Handley

picon face
At 07:24 AM 9/12/97 -0400, Pedro J Barrios wrote:
>Hi:
>
>Would you please explain me what is being done in these instructions:
>
>>
>>
>>         MOVLW   0100H - 060H    <--------------------- ????
>> SUB2:   ADDWF   INDF,W
>>         SKPNC
>>         CLRF    INDF
>>
>>         RETLW   0100H - 024H    <- ------------------- ????

  Pedro, those are assembler expressions. So "0100H - 060H" assembles
as "0A0H". The following are the same:

     MOVLW   0100H - 060H
     MOVLW   0A0H

  - Tom

1997\09\12@160942 by Andres Djordjalian

flavicon
face
> From:          Shane Nelson <@spam@iKILLspamspamCHEETAH.SPOTS.AB.CA>
> Subject:       clock code challenge:
> Hi,
> After having seen the amazing results from other code challenges
> I thought I would try putting one out there myself.
> The goal is to have a routine that increments and keeps track of
> time in a BCD format.
> Points are scored as follows:
>         1 point for each ROM location used.
>         3 for each RAM.

; BCD clock code challenge
; WARNING: This code is not tested
;
; The regs must be allocated so that a certain bit is set only for
; the hours, for example:

seconds EQU     20
minutes EQU     21
hours   EQU     22
#define hoursFlag               FSR,1

ClockTick:      MOVLW   seconds
loop:           MOVWF   FSR
                       INCF            INDF,f
                       MOVLW   6
                       BTFSC   hoursFlag
                       MOVLW   11
                       ADDWF   INDF,f
                       BTFSS   STATUS,DC
                       SUBWF   INDF,f
                       MOVLW   60
                       BTFSC   hoursFlag
                       MOVLW   24
                       SUBWF   INDF,W
                       BTFSS   STATUS,C
                       RETURN
                       CLRF            INDF
                       INCF            FSR,W
                       BTFSS   hoursFlag
                       GOTO            loop
; This last instruction is not necessary if the code that's executed
; after the routine follows, in that case the "RETURN" of a fwe lines
; above should be a "GOTO..."
                       RETURN

IF it works, it is 19 or 20 ROM words long, and uses 3 regs and FSR.
That is 31 or 32 points.

Regards,




Andres Djordjalian                             KILLspamadjordjKILLspamspamaleph.fi.uba.ar

*** INDICART Carteles Electronicos                    Buenos Aires, Argentina
*** http://www.indicart.com.ar

1997\09\12@163621 by Andres Djordjalian

flavicon
face
Oouch! I've just realized that the times of day from 5 to 10 AM and
PM do actually exist, so I guess my BCD clock code should have a few
changes. Luckily for me, the changes that were needed were just
eliminating two lines, so the result was shorter:

{Quote hidden}

; I removed two lines that were here
{Quote hidden}

IF it works, it is 17 or 18 ROM words long, and uses 3 regs and FSR.
That is 29 or 30 points. Mmm, I guess it is a draw with
Scott-and-Andrew's routine. . . The nice thing about this one is that
it uses no stack space.

Regards,

Andres Djordjalian                             RemoveMEadjordjTakeThisOuTspamaleph.fi.uba.ar

*** INDICART Carteles Electronicos                    Buenos Aires, Argentina
*** http://www.indicart.com.ar

1997\09\12@163827 by sdattalo

face
flavicon
face
Andres Djordjalian wrote:
{Quote hidden}

Actually, the next two lines should be deleted. You don't
want to check for 3 rolling over to 4 if this is the hour's
pass through the loop. Especially since the roll over from
23 to 24 is checked a few lines below.

{Quote hidden}

1997\09\12@234400 by fastfwd

face
flavicon
face
Pedro J Barrios <TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU> wrote:

> Would you please explain me what is being done in these
> instructions:
>
> >         MOVLW   0100H - 060H    <--------------------- ????
> >  [and]
> >         RETLW   0100H - 024H    <- ------------------- ????

Pedro:

I can't tell whether you're asking what the instructions DO, or
what they MEAN, so I'll answer both:

The first instruction, "MOVLW 0100H-060H" loads the W register with
the hexadecimal number A0 (100 hex minus 60 hex = A0 hex).
Similarly, the second instruction returns from the subroutine and
loads W with DC hex.

I could have replaced these with "MOVLW 0A0H" and "RETLW 0DCH" (or by
the decimal equivalents "MOVLW 160" and "RETLW 220"), but then the
code wouldn't hint so strongly as to its purpose, which is:

The "MOVLW 0100H-060H" is followed by an "ADDWF INDF,W".  Since INDF
points to either the SECONDS or MINUTES register, the ADDWF has the
effect of setting the Carry flag if SECONDS or MINUTES is (greater
than or) equal to 60 hex.  After the ADDWF, the code checks the Carry
flag to determine whether the SECONDS or MINUTES register has reached
60; if it has, that register is cleared and the "SKPNC/INCF INDF" at
the top of the subroutine increments the MINUTES or HOURS register,
respectively.

The "RETLW 0100H-024H" has no effect when INDF is pointing at either
MINUTES or SECONDS; when INDF is pointing at HOURS, though, the code
at SUB2 (when it's reached from the "GOTO SUB2") adds 0100H-024H to
the HOURS register, setting the Carry flag if HOURS is (greater than
or) equal to 24.  If the Carry flag's set after the ADDWF, the HOURS
register is cleared.

I know this doesn't make much sense... As we've discussed here
before, really tight code is often VERY hard to follow.

-Andy

=== Andrew Warren - RemoveMEfastfwdspamTakeThisOuTix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

=== For PICLIST help (including "unsubscribe" instructions),
=== put the single word "help" in the body of a message and
=== send it to: listservEraseMEspam.....mitvma.mit.edu

1997\09\13@012526 by mikesmith_oz.nosp*m

flavicon
face
On 12 Sep 97 at 17:36, Andres Djordjalian wrote:

> Oouch! I've just realized that the times of day from 5 to 10 AM and

I've never found them to exist.  I believe they are mythical. <g>
MikeS
<mikesmith_oz@nosp*m.relaymail.net>
(remove the you know what before replying)

1997\09\13@175033 by T. Scott Dattalo

face
flavicon
face
Here's a slight improvement on Andres' implementation.
It still has the same restriction that 'seconds' must
have a ram address that is 0 mod 4 (e.g. 0x20, 0x24,...)

#define hours   FSR,1

       MOVLW   seconds
loop    MOVWF   FSR
       MOVF    INDF,W
       ADDLW   7
       SKPDC
        ADDLW  -6
       MOVWF   INDF

       ADDLW   0x100-0x60
       BTFSC   hours
        ADDLW  (0x100-0x24)-(0x100-0x60)
       SKPC
        RETURN

       MOVWF   INDF
       INCF    FSR,W
       BTFSC   hours
        RETURN

       goto    loop


17 Instructions, 3 RAM locations (or 4 if you count
FSR), and a restrictive location for the variables.
This has not been tested.

Scott

1997\09\17@061531 by : Cassie Carstens

flavicon
face
Hi
I am no judge, don't want to be and certainly not qualified to.
However, the UMPSDEMO looked nice and i needed some code to test
drive it. Andres warned that his code was untested and, for the life
of me, i could not follow it so it got pasted into the UMPSDEMO. In
the end   Scott and Andrew's routines also ended up there. I had to
change a few ORIGINAL INSTRUCTIONS for it to compile. I can only hope
that the replacements does what was originally intended, so please
check. STILL, I CAN NOT FIGURE OUT WHAT NUMBER BASE IS USED IN THE
ROUTINE FROM ANDRES ?????
The tree routines is at the end of this and all the lines preceeded with CCC is
my notes.
The cycles time notes is based on the code in its modified state. REMEMBER I
DID CHANGE SOME INSTRUCTIONS.
Hope you find this informative.
Kind regards en Groetnis
Cassie
****************CODE + SNIPETS***********************************
                       ORG  6                  ;(-c84)
DoenWeer:     ;Call ClockTick
                       ; Call TICK_a
                       ;Call TICK_b
                       goto DoenWeer

; BCD clock code challenge
; From: Andres Djordjalian <EraseMEadjordjspamALEPH.FI.UBA.AR>
; WARNING: This code is not tested
; IF it works, it is 17 or 18 ROM words long, and uses 3 regs and
; FSR. ; That is 29 or 30 points. Mmm, I guess it is a draw with ;
; Scott-and-Andrew's routine. . . The nice thing about this one is
; that it uses no stack space.
; The regs must be allocated so that a certain bit is set only for ; the hours,
for example:
seconds     EQU     $20    ; mm, it would work the same with 20
                                           ;decimal minutes
                  EQU     $21     ; but, to be honest, my intention was to put
hours
                  EQU     $22     ; it in hex
INDF         equ     $00       ;CCC
$DEFINE       hoursFlag   FSR,1
;CCC 16 cycles for one pass and 33 cyc when rollover
;CCC at 528780 cycles it completed a 24hour day!!! That is
;6.059308218919 days in 3204041 cycles.
;CCC seconds and minutes count up to 39 then wraps around ??
;CCC hours goes up to 17 then wraps ??
;Andrew and Scott's version goes up to 59, 59, 23 respectivly

ClockTick:    MOVLW   seconds loop:
              MOVWF   FSR
              INCF    INDF   ;,f ??
              MOVLW   6
; I removed two lines that were here
              ADDWF   INDF   ;,f ??
              BTFSS   STATUS,DC
              SUBWF   INDF   ;,f ??
              MOVLW   60
              BTFSC   fsr,1  ;hoursFlag ??
              MOVLW   24
              SUBWF   INDF,W
              BTFSS   STATUS,C
              RETURN
              CLRF    INDF
              INCF    FSR,W
              BTFSS   fsr,1  ;hoursFlag ??
              GOTO    loop
; This last instruction is not necessary if the code that's executed
; after the routine follows, in that case the "RETURN" of a fwe lines
; above should be a "GOTO..."
              RETURN
;*************************************************************
;From: Andrew Warren <RemoveMEfastfwdEraseMEspamEraseMEix.netcom.com>
;18 instructions, so the score for this one is 4*3 + 18 = 30.
;CCC 60 cycles for one pass and 60 cyc when rollover
;CCC 3204041 cycles later it stopped at 15hours 53min 36secs
;*************************************************************
TICK_a:   MOVLW   SECONDS
                 MOVWF   FSR
                 CALL    SUB1_a
                 CALL    SUB0_a
                 CALL    SUB0_a
                 GOTO    SUB2_a
SUB0_a:   INCF    FSR
                btfsc   STATUS,0       ;SKPNC  ??
SUB1_a:   INCF    INDF
                 MOVLW   6
                 ADDWF   INDF
                 btfss   STATUS,1       ;SKPDC  ??
                SUBWF   INDF
                MOVLW   0100H - 060H
SUB2_a:   ADDWF   INDF,W
                btfsc   STATUS,0
                CLRF    INDF
                RETLW   0100H - 024H

;*****************************************************************
;From: Scott Dattalo <RemoveMEsdattalospam_OUTspamKILLspamunix.SRI.COM> ;19 + 3*4 = 31 points
;With the added benefit that upon returning from 'TICK', the ;carry
;flage indicates if 24 hours has rolled over.
;CCC 45 cyc for one pass and 50 at rollover
;CCC 3204041 cycles later it stopped at 21hours 26 minutes 30 secs
;*****************************************************************
TICK_b:  MOVLW   SECONDS
                MOVWF   FSR
                CALL    SUB1_b
                CALL    SUB0_b
                CALL    SUB0_b
                ADDWF   INDF,W
                goto    SUB2_b
SUB0_b    INCF    FSR            ;,F    ??
                btfsc   status,0       ;SKPNC ??
SUB1_b    INCF   INDF            ;,F    ??
                MOVLW   6
                ADDWF   INDF,W
                btfss   status,1        ;SKPDC ??
                RETLW  0100H - 24H
                MOVWF   INDF
                ADDLW   0xa0
SUB2_b    btfsc   status,0        ;SKPNC ??
                MOVWF   INDF
                RETLW   0
                END
*********Yea, the end*************************

1997\09\17@135503 by fastfwd

face
flavicon
face
: Cassie Carstens <RemoveMEPICLISTTakeThisOuTspamspamMITVMA.MIT.EDU> wrote:

> Scott and Andrew's routines also ended up there. I had to change a
> few ORIGINAL INSTRUCTIONS for it to compile. I can only hope that
> the replacements does what was originally intended, so please
> check.

   Cassie:

   Your replacements are correct; "SKPNZ" is equivalent to "BTFSC
   STATUS,C" and "SKPDC" is equivalent to "BTFSS STATUS,DC".

> STILL, I CAN NOT FIGURE OUT WHAT NUMBER BASE IS USED IN THE
> ROUTINE FROM ANDRES ?????

   Hexadecimal.

   -Andy

=== Meet other PICLIST members at the Embedded Systems Conference:
=== 6:30 pm on Wednesday, 1 October, at Bytecraft Limited's booth.
===
=== For more information on the Embedded Systems Conference,
=== see: http://www.embedsyscon.com/

=== Andrew Warren - EraseMEfastfwdspamspamspamBeGoneix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

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