Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help to do function pointer in PIC assembler

Status
Not open for further replies.

glias

Full Member level 2
Joined
Jul 31, 2004
Messages
140
Helped
2
Reputation
4
Reaction score
3
Trophy points
1,298
Activity points
1,076
Hello,
I would want to do a function pointer in PIC assembler, does anyone know how to do it ? (I use a PIC18F4620 with MPASM)

thanks
 

I don't use pic, but I use function pointers.

Code:
void function1 (void) //declare two functions: function 1
{
  //function 1 code
}
void function2 (void)  //and 2
{
  //function 2 code
}

void (*fp) (void);  //declare function pointer. The pointed function must 
//be void on both return value and arguments fields, like function1 and function2

void main (void)
{
  uint8 status;
  while(1)
  {
    status = pin_read(P0);
    if (status)  //depending on pin status
    {
      fp = function1;  //fp points to function1
    }
    else
    {
      fp = function2;  //or to function2
    }
    fp();  //run function (function1 or 2, depending on the pin status)
  }
}

Now you could have a function pointer like:

Code:
uint16 (*fp) (uint8, uint8); 
uint16 function3 (uint8 x, uint8 y);  
fp = function3; 
uint16 z = fp(219,5);

Hope that helped.

Sorry, I just saw you asked for assembler, anyway I leave the code for someone who will need it on C.
 
Last edited:

What your attempting to implement is a function pointer table or branch table:



The link above shows various examples of a function pointer table.

Another example:

Branch table - Wiki

Hope the links help.
 

Since I was facing the same problem, here is my solution:

Code:
	; setup function pointer
	movlw	HIGH(func_a)
	movwf	FPTR_H  	  	; high address of function
	movlw	LOW(func_a)
	movwf	FPTR_L  	  	; low addr ...

; ------------------------------------------------------------------------

	; call function pointer

	; 1. push return value onto the call stack
	push	   	; make room on stack
	movlw	HIGH(return_from_function_call)
	movwf	TOSH
	movlw	LOW(return_from_function_call)
	movwf	TOSL
	clrf	TOSU

	; do the call / load function pointer into PC (PCL last, executes PC update)
	clrf		PCLATU
	movf	FPTR_H, W, ACCESS
	movwf	PCLATH
	movf	FPTR_L, W, ACCESS
	movwf	PCL			; call function

return_from_function_call

so since my device has only 16k of flash I only take care of the lower 16bits if your function may be located at >64k you should update the "UPPER" registers to.

Hope this helps
 

Hello all,
Thanks a lot for your help, I'm going to take a look at your example alemaxx, it's seem to be exactly the same as I want to do

I hope that I will understand your code and use it in my application context

regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top