hallo,
habe zwei funktionen geschrieben zur Umwandlung einer 16bit int
Festkommazahlen in einen String zur UART oder LCD Ausgabe
Die Funktionen sind eher nicht so schön geworden...dafür aber vermutlich
selten;)
Hat jemand vielleicht eine Idee wie ich das evtl. anderst oder besser
realisieren könnte?
Sämtliche Verbesserungsvorschläge werden gerne angenommen...
Gruß
1 | static void debug_slave_convert_integer16_fixedpoint_to_array(int16_t fixedpoint_integer, int8_t ausgabearray[], uint8_t anzahl_nachkommastellen)
|
2 | {
|
3 | uint8_t divisor=2;
|
4 |
|
5 | for (uint8_t i=0;i<(anzahl_nachkommastellen-1);i++)
|
6 | {
|
7 | divisor *=2;
|
8 | }
|
9 |
|
10 | //Array enthält 1 Vorkommastelle
|
11 | ausgabearray[0]=fixedpoint_integer/divisor;
|
12 |
|
13 |
|
14 | //Array enthält maximal 4 Nachkommastellen
|
15 |
|
16 | fixedpoint_integer=abs(fixedpoint_integer);
|
17 |
|
18 | for(uint8_t i=1;i<(anzahl_nachkommastellen+1);i++)
|
19 | {
|
20 | fixedpoint_integer=fixedpoint_integer%divisor;
|
21 | fixedpoint_integer=fixedpoint_integer*10;
|
22 | ausgabearray[i]=fixedpoint_integer/divisor;
|
23 | }
|
24 | }
|
25 |
|
26 |
|
27 | static void debug_slave_ausgabe_string_erzeugen(int8_t eingabearray[], uint8_t anzahl_nachkommastellen, char ausgabestring[])
|
28 | {
|
29 | char tmp[4];
|
30 |
|
31 | if(eingabearray[0]<0)
|
32 | {
|
33 | ausgabestring[0]='-';
|
34 | }
|
35 | else
|
36 | {
|
37 | ausgabestring[0]='+';
|
38 | }
|
39 |
|
40 | ausgabestring[1]='\0';
|
41 |
|
42 |
|
43 | if(abs(eingabearray[0])<100)
|
44 | {
|
45 |
|
46 | ausgabestring[1]=' ';
|
47 | ausgabestring[2]='\0';
|
48 | }
|
49 |
|
50 | if(abs(eingabearray[0])<10)
|
51 | {
|
52 |
|
53 | ausgabestring[2]=' ';
|
54 | ausgabestring[3]='\0';
|
55 | }
|
56 |
|
57 |
|
58 | itoa(abs(eingabearray[0]),tmp, 10);
|
59 | strcat(ausgabestring,tmp);
|
60 | strcat(ausgabestring,",");
|
61 |
|
62 | for(uint8_t i=1;i<(anzahl_nachkommastellen+1);i++)
|
63 | {
|
64 | itoa(eingabearray[i],tmp,10);
|
65 | strcat(ausgabestring,tmp);
|
66 | }
|
67 | }
|