16F1789 with simple delay - not working as expected

Author
Stef_r
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2013/12/13 06:55:02
  • Location: The Netherlands
  • Status: offline
2014/08/11 10:29:29 (permalink)
4 (1)

16F1789 with simple delay - not working as expected

Hi,
I'm getting to know the Assembler language through the Gooligum tutorials.
so far so good, I have converted the example code to fit my 16F1789 processor.
But now I see something weird...
 
I have written the code below to alternate 4 LED's.

 
 
 
MAIN_LOOP
                  ; turn on LED @ RE2
                  banksel LATE
                  bsf LATE,RE2               ; RE2 -> HIGH
 
 
 
                  ; turn on LED @ RC0
                  banksel LATC
                  bsf LATC,RC0               ; RC0 -> HIGH
 
 
 
 
 
 
 
                  ; delay 1 Sec.
                  movlw .100                   ; ~30cycl. / min @ 500KHz
                  pagesel delay10             ; delay 50 x 10 ms = 500 ms
                  call delay10
 
 
 
 
 
 
 
                  ; turn off LED RE2/RC0 and turn on LED RC6/RD3
                  banksel LATE
                  bcf LATE,RE2
                  banksel LATC
                  bcf LATC,RC0
                  bsf LATC,RC6
                  banksel LATD
                  bsf LATD,RD3
 
 
 
 
 
 
 
                  ; delay 1 Sec
                  movlw .100                   ; ~30cycl. / min @ 500KHz
                  pagesel delay10             ; delay 100 x 10 ms = 1000 ms
                  call delay10
 
 
 
                   
 
 
 
                  ; turn off LED RC6/RD3
                  banksel LATC
                  bcf LATC,RC6
                  banksel LATD
                  bcf LATD,RD3
 
 
 
 
 
 
 
                  ; repeat forever
                  pagesel MAIN_LOOP
                  goto MAIN_LOOP
 
 
 
 
 
 
 

 
So far, the code above works fine; it does what I expect it to do.
But now let's see the code below.
I've used the exact same code as above, only deleted some parts of it to only simply flash the LED connected to RE2.
 

 
 
 
MAIN_LOOP
                  ; turn on LED @ RE2
                  banksel LATE
                  bsf LATE,RE2                 ; RE2 -> HIGH
 
 
 
                  
 
 
 
                  ; delay 1 Sec.
                  movlw .100                    ; ~30cycl. / min @ 500KHz
                  pagesel delay10              ; delay 100 x 10 ms = 1000 ms
                  call delay10
 
 
 
 
 
 
 
                  ; turn off LED RE2          ; RE2 -> LOW
                  banksel LATE
                  bcf LATE,RE2
 
 
 
 
 
 
 
                  ; repeat forever
                  pagesel MAIN_LOOP
                  goto MAIN_LOOP
 
 
 

 
If I load this code into my PIC, the LED turn on continiously.
however, if I comment-out the the goto line like this  
 ; goto MAIN_LOOP 
the LED @ RE2 does flash correctly....
 
Can someone explain to me why the LED only flashes when the GOTO function is commented-out?
 
 
 
 
post edited by Stef_r - 2014/08/11 11:16:43
#1

10 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 12:48:33 (permalink)
    0
    Strange,
    because removing the GOTO MAIN_LOOP will cause your PIC to run into "the wild" of unprogrammed memory, waste some time and in the end restart...
     
    Makes no sense. Are you sure about your first code working, and the delay amounts?

    GENOVA :D :D ! GODO
    #2
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 12:48:33 (permalink)
    0
    Strange,
    because removing the GOTO MAIN_LOOP will cause your PIC to run into "the wild" of unprogrammed memory, waste some time and in the end restart...
     
    Makes no sense. Are you sure about your first code working, and the delay amounts?

    GENOVA :D :D ! GODO
    #3
    DavidBLit
    Super Member
    • Total Posts : 1574
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 13:21:52 (permalink)
    +2 (2)
    Stef_r
    ...
    however, if I comment-out the the goto line like this  
     ; goto MAIN_LOOP 
    the LED @ RE2 does flash correctly....
     
    Can someone explain to me why the LED only flashes when the GOTO function is commented-out?
    ...



    I'd suspect it's not truly flashing "correctly" and what you see is the side-effect of a watchdog expiry.  Include your config settings with your code, please.
     
    I'd also suspect your delay isn't really doing what you think.
     
    #4
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 13:39:34 (permalink)
    0
    Assuming your delay routine really is on another page, you need to change
                      movlw .100                   ; ~30cycl. / min @ 500KHz
                      pagesel delay10             ; delay 50 x 10 ms = 500 ms
                      call delay10

     
    to
     
                      movlw .100                   ; ~30cycl. / min @ 500KHz
                      pagesel delay10             ; delay 50 x 10 ms = 500 ms
                      call delay10
                     pagesel $


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 15:45:11 (permalink)
    0
    I did consider that, Ric, but since there are no other GOTOs/CALLs in between and there *is* a PAGESEL before GOTO MAIN... that should be ok...

    GENOVA :D :D ! GODO
    #6
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 16:05:33 (permalink)
    +3 (3)
    Yes, you're right Dario.
    The second version of the code doesn't flash simply because the second delay loop has been removed, so the LED is only off for a microscopic period of time!

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    Stef_r
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2013/12/13 06:55:02
    • Location: The Netherlands
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 22:05:29 (permalink)
    0
    Strange indeed and I still haven't figured out why yet.
    The CONFIG code is found below:

    ; PIC16F1789 Configuration Bit Settings
     
    #include "p16F1789.inc"
     
    ; CONFIG1
    ; __config 0xFFA4 
    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
     
    ; CONFIG2
    ; __config 0xDEFF
    __CONFIG _CONFIG2, _WRT_OFF & _VCAPEN_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_OFF

     
    I have used the configuration bits editor of MPLAB to generate the code anjd then paste it into the program.
     
    About flashing 'into the wild'; it *looks* like the LED is flashing the correct time, but maybe I can find a scope to put on the output to see whats really happening here.
     
    The delay code is staight from the Gooligum tutorial and nothing edited:

    #include "p16f1789.inc" ; any enhanced mid-range device will do
    GLOBAL delay10

    ;***** VARIABLE DEFINITIONS
    UDATA
    dc1 res 1 ; delay loop counters
    dc2 res 1

    ;***** SUBROUTINES ******************************************************
    CODE
    ;***** Variable delay: 10 ms to 2.55 s
    ;
    ; Delay = W x 10 ms
    ;
        delay10            ; delay = 2+Wx(223+1023+4)-1+4
        banksel dc1       ; = W x 1250 + 5 cycles
        movwf dc2         ; = W x 10.0 ms @ 8 us/cycle
        dly3 movlw .74    ; inner loop 1: 2 + 74 x 3 - 1
        movwf dc1         ; = 223 cycles
        dly1 decfsz dc1,f
        goto dly1
        dly2 nop            ; inner loop: 256 x 4 - 1
        decfsz dc1,f        ; = 1023 cycles
        goto dly2
        nop
        decfsz dc2,f        ; end outer loop
        goto dly3

        return
        END

     
    #8
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/11 22:30:11 (permalink)
    +1 (1)
    Did you ignore my second post above?
    It explains why you can't see the LED flash.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/12 01:06:22 (permalink)
    0
    ric
    Yes, you're right Dario.
    The second version of the code doesn't flash simply because the second delay loop has been removed, so the LED is only off for a microscopic period of time!




     
    Argh you're right! I missed that... !

    GENOVA :D :D ! GODO
    #10
    Stef_r
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2013/12/13 06:55:02
    • Location: The Netherlands
    • Status: offline
    Re: 16F1789 with simple delay - not working as expected 2014/08/12 04:13:35 (permalink)
    0 (1)
    pinkpinkpink How could I have missed that......
     
    Ofcourse, when I read back the code in the first post I made, I see indeed the missing code after the LED is turned off...
     
    I guess it must have been late yesterday....cool: [8)]
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5