Thus it seems clear that the dsPIC30F4013 is repeatedly resetting, most likely on the call to FDecFloatToStr. Any comment would be apprechiated. (It is easier to see all the commented out code if copied into the IDE). Tested on EASYdsPIC2, 80MHz PLL8 10MHz resonator. And WDT is off.
Code: Select all
program FloatToStrFixedTest;
//USES FloatToStrFDP;
var
txt: string[23];
Realval : Real;
procedure FDecFloatToStr(Rval: Real; var txt: String[23];
FSpace,FDec,RJust,SignJust:Word);
//these two works as in the TurboPascal Str procedure
//FSpace total space to use unless more space required
//FDec : number of decimals after decimal point
//RJust =1 right justification
//SignJust=1 a blank is inserted instead of - sign, even when left justified
//Note StrLnth is always the last index of the string, i.e one less than
//total number of characters before the null terminator.
var
Tmptxt : String[23];
StrLnth,ExpPos,DPPos,i : Word;
Expn,TDigit,RIdx : Word;
begin
Tmptxt:=' 0.000';
{If Rval=0.0 then begin
If SignJust>0
then Tmptxt:=' 0.' //pad in blank for non-negative numbers
else Tmptxt:='0.';
StrLnth:=Word(FDec+1+SignJust);
i:=2+SignJust;
While i<StrLnth+1 do begin
Tmptxt[i]:='0';
Inc(i);
end;
//StrLnth:=i-1;
Tmptxt[i]:=0;
end
else begin
If Rval>0.0 then Rval:=Rval+0.000001; //compensate roundoff error in whole pos numbers
FloatToStr(Rval,Tmptxt);
StrLnth:=0; //Find length of string
While Tmptxt[StrLnth]<>0 do begin
Inc(StrLnth);
end;
If StrLnth>0 then dec(StrLnth); //Length is one less than null terminator
i:=0;
While (Tmptxt[i]<>'.') //find pos of decimal point
and (i<=StrLnth)
do begin
Inc(i);
end;
If i<StrLnth
then DPPos:=i
else begin //No dec. point, Pad string with '.000000' for whole numbers
Tmptxt[StrLnth+1]:='.';
i:=StrLnth+2;
While i<StrLnth+9 do begin
Tmptxt[i]:='0';
inc(i);
end;
Tmptxt[i]:=0; //Terminate string
DPPos:=StrLnth+1;
StrLnth:=i-1; //New string length
end;
i:=0;
While (Tmptxt[i]<>'e') //find pos of exponential indicator
and (i<=StrLnth)
do begin
Inc(i);
end;
If i<StrLnth
then ExpPos:=i
else ExpPos:=0;
StrLnth:=ExpPos-1; //String length now without exponent.
If ExpPos>0 then begin //Remove exponential
Expn:=Tmptxt[ExpPos+2]-48; //find absolute value of exponent, max +-9
If Tmptxt[ExpPos+1]='-' then begin //move dec point for negative numbers
TmpTxt[StrLnth+Expn+1]:=0; //Change null termination of string
i:=StrLnth;
While i>DPPos do begin //shift numbers by exponent places
TmpTxt[i+Expn]:=TmpTxt[i];
dec(i);
end;
TmpTxt[i+Expn]:=TmpTxt[DPPos-1]; //move the number before decimal point
TmpTxt[0]:='0'; //zero before decimal point
If Expn>1 then begin
i:=DPPos+Expn-1;
While i>DPPos do begin //zeroes after decimal point
TmpTxt[i]:='0';
dec(i);
end;
end;
StrLnth:=StrLnth+Expn;
end; //currently not seen use of positive exponents in FloatToStr
end;
If DPPos>0 then begin
StrLnth:=DPPos+FDec; //Now set the right cropped length
TDigit:=Tmptxt[StrLnth+1]-48; //Check for cropped number rounding errors
If TDigit>4 then begin //correct rounding errors
TDigit:=5; //Dummy initiation
i:=StrLnth;
While (TDigit>0) and (i>DPPos) do begin
RIdx:=i;
TDigit:=TmpTxt[i]-48+1;
If TDigit>9 then begin
TDigit:=0;
Dec(i);
end;
Tmptxt[Ridx]:=Tdigit+48;
end;
If TDigit=0 then begin //Rounding before decimal point
TDigit:=5;
i:=DPPos-1;
If Rval>0.0
then DPPos:=0 //DPPos is now used for pos of first number!!!
else DPPos:=1;
While (TDigit>0) and (i>DPPos) do begin
RIdx:=i;
TDigit:=TmpTxt[i]-48+1;
If TDigit>9 then begin
TDigit:=0;
Dec(i);
end;
Tmptxt[Ridx]:=Tdigit+48;
end;
If TDigit=0 then begin
i:=StrLnth;
While i>DPPos do begin
Tmptxt[i+1]:=Tmptxt[i];
Dec(i);
end;
Tmptxt[DPPos]:='1';
Inc(StrLnth);
end;
end;
end;
Tmptxt[StrLnth+1]:=0; //truncate digits/set end of string
end;
If (SignJust>0) and (Rval>0.0) then begin // pad with leading space for non-negative numbers
Tmptxt[StrLnth+2]:=0;
i:=StrLnth;
While i>0 do begin
Tmptxt[i+1]:=Tmptxt[i];
Dec(i);
end;
Tmptxt[i+1]:=Tmptxt[i];
Tmptxt[0]:=' ';
end;
end;
If StrLnth<FSpace-1 then begin
RIdx:=FSpace-1-StrLnth; //Ridx is now the number of space to add.
If RJust>0 then begin //Add blaks to the left of string
i:=StrLnth;
While i>0 do begin
Tmptxt[i+Ridx]:=Tmptxt[i];
dec(i);
end;
Tmptxt[i+Ridx]:=Tmptxt[i];
i:=0;
While i<Ridx do begin
Tmptxt[i]:=' ';
inc(I);
end;
end
else begin //Add blanks after string
i:=StrLnth+1;
While i<StrLnth+Ridx+1 do begin
Tmptxt[i+Ridx]:=Tmptxt[i];
end;
end;
StrLnth:=StrLnth+Ridx; //Terminate string;
Tmptxt[StrLnth+1]:=0;
end;}
txt:=TmpTxt;
end;
begin
ADPCFG := $F0FF; // PORTB is partly digital, pin 8-11 analog
TrisB := $0F0F;
LATB := $0000;
TRISC := $0000;
LATC := $0000; // Set all pins to zero;
TRISD := $0000; // configure pins of portd as output
LATD := $0000;
TRISF := $0000; // configure pins of portd as output
LATF := $0000;
Delay_ms(400);
Lcd_Init(PORTB, 7, 6, 5, 4, PORTD, 0, 1, 2);
//Dataport,pins, COntrPort,rs,rw,enable
Lcd_Cmd(LCD_CURSOR_OFF);
txt:='Test';
Lcd_Out(2, 1, txt);
Delay_ms(1000);
Realval:= 0.0;
FDecFloatToStr(Realval,txt,1,1,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(2000);
Realval:= 1.0;
FDecFloatToStr(Realval,txt,6,3,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(2000);
Realval:= -1.0;
FDecFloatToStr(Realval,txt,6,3,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(2000);
Realval:= -0.5;
FDecFloatToStr(Realval,txt,6,3,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(2000);
Realval:= -0.1;
FDecFloatToStr(Realval,txt,6,3,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(2000);
Realval:= -1.10;
While True do begin
Lcd_Cmd(LCD_CLEAR);
Realval:=RealVal+0.01;
FloatToStr(Realval,txt);
Lcd_Out(1, 1, txt);
//FloatToStrF(Realval,txt);
//Lcd_Out(2, 1, txt);
FDecFloatToStr(Realval,txt,6,3,0,0);
Lcd_Out(2, 1, txt);
Delay_ms(100);
Realval:= Realval+0.01;
end;
end.