Timer0 loop not working on PIC12F508

assemblerpic

I'm quite new in programming PICS, but have already done some projects. All of them were working fine. But now I'm stuck with a very simple one and I don't find out what's wrong. I'm pretty sure that's due to some really silly mistake, but due to the lack of debugging support of the PIC12F508 I do not know how to find my mistake, so I decided to ask you guys.
The problem is, that the LED (connected to GP5 which is configured as output) is on at first, then is switched off after approx. 1 second and then never goes on.
I expected it to blink once a second…

Here's the code where the bug is hidden. I'm staring at it hours now, but I don't find it. (Ah, btw.: running the code in the simulator is working fine. The error does not occur.)

START

    MOVLW       .199                    ; no WakeUp on pin change, no weak pull-ups, timer source internal, don't care, prescaler for timer0, 1:256
    OPTION                              ; set options

    MOVLW       .0                      ; all GPIOs are outputs
    TRIS        GPIO                    ; set tristate register

; the pre-scaler is set to 1:256, which means that every 256 µs the timer
; is increased by 1. This is done 256 times until it overflows. During this
; time 65 ms have passed. 15 times 65 ms are approx 1 s. So we need to do the
; loop 15 times

RestartLoop:
    MOVLW       .15                     ; 15 loops
    MOVWF       LoopCount               ; write into file register (correctly defined)
NextRound:
    MOVLW       .1                      ; set the TMR0 register to 1
    MOVWF       TMR0                    ; it should not be 0 as the logic below would not work
                                        ; I know, that's not good practice, but at least it should work
CheckTimer:
    MOVF        TMR0, W                 ; read current timer value into W
    BTFSS       STATUS, Z               ; has an overflow occurred and it is 0?
    GOTO        CheckTimer              ; no, read timer value again
    DECF        LoopCount, F            ; yes, decrease the loop counter by 1
    BTFSS       STATUS, Z               ; is the loop counter 0?
    GOTO        NextRound               ; no, then restart Timer0 and wait again until it's 0
    MOVF        LED, W                  ; yes, read the file register LED into W
    BTFSS       STATUS, Z               ; is it 0?
    GOTO        MakeLEDOff              ; no, LED is on, switch it off

    BSF         LED, 0                  ; yes, LED is off, switch it on
                                        ; set bit 0 in the file register LED
    BSF         GPIO, GP5               ; switch on the LED
    GOTO        RestartLoop             ; restart the 1 second loop

MakeLEDOff:
    BCF         LED, 0                  ; clear bit 0 in file register LED (should be 0 then)
    BCF         GPIO, GP5               ; switch off LED
    GOTO        RestartLoop             ; restart the 1 second loop


    MOVLW 0x55                      ; your instructions
    GOTO $                          ; loop forever

    END

Any clue why this is not working as expected?

One more thing that might be of interest is the setting of the configuration bits:

     __CONFIG _OSC_IntRC & _WDT_OFF & _CP_OFF & _MCLRE_OFF

Best regards,
Boxy

Best Answer

You never initialized LED. If any of the upper 7 bits are set, then your algorithm doesn't work.

Related Topic