For the statement "DebugStr('Zero Counts: ' + s1 + ' (11.7mA resolution)' + #13 + #10);" the compiler has to reserve memory to hold the total string that will be sent to the uart.
In case you e.g. define "procedure DebugStr(var debugstring: string[50]);" it will reserve (on the stack) 50 bytes for the total string sent to DebugStr.
This means also, that, when you take a number that is too low (e.g. "procedure DebugStr(var debugstring: string[5]);" the software will block due to memory corruption. (tested: it happens).
In case of using "procedure DebugStr(var debugstring: string);" the compiler has no clue about the number of bytes to reserve.
The P24 mP compiler version 6.0.1 takes here a size based on the size of S1, which is 129 bytes, plus the constants in the DebugStr statement, comes in total 164 bytes reserved for the total string.
So, as far as I can see using "procedure DebugStr(var debugstring: string);" should be safer than using "procedure DebugStr(var debugstring: string[50]);" because in the latter case one can reserve a too low amount.
p.s. I did not know this could happen.
Anyway: I see the opposite happening of what You detected Jim, but only if the size of the string parameter is too low to hold the complete string parameter for DebugStr. Do you use the same mP compiler version as I do?
I did the test with the P24FJ64GA002, what type did you use?
p.s. to prevent these type of problems it is perhaps better not to use the '+' operator when handling strings. This means creating your own temp string variable and using "strcat".
My Test code:
Code: Select all
program Jim_Keuneman;
{ Declarations section }
var
ADCZeroCounts: Integer;
Res: word;
procedure DebugStr(var debugstring: string);
begin
UART1_Write_Text(debugstring);
end;
begin
{ Main program }
ADPCFG := 0xFFFF; // Configure AN pins as digital I/O
Res := PPS_Mapping (7, _INPUT, _U1RX); // RP7 is uart1 input
Res := PPS_Mapping (6, _OUTPUT, _U1TX); // RP6 is uart1 output
Uart1_init(115200);
delay_ms(250);
ADCZeroCounts := 1250;
while true do
begin
WordToStr(ADCZeroCounts, s1);
DebugStr('Zero Counts: ' + s1 + ' (11.7mA resolution)' + #13 + #10);
end;
end.