View previous topic :: View next topic |
Author |
Message |
Electrofabio
Joined: 30 Mar 2011 Posts: 5
|
array of function pointers |
Posted: Wed May 04, 2011 9:01 am |
|
|
Hi everybody,
I'm having issues with the implementation of an array of function pointers.
I'm using PIC 18F47J53, with 4.114 compiler.
The firmware I'm using is the following:
Code: | #include <18F47J53.h>
#include <usb_bootloader.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES PLL5 //Divide By 5(20MHz oscillator input)
#FUSES PLLEN
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOCPUDIV
#FUSES NOPROTECT //Code not protected from reading
#FUSES HSPLL //External Clock with PLL enabled and Fosc/4 on RA6
#FUSES SOSC_HIGH
#FUSES CLOCKOUT
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES DSWDTOSC_INT
#FUSES RTCOSC_T1
#FUSES DSBOR
#FUSES DSWDT
#FUSES DSWDT2147483648
//#FUSES IOL1WAY //Allows only one reconfiguration of peripheral pins
#FUSES ADC10
#FUSES MSSPMSK7
#FUSES WPFP
#FUSES NOWPCFG
#FUSES WPDIS
#FUSES WPBEG
#FUSES LS48MHZ
#use delay (clock=48000000)
typedef void (*pointer)(); //function pointer definition
pointer parse_function[10]; //array of function pointer definition
void fun_001();
void fun_002();
void fun_003();
void fun_004();
void fun_005();
void fun_006();
void fun_007();
void fun_008();
void fun_009();
void fun_010();
void main()
{
parse_function[0] = fun_001;
parse_function[1] = fun_002;
parse_function[2] = fun_003;
parse_function[3] = fun_004;
parse_function[4] = fun_005;
parse_function[5] = fun_006;
parse_function[6] = fun_007;
parse_function[7] = fun_008;
parse_function[8] = fun_009;
parse_function[9] = fun_010;
while (true)
{
output_high(pin_D2);
output_high(pin_D4);
delay_ms(500);
output_low(pin_D2);
output_low(pin_D4);
delay_ms(500);
(*parse_function[0])();
(*parse_function[1])();
(*parse_function[2])();
(*parse_function[3])();
(*parse_function[4])();
(*parse_function[5])();
(*parse_function[6])();
(*parse_function[7])();
(*parse_function[8])();
}
}
void fun_001()
{
output_high(pin_D2);
output_high(pin_D4);
delay_ms(1000);
output_low(pin_D2);
output_low(pin_D4);
delay_ms(1000);
}
void fun_002()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(1000);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(1000);
}
void fun_003()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(100);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(100);
}
void fun_004()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(500);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(500);
}
void fun_005()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(200);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(200);
}
void fun_006()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(300);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(300);
}
void fun_007()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(700);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(700);
}
void fun_008()
{
output_high(pin_D3);
output_high(pin_D5);
delay_ms(800);
output_low(pin_D3);
output_low(pin_D5);
delay_ms(800);
}
void fun_009()
{
output_high(pin_D2);
output_high(pin_D4);
delay_ms(500);
output_low(pin_D2);
output_low(pin_D4);
delay_ms(500);
}
void fun_010()
{
}
|
(the code inside fun_001 ... fun_010 is just a debugging example)
When I try to run the program, the PIC restarts at the moment of the call to the first function, continuosly;
Do you have any experience with function pointers? is there any error in the code?
I made a lot of test with different configurations; now I'm wondering about a bug in the compiler or even a problem due to my specific hardware.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 04, 2011 11:38 am |
|
|
I the problem is due to that particular PIC. There may be a bug with the
compiler code for it. I modified your program to use an 18F452 and it
ran OK in the MPLAB simulator (MPLAB vs. 8.66). But with the 18F47J53
it got "Out of Bounds memory access" errors.
I don't have time this morning to try to figure out the problem in the ASM
code. It's possible that some work-around could be devised, but I don't
have time right now.
I tested this with vs. 4.121. It didn't work with that one so I didn't bother
trying it with vs. 4.114. |
|
|
Electrofabio
Joined: 30 Mar 2011 Posts: 5
|
|
Posted: Wed May 04, 2011 3:37 pm |
|
|
Thanks for now
I'll be waiting for a confirmation about this bug. |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Wed May 04, 2011 5:32 pm |
|
|
I would not waste time trying to use function pointers with ccs. Build a switch() with one case for each function you want, wrap it in a function and call it with the number you want.
The compiler will create a jump table with good speed and no bugs. |
|
|
|