简体   繁体   中英

PIC Assembly: Calling functions with variables

So say I have a variable, which holds a song number. -> song_no

Depending upon the value of this variable, I wish to call a function.

Say I have many different functions:

Fcn1 .... Fcn2 .... Fcn3

So for example, If song_no = 1, call Fcn1 If song_no = 2, call Fcn2 and so forth...

How would I do this?

you should have compare function in the instruction set (the post suggests you are looking for assembly solution), the result for that is usually set a True bit or set a value in a register. But you need to check the instruction set for that.

the code should look something like:

load(song_no, $R1)
cmpeq($1,R1)   //result is in R3
jmpe  Fcn1     //jump if equal
cmpeq ($2,R1)
jmpe  Fcn2
....

Hope this helps

I'm not well acquainted with the , but these sort of things are usually implemented as a jump table . In short, put pointers to the target routines in an array and call/jump to the entry indexed by your song_no . You just need to calculate the address into the array somehow, so it is very efficient. No compares necessary.

To elaborate on Jens' reply the traditional way of doing on 12/14-bit PICs is the same way you would look up constant data from ROM, except instead of returning an number with RETLW you jump forward to the desired routine with GOTO. The actual jump into the jump table is performed by adding the offset to the program counter.

Something along these lines:

    movlw high(table)
    movwf PCLATH
    movf song_no,w
    addlw table
    btfsc STATUS,C
    incf PCLATH
    addwf PCL

table:
    goto fcn1
    goto fcn2
    goto fcn3
     .
     .
     .

Unfortunately there are some subtleties here.

  • The PIC16 only has an eight-bit accumulator while the address space to jump into is 11-bits. Therefore both a directly writable low-byte (PCL) as well as a latched high-byte PCLATH register is available. The value in the latch is applied as MSB once the jump is taken.
  • The jump table may cross a page, hence the manual carry into PCLATH. Omit the BTFSC/INCF if you know the table will always stay within a 256-instruction page.
  • The ADDWF instruction will already have been read and be pointing at table when PCL is to be added to. Therefore a 0 offset jumps to the first table entry.
  • Unlike the PIC18 each GOTO instruction fits in a single 14-bit instruction word and PCL addresses instructions not bytes, so the offset should not be multiplied by two.

All things considered you're probably better off searching for general PIC16 tutorials. Any of these will clearly explain how data/jump tables work, not to mention begin with the basics of how to handle the chip. Frankly it is a particularly convoluted architecture and I would advice staying with the "free" hi-tech C compiler unless you particularly enjoy logic puzzles or desperately need the performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM