View previous topic :: View next topic |
Author |
Message |
bennisx
Joined: 05 Jul 2010 Posts: 4
|
void function pointer |
Posted: Mon Jul 05, 2010 12:47 am |
|
|
Hi everybody,
I am using the folowing code to pass the address of the next function to be executed.
Code: |
typedef void (*fn)();
void green()
{
output_bit(pin_green, 0);
delay_ms(500);
output_bit(pin_green, 1);
}
void yellow()
{
output_bit(pin_yellow, 0);
delay_ms(500);
output_bit(pin_yellow, 1);
}
int main(void)
{
fn ptr;
init_hardware();
while (TRUE)
{
ptr = green;
(*ptr)();
ptr = yellow;
(*ptr)();
}
return 0;
}
|
My issue is that when I use the CCS debugger I found that the void pointer "ptr" has a one byte value and not 2 bytes as expected.
When executing this code (*ptr)(); using the single step option in the debugger, the debugger becomes irresponsive and I have to click on HALT PROGRAM to stop it. though the code works.
My aim is to be able to use the debugger single step mode and the void pointer to a function at the same time because my code has about 400 lines and without the single step option in the debugger I won't be able to get it to work.
Thanks a lot |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 05, 2010 2:00 am |
|
|
Post your PIC and compiler version. |
|
|
bennisx
Joined: 05 Jul 2010 Posts: 4
|
|
Posted: Mon Jul 05, 2010 2:07 am |
|
|
PCM programmer wrote: | Post your PIC and compiler version. |
My PIC is the 18F4580 and my compiler ver is 4.104
Thank you |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 05, 2010 11:47 am |
|
|
I installed vs. 4.104, and modified your program so I could test it in
MPLAB simulator. I changed it to do putc() statements so I can see
the results in the Output Window of MPLAB. It works. It displays this output:
Quote: |
GyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGy
GyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGy
GyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGyGy |
It's possible that you have a CCS debugger/IDE issue. But the compiler
itself produces code that does work, at least in this simple test program.
I don't have the CCS IDE (I use MPLAB) so I can't really help any more
on that.
Code: |
#include <18F4580.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
typedef void (*fn)(void);
void green()
{
putc('G');
}
void yellow()
{
putc('y');
}
void main(void)
{
fn ptr;
//init_hardware();
while (TRUE)
{
ptr = green;
(*ptr)();
ptr = yellow;
(*ptr)();
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Jul 06, 2010 2:05 am |
|
|
Agree.
Tried PCM programmers test code, in a hardware ICE, and it reports the pointer as a two byte value, and it works.
Are you sure that it is not just a case of needing to explicitly tell the debugger you are using, that the value is an 'int16', with it possibly assuming the value to be an int8, by 'default?.
Best Wishes |
|
|
bennisx
Joined: 05 Jul 2010 Posts: 4
|
|
Posted: Tue Jul 06, 2010 12:14 pm |
|
|
Ttelmah wrote: | Agree.
Tried PCM programmers test code, in a hardware ICE, and it reports the pointer as a two byte value, and it works.
Are you sure that it is not just a case of needing to explicitly tell the debugger you are using, that the value is an 'int16', with it possibly assuming the value to be an int8, by 'default?.
Best Wishes |
I found out that the code works fine. It's just when I try to debug it using the single step option. when I reach " ptr = yellow; " or "ptr = green; " I have to click on halt program in order to debug the code further because it becomes irresponsive.
Thank you |
|
|
|