Hallo Forum,
zur übersichtlichen Zahlenausgabe auf LCD und über UART habe ich mir eine Funtion programmiert, welche mir einen Single-Wert in einen String umwandelt, unter Beachtung von Rundung sowie der Vorgabe der Anzahl der Vorkomma- und Nachkommastellen.
Code:$regfile = "m128def.dat" $crystal = 16000000 $framesize = 120 $swstack = 120 $hwstack = 120 $baud = 57600 $sim Declare Function Ausgabe(byval _x As Single , Byval _v As Byte , Byval _n As Byte) As String Dim Ausgabe_string As String * 20 Dim Ausgabe_string_zeichen(21) As Byte At Ausgabe_string Overlay Dim Zahl As Single : Zahl = -0.2 Dim Restzeit As Long : Restzeit = 0 Do Zahl = Zahl + 0.1 Print Ausgabe(zahl , 3 , 3) Loop End Function Ausgabe(byval _x As Single , _v As Byte , _n As Byte) As String '_v = Anzahl Vorkommastellen ; _n = Anzahl Nachkommastellen Local _string As String * 15 : _string = "" Local _laenge As Byte Local _zaehler As Byte : _zaehler = 0 Local _faktor As Long : _faktor = 1 Local _nachkomma_string As String * 10 Local _vorkomma_string As String * 10 Local _anz_zeichen As Byte Local _minus As Byte While _zaehler <> _n _faktor = _faktor * 10 Incr _zaehler Wend _x = _x * _faktor _x = Round(_x) If _x >= 0 Then 'bei x=0 springt der Simulator in Else-Zweig _minus = 0 Else _minus = 1 _x = Abs(_x) End If Ausgabe_string = Str(_x) _laenge = Len(ausgabe_string) _laenge = _laenge - 2 Ausgabe_string = Left(ausgabe_string , _laenge) '".0" abschneiden If Ausgabe_string <> "0" Then _nachkomma_string = Right(ausgabe_string , _n) Else _nachkomma_string = "0" _anz_zeichen = _v + _n While _laenge < _anz_zeichen Ausgabe_string = "0" + Ausgabe_string _laenge = Len(ausgabe_string) Wend _vorkomma_string = Left(ausgabe_string , _v) If _minus = 1 Then _string = "-" Else _string = " " _string = _string + _vorkomma_string _string = _string + "," _string = _string + _nachkomma_string Ausgabe = _string End Function
Funktionieren tut es im Simulator - jedoch wird die 0 als Wert nicht erkannt.
Ich habe die Stelle kommentiert - es wird laut Simulator in den Else-Zweig gesprungen, was dann zur Folge hat, dass mir -0 ausgegeben wird.
Ich hab mir ziemlich schwer getan mit dieser Funktion - hat vielleicht jemand eine bessere, schnellere, ressourcenschonendere?
besten Dank
BoGe-Ro







Zitieren
Lesezeichen