Hi, during testing on a P16F628A of the "StateMachine library" I encountered a peculiar phenomenon: the execution of the last entry in a "const array" of "pointers to functions" failed: the PIC resets.
In the code below I would expect an output (via uart) as here:
but I seeStarted
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
From: 2, To: 2, Event: 3here
Apparently when the last entry in the array gets executed something goes wrong, the PIC restarts.Started
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
From: 0, To: 2, Event: 3
From: 1, To: 2, Event: 3
Started <--- reset
etc...
This is the code:
Code: Select all
{$DEFINE DEBUG}
type TActionProc = procedure(From, Towards, Event: byte);
Procedure A_a(from, towards, event: byte);
begin
{$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}
end;
Procedure A_b(from, towards, event: byte);
begin
{$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}
end;
Procedure A_i_(from, towards, event: byte);
begin
{$IFDEF DEBUG} ShowAction(From, Towards, Event); {$ENDIF}
Uart_write_Line('here');
end;
const SmallTable: array[3] of ^TActionProc =
(@A_a,
@A_b,
@A_i_
);
....
begin
{ Main program }
CMCON := 7; // Disable Comparator module's
PORTA := 0; // initialize portA
TRISA := %00110011;
{$IFDEF DEBUG}
Uart1_init(9600);
delay_ms(100);
Uart_write_Line('Started');
for TmpIndex := 0 to 2 do
begin
SmallTable[TmpIndex]^(TmpIndex, 2, 3); // call the routine, first parameter is the index in the table
Uart_write_line('');
end;
Uart_write_line('');
while true do ;
{$ENDIF}
end.
Only the last entry in the table shows the erroneous behaviour, so extending the table with one (not used) entry (e.g. with "nil" as value) makes the problem go away.
I have only see this with the P16F628A, not with e.g the P18F2550.
As far as I can tell the content in rom of "SmallTable" seems to be correct (tough I do not understand it).
Thanks in advance!