Continue to Site

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.

  • 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.

XC8 + Assembly: Local Scope?

Status
Not open for further replies.

Mike - K8LH

Well-Known Member
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:
    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:
For this to work, both _beep and _duration must be in the same bank and compiler should do the corresponding movlb.
 
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:
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...

Don't apologise, post your findings here, so others can see them :D
 
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:
    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:
And added pleasure maybe...?
 
586.gif
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top