1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

XC8 + Assembly: Local Scope?

Discussion in 'Microcontrollers' started by Mike - K8LH, Nov 14, 2015.

  1. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I'm stumped trying to access a local variable from assembly in XC8. If I make "duration" a global variable it works fine. Any ideas, Gentlemen?

    Come to think of it, I also couldn't figure out how to use a constant for the GP2 pin mask.

    TIA. Cheerful regards, Mike

    Code (text):

        void interrupt isr(void)
        {
          static unsigned char duration = 0;

          T0IF = 0;                     // clear TMR0 interrupt flag
      /*                                                                *
       *  Mike McLaren's non-blocking isochronous "beep task" runs      *
       *  at 250-usec intervals and produces 'N' number of 32-msec      *
       *  2000-Hz beeps spaced 32-msecs apart. Set 'beep' variable      *
       *  to 'N+N-1' for number of beeps desired (128 beep limit).      *
       *                                                                *
       *  beeptask() macro;  #define beeptask(n)  beep = (n+n-1)        *
       *                                                                */
          asm("movlw   0b00000100   "); // mask for 'spkr' on GP2
          asm("btfsc   _beep,0      "); // beep b0 == 1? no, skip,
          asm("xorwf   _GPIO,F      "); // else, toggle 'spkr' pin
          asm("movf    _beep,F      "); // beep task running?
          asm("skipz                "); // no, skip, else
          asm("incf    _duration,F  "); // bump 'duration'
          asm("btfsc   _duration,7  "); // 32-ms timout? no, skip,
          asm("decf    _beep,F      "); // else, decrement 'beep'
          asm("btfsc   _duration,7  "); // 32-ms timout? no, skip,
          asm("clrf    _duration    "); // else, reset 32-ms timer
     
     
    Last edited: Nov 14, 2015
  2. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    For this to work, both _beep and _duration must be in the same bank and compiler should do the corresponding movlb.
     
  3. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Yes, I'm aware of the banking issues, North'...

    I'd still like to know how to access the "duration" variable with local scope in the ISR from the assembly language code.

    ~~~~~~~~~~~~~~~~~~~~

    Actually, I think I just found my answer in section "5.12.3.1 Equivalent Assembly Symbols" in the XC8 Compiler User Guide.

    My apologies for taking up bandwidth...
     
    Last edited: Nov 14, 2015
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK

    Don't apologise, post your findings here, so others can see them :D
     
  6. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I am now able to access the static local variable from an assembly language routine as prescribed in the XC8 Compiler User Guide. Works a treat!

    Code (text):

        void interrupt isr(void)
        {
          static char durex = 0;        // beep task duration var

          T0IF = 0;                     // clear TMR0 interrupt flag
      /*                                                                *
       *  Mike McLaren's non-blocking isochronous "beep task" runs      *
       *  at 250-usec intervals and produces 'N' number of 32-msec      *
       *  2000-Hz beeps spaced 32-msecs apart. Set 'beep' variable      *
       *  to 'N+N-1' for number of beeps desired (128 beep limit).      *
       *                                                                *
       *  beep() macro;  #define beep(n)  beep = (n+n-1)                *
       *                                                                */
          asm("movlw   0b00000100   "); // mask for 'spkr' on GP2
          asm("btfsc   _beep,0      "); // beep b0 == 1? no, skip,
          asm("xorwf   _GPIO,F      "); // else, toggle 'spkr' pin
          asm("movf    _beep,F      "); // beep task running?
          asm("skipz                "); // no, skip, else
          asm("incf    isr@durex,F  "); // bump 'duration'
          asm("btfsc   isr@durex,7  "); // 32-ms timout? no, skip,
          asm("decf    _beep,F      "); // else, decrement 'beep'
          asm("bcf     isr@durex,7  "); // reset 32-ms timer
     
     
    Last edited: Nov 15, 2015
  7. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    Were you aware Mike that 'durex' is the leading brand of condoms in the UK? :p
     
  8. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    One should not infer that the "durex" variable provides any sort of "extra protection"...
     
    • Like Like x 2
  9. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    perhaps extra durability :)
     
  10. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,812
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    And added pleasure maybe...?
     
  11. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    924
    Likes:
    86
    Location:
    Georgia, USA
    [​IMG]
     

Share This Page