View previous topic :: View next topic |
Author |
Message |
Markus
Joined: 30 Oct 2003 Posts: 13
|
Kinda "pointer to function" question! |
Posted: Tue Sep 14, 2004 7:50 am |
|
|
Heyhey!
This code works:
Code: |
#device *=16
byte Read(myData index) {
return *index;
}
void main() {
typedef byte *myData;
typedef byte (*readFunc)(myData index);
readFunc do_Read;
do_Read = Read;
}
|
But this one doesn't:
Code: |
#device *=16
byte Read(myData index) {
return *index;
}
void main() {
typedef byte *myData;
typedef int16 (*readFunc)(myData index);
readFunc do_Read;
do_Read = Read; // (*)
}
|
It returns with:
CCS 3.184 wrote: |
*** Error 118 "...\test.c" Line (*): Invalid type conversion
|
What am I missing?
Markus |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 14, 2004 10:27 am |
|
|
Code: |
byte Read(myData index)
|
The function is declared with a return value of a byte and not int16 |
|
|
Markus
Joined: 30 Oct 2003 Posts: 13
|
|
Posted: Tue Sep 14, 2004 11:35 am |
|
|
Mark, you're right. I made this mistake while writing my first post. It _is_ defined as int16 in the project, but that doesn't work (see above)...
Markus |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 14, 2004 11:50 am |
|
|
Not sure what you are trying to do but this works just fine:
Code: |
#if defined(__PCM__)
#include <16f877.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#elif defined(__PCH__)
#include <18f452.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
typedef byte *myData;
typedef int16 (*readFunc)(myData index);
int16 Read(myData index)
{
return *index;
}
void main()
{
readFunc do_Read;
mydata test;
byte input = 123;
int16 result;
test = &input;
do_Read = Read;
result = (*do_Read)(test);
while(1);
}
|
|
|
|
Tom-H-PIC
Joined: 08 Sep 2003 Posts: 105 Location: New Castle, DE
|
pointers to function ? |
Posted: Tue Sep 14, 2004 1:20 pm |
|
|
Ok the question do pointers work with function or not?
Maybe I have not totally thought this through but what is the point of the pointer to a function thing?
If a person writes a function and uses it in code the compiler uses a goto in the list file to jump to the function. To me this would be the same as a pointer to the function.
Or are we saying that we would be able to use pointers to a function like a pointer to fprintf and only have one fprintf in the total code.
Thanks guys it will help if you can help clear up my thinking on this. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 14, 2004 1:30 pm |
|
|
With a pointer to a function, you can dynamically assign a function call to a variable and then call it. It is great for doing menus and command parsers. You only need one handler and typically get the function addresses from a constant look up table. Here is another use. Let say you have a device that has 2 ports. Lets say you want to be able to select which port the data goes out at runtime. Well you just create 2 functions. One for each port. Now all your output uses the function pointer. Simply set the pointer equal to the function that you want to use (maybe something like
Code: |
if (debug)
pFunc = DebugOutput;
else
pFunc = NormalOutput;
|
) does it make sense yet?
More reading: http://www.ccsinfo.com/forum/viewtopic.php?t=20416 |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 14, 2004 7:03 pm |
|
|
Another example of using pointer to function is in state machines. Do you guys remeber Melly and Moore (spelling?) state machines? Well you can set up a table of states and what function to call when you are in that state and/or what function to call when you change states.
Trampas |
|
|
Bilk Guest
|
pointers to functions |
Posted: Thu Sep 16, 2004 1:21 pm |
|
|
Beware: I have found that pointers to functions work -- but there seems to be a problem if an interrupt occurs exactly when the pointer to function call is being pushed.
The ISR_() destroys the stack push and the program continues neatly. The desired (*ptr_funct)() is then never called.
Workaround, disable interrupts immediately prior to making (*ptr_funct)(); call and renable as first item in called functions.
Compiler version 3.208 - PIC18f452 |
|
|
|