Forum: Mikrocontroller und Digitale Elektronik Fließkommazahlen in Ganzzahlen umrechnen für Register


von Alexander (Gast)


Angehängte Dateien:

Lesenswert?

Hallo Leute,

ich bin nicht sonderlich geübt im Umgang mit uC Programmierung und 
verwende den uC nur als Mittel zum Zweck, um mir eine Sinus PWM zu 
erzeugen.

Nun ist es so, dass ich gerne die Amplitude der Referenzspannung 
variabel halten möchte, um über den Modulationsgrad auf die gewünschte 
Ausgangsspannung zu kommen.

Im Prinzip also wie folgt:
mit
 von 0 bis 1 (nur eine Halbwelle verwendet aufgrund der Symmetrie) und
 von 0 bis zum Wert des Compareregisters.

Anbei die Sinustabelle:
1
const double sineLookup[]=
2
                       {
3
                           0.00,
4
                           0.02,
5
                           0.03,
6
                           0.05,
7
                           0.06,
8
                           0.08,
9
                           0.09,
10
                           0.11,
11
                           0.13,
12
                           0.14,
13
                           0.16,
14
                           0.17,
15
                           0.19,
16
                           0.20,
17
                           0.22,
18
                           0.23,
19
                           0.25,
20
                           0.26,
21
                           0.28,
22
                           0.29,
23
                           0.31,
24
                           0.32,
25
                           0.34,
26
                           0.35,
27
                           0.37,
28
                           0.38,
29
                           0.40,
30
                           0.41,
31
                           0.43,
32
                           0.44,
33
                           0.45,
34
                           0.47,
35
                           0.48,
36
                           0.50,
37
                           0.51,
38
                           0.52,
39
                           0.54,
40
                           0.55,
41
                           0.56,
42
                           0.58,
43
                           0.59,
44
                           0.60,
45
                           0.61,
46
                           0.63,
47
                           0.64,
48
                           0.65,
49
                           0.66,
50
                           0.67,
51
                           0.68,
52
                           0.70,
53
                           0.71,
54
                           0.72,
55
                           0.73,
56
                           0.74,
57
                           0.75,
58
                           0.76,
59
                           0.77,
60
                           0.78,
61
                           0.79,
62
                           0.80,
63
                           0.81,
64
                           0.82,
65
                           0.83,
66
                           0.84,
67
                           0.84,
68
                           0.85,
69
                           0.86,
70
                           0.87,
71
                           0.88,
72
                           0.88,
73
                           0.89,
74
                           0.90,
75
                           0.90,
76
                           0.91,
77
                           0.92,
78
                           0.92,
79
                           0.93,
80
                           0.94,
81
                           0.94,
82
                           0.95,
83
                           0.95,
84
                           0.96,
85
                           0.96,
86
                           0.96,
87
                           0.97,
88
                           0.97,
89
                           0.98,
90
                           0.98,
91
                           0.98,
92
                           0.99,
93
                           0.99,
94
                           0.99,
95
                           0.99,
96
                           0.99,
97
                           1.00,
98
                           1.00,
99
                           1.00,
100
                           1.00,
101
                           1.00,
102
                           1.00,
103
                           1.00,
104
                           1.00,
105
                           1.00,
106
                           1.00,
107
                           1.00,
108
                           1.00,
109
                           1.00,
110
                           0.99,
111
                           0.99,
112
                           0.99,
113
                           0.99,
114
                           0.99,
115
                           0.98,
116
                           0.98,
117
                           0.98,
118
                           0.97,
119
                           0.97,
120
                           0.96,
121
                           0.96,
122
                           0.96,
123
                           0.95,
124
                           0.95,
125
                           0.94,
126
                           0.94,
127
                           0.93,
128
                           0.92,
129
                           0.92,
130
                           0.91,
131
                           0.90,
132
                           0.90,
133
                           0.89,
134
                           0.88,
135
                           0.88,
136
                           0.87,
137
                           0.86,
138
                           0.85,
139
                           0.84,
140
                           0.84,
141
                           0.83,
142
                           0.82,
143
                           0.81,
144
                           0.80,
145
                           0.79,
146
                           0.78,
147
                           0.77,
148
                           0.76,
149
                           0.75,
150
                           0.74,
151
                           0.73,
152
                           0.72,
153
                           0.71,
154
                           0.70,
155
                           0.68,
156
                           0.67,
157
                           0.66,
158
                           0.65,
159
                           0.64,
160
                           0.63,
161
                           0.61,
162
                           0.60,
163
                           0.59,
164
                           0.58,
165
                           0.56,
166
                           0.55,
167
                           0.54,
168
                           0.52,
169
                           0.51,
170
                           0.50,
171
                           0.48,
172
                           0.47,
173
                           0.45,
174
                           0.44,
175
                           0.43,
176
                           0.41,
177
                           0.40,
178
                           0.38,
179
                           0.37,
180
                           0.35,
181
                           0.34,
182
                           0.32,
183
                           0.31,
184
                           0.29,
185
                           0.28,
186
                           0.26,
187
                           0.25,
188
                           0.23,
189
                           0.22,
190
                           0.20,
191
                           0.19,
192
                           0.17,
193
                           0.16,
194
                           0.14,
195
                           0.13,
196
                           0.11,
197
                           0.09,
198
                           0.08,
199
                           0.06,
200
                           0.05,
201
                           0.03,
202
                           0.02,
203
                       };

Und anbei der Eintrag in das Register:
1
    EPwm1Regs.CMPA.half.CMPA = amplitude*sineLookup[i];

mit diesem Code gibt mir der Compiler eine Fehlermeldung, allerdings 
nichts Konkretes (siehe Bild).

Ich bin mir sicher, dass es daran liegt, dass das Ergebnis von 
aplitude*sineLookup[i] eine Fließkommazahl ist, das Register allerdings 
eine Ganzzahl benötigt.

Verwendet wird ein F28027 DSP von TI.

Ich habe schon nach Castings (int)... im Internet gesucht, aber nichts 
hat so wirklich funktioniert.

Wie kann ich das am geschicktesten in einen Int umwandeln oder runden?

Für eure Hilfe bin ich sehr dankbar.


Gruß,
Alexander

von Dirk B. (devnull)


Lesenswert?

Hallo,

bei Deiner Tabelle würde ich sagen: Multiplikation mit 100...

Grüße,
Dirk

von Steel (Gast)


Lesenswert?

Guck doch mal in Line 117.

Das casting ist sicher nicht das Problem.

von Ralf G. (ralg)


Lesenswert?

Dirk B. schrieb:
> bei Deiner Tabelle würde ich sagen: Multiplikation mit 100...

und die Tabelle auf 90° begrenzen.

von Karl H. (kbuchegg)


Lesenswert?

Ist ein bischen eigenartig.
Denn Zeile 117 in deinem Code ist mitten in einem Kommentar.

Wie geht denn die Fehlermeldung dort weiter. bei 'Location' kann ich nut 
lesen "line 117, exter..."
wie geht dort der Text weiter? Zieh dir doch mal die Spalte dort in den 
Fehlermeldungen breiter, damit du das komplett lesen kannst.

von Ralf G. (ralg)


Lesenswert?

Alexander schrieb:
> mit diesem Code gibt mir der Compiler eine Fehlermeldung, allerdings
> nichts Konkretes (siehe Bild).

Wie ich das lese, kommt die Meldung vom Linker. Der Compiler hat was 
'zurechtgebastelt', dich aber vorher gewarnt!

von Alexander (Gast)


Angehängte Dateien:

Lesenswert?

Dirk B. schrieb:
> Hallo,
>
> bei Deiner Tabelle würde ich sagen: Multiplikation mit 100...
>
> Grüße,
> Dirk

Hi,
das könnte gut funktionieren. Allerdings habe ich dann bei der Amplitude 
weniger Spielraum (da ich die Amplitude um den Faktor 100 kleiner habe. 
Könnte aber sehr gut sein, dass es sich auf die gewünschte 
Ausgangsspannung wenig auswirkt).

Karl Heinz schrieb:
> Ist ein bischen eigenartig.
> Denn Zeile 117 in deinem Code ist mitten in einem Kommentar.
>
> Wie geht denn die Fehlermeldung dort weiter. bei 'Location' kann ich nut
> lesen "line 117, exter..."
> wie geht dort der Text weiter? Zieh dir doch mal die Spalte dort in den
> Fehlermeldungen breiter, damit du das komplett lesen kannst.

Siehe Anhang.

von the expert (Gast)


Lesenswert?

Ralf G. schrieb:
> Dirk B. schrieb:
>> bei Deiner Tabelle würde ich sagen: Multiplikation mit 100...
>
> und die Tabelle auf 90° begrenzen.
und symmetrisch machen! Sie fängt bei 0.00 an, hat dann 0.02 und am Ende 
auch nochmal 0.02. Das riecht nach falsch.

Hat natürlich mit der Programmierung nichts zu tun. Extrathema

von Karl H. (kbuchegg)


Lesenswert?

Alexander schrieb:

>> Wie geht denn die Fehlermeldung dort weiter. bei 'Location' kann ich nut
>> lesen "line 117, exter..."
>> wie geht dort der Text weiter? Zieh dir doch mal die Spalte dort in den
>> Fehlermeldungen breiter, damit du das komplett lesen kannst.

Ist immer noch nicht komplett lesbar.
Dort steht ein Dateiname. Diese Datei kann man mal aufmachen und 
reinschauen, was dort an der Zeile 177 steht.


Ich schätze mal, es hat was mit der Floating Point Arithmetik zu tun. Da 
es eine Meldug vom Linker ist, wird halt die Floating Point nicht 
komplett gelinkt worden sein, wenn überhaupt.

Was besagen die 3 Warnings?

: Bearbeitet durch User
von Alexander (Gast)


Angehängte Dateien:

Lesenswert?

Karl Heinz schrieb:
> Ist immer noch nicht komplett lesbar.
Okay, jetzt habe ich dich verstanden (sorry :/)

Dateiname: F2802x_generic_ram.cmd

Inhalt:
1
/*
2
//###########################################################################
3
//
4
// FILE:    F2802x_generic_ram.cmd
5
//
6
// TITLE:   Linker Command File For 280220 examples that run out of RAM
7
//
8
//          This ONLY includes all SARAM blocks on the 280220 device.
9
//          This does not include flash or OTP.
10
//
11
//          Keep in mind that L0 is protected by the code
12
//          security module.
13
//
14
//          What this means is in most cases you will want to move to
15
//          another memory map file which has more memory defined.
16
//
17
//###########################################################################
18
// $TI Release: f2802x Support Library v200 $
19
// $Release Date: Tue Jul 24 10:01:39 CDT 2012 $
20
//###########################################################################
21
*/
22
23
/* ======================================================
24
// For Code Composer Studio V2.2 and later
25
// ---------------------------------------
26
// In addition to this memory linker command file,
27
// add the header linker command file directly to the project.
28
// The header linker command file is required to link the
29
// peripheral structures to the proper locations within
30
// the memory map.
31
//
32
// The header linker files are found in <base>\F2802x0_headers\cmd
33
//
34
// For BIOS applications add:      F2802x_Headers_BIOS.cmd
35
// For nonBIOS applications add:   F2802x_Headers_nonBIOS.cmd
36
========================================================= */
37
38
/* ======================================================
39
// For Code Composer Studio prior to V2.2
40
// --------------------------------------
41
// 1) Use one of the following -l statements to include the
42
// header linker command file in the project. The header linker
43
// file is required to link the peripheral structures to the proper
44
// locations within the memory map                                    */
45
46
/* Uncomment this line to include file only for non-BIOS applications */
47
/* -l F2802x0_Headers_nonBIOS.cmd */
48
49
/* Uncomment this line to include file only for BIOS applications */
50
/* -l F2802x0_Headers_BIOS.cmd */
51
52
/* 2) In your project add the path to <base>\F2802x0_headers\cmd to the
53
   library search path under project->build options, linker tab,
54
   library search path (-i).
55
/*========================================================= */
56
57
/* Define the memory block start/length for the F2802x0
58
   PAGE 0 will be used to organize program sections
59
   PAGE 1 will be used to organize data sections
60
61
   Notes:
62
         Memory blocks on F280220 are uniform (ie same
63
         physical memory) in both PAGE 0 and PAGE 1.
64
         That is the same memory region should not be
65
         defined for both PAGE 0 and PAGE 1.
66
         Doing so will result in corruption of program
67
         and/or data.
68
69
         The L0 memory blocks is mirrored - that is
70
         it can be accessed in high memory or low memory.
71
         For simplicity only one instance is used in this
72
         linker file.
73
74
         Contiguous SARAM memory blocks can be combined
75
         if required to create a larger memory block.
76
*/
77
78
MEMORY
79
{
80
PAGE 0 :
81
   /* For this example, L0 is split between PAGE 0 and PAGE 1 */
82
   /* BEGIN is used for the "boot to SARAM" bootloader mode   */
83
84
   BEGIN      : origin = 0x000000, length = 0x000002
85
   RAMM0      : origin = 0x000050, length = 0x0003B0
86
   RAML0      : origin = 0x008000, length = 0x000400
87
   RESET      : origin = 0x3FFFC0, length = 0x000002
88
89
   IQTABLES   : origin = 0x3FE000, length = 0x000B50     /* IQ Math Tables in Boot ROM */
90
   IQTABLES2  : origin = 0x3FEB50, length = 0x00008C     /* IQ Math Tables in Boot ROM */
91
   IQTABLES3  : origin = 0x3FEBDC, length = 0x0000AA     /* IQ Math Tables in Boot ROM */
92
93
   BOOTROM    : origin = 0x3FF27C, length = 0x000D44
94
95
96
PAGE 1 :
97
98
   /* For this example, L0 is split between PAGE 0 and PAGE 1 */
99
   BOOT_RSVD   : origin = 0x000002, length = 0x00004E     /* Part of M0, BOOT rom will use this for stack */
100
   RAMM1       : origin = 0x000400, length = 0x000400     /* on-chip RAM block M1 */
101
}
102
103
104
SECTIONS
105
{
106
   /* Setup for "boot to SARAM" mode:
107
      The codestart section (found in DSP28_CodeStartBranch.asm)
108
      re-directs execution to the start of user code.  */
109
   codestart        : >  BEGIN,              PAGE = 0
110
   ramfuncs         : >> RAMM0 | RAML0,      PAGE = 0
111
   .text            : >> RAMM0 | RAML0,      PAGE = 0
112
   .cinit           : >  RAMM0 | RAML0,      PAGE = 0
113
   .pinit           : >  RAMM0 | RAML0,      PAGE = 0
114
   .switch          : >> RAMM0 | RAML0,      PAGE = 0
115
   .reset           : >  RESET,              PAGE = 0, TYPE = DSECT /* not used, */
116
117
   .stack           : > RAMM1,     PAGE = 1
118
   .ebss            : > RAMM1,     PAGE = 1
119
   .econst          : > RAMM1,     PAGE = 1
120
   .esysmem         : > RAMM1,     PAGE = 1
121
122
   IQmath           : > RAMM0 | RAML0,      PAGE = 0
123
   IQmathTables     : > IQTABLES,  PAGE = 0, TYPE = NOLOAD
124
125
  /* Uncomment the section below if calling the IQNexp() or IQexp()
126
      functions from the IQMath.lib library in order to utilize the
127
      relevant IQ Math table in Boot ROM (This saves space and Boot ROM
128
      is 1 wait-state). If this section is not uncommented, IQmathTables2
129
      will be loaded into other memory (SARAM, Flash, etc.) and will take
130
      up space, but 0 wait-state is possible.
131
   */
132
   /*
133
   IQmathTables2    : > IQTABLES2, PAGE = 0, TYPE = NOLOAD
134
   {
135
136
              IQmath.lib<IQNexpTable.obj> (IQmathTablesRam)
137
138
   }
139
   */
140
   /* Uncomment the section below if calling the IQNasin() or IQasin()
141
      functions from the IQMath.lib library in order to utilize the
142
      relevant IQ Math table in Boot ROM (This saves space and Boot ROM
143
      is 1 wait-state). If this section is not uncommented, IQmathTables2
144
      will be loaded into other memory (SARAM, Flash, etc.) and will take
145
      up space, but 0 wait-state is possible.
146
   */
147
   /*
148
   IQmathTables3    : > IQTABLES3, PAGE = 0, TYPE = NOLOAD
149
   {
150
151
              IQmath.lib<IQNasinTable.obj> (IQmathTablesRam)
152
153
   }
154
   */
155
156
}
157
158
/*
159
//===========================================================================
160
// End of file.
161
//===========================================================================
162
*/
Zeile 117:
1
   .stack           : > RAMM1,     PAGE = 1

> Was besagen die 3 Warnings?
Siehe Anhang.

von RobWa (Gast)


Lesenswert?

Hi,


Alexander schrieb:
> 0.03,
>                            0.02,
>                        };
... ist das letzte Komma nach 0.02 nicht zuviel?

von Karl H. (kbuchegg)


Lesenswert?

RobWa schrieb:
> Hi,
>
>
> Alexander schrieb:
>> 0.03,
>>                            0.02,
>>                        };
> ... ist das letzte Komma nach 0.02 nicht zuviel?

Ist wurscht.
Man darf da eines machen, muss es aber nicht machen.

Gerade wenn man Tabellen per Programm generiert, ist es oft praktisch, 
dass man dieses letzte, eigentlich überflüssige, Komma machen darf.

von Alexander (Gast)


Lesenswert?


von Ralf G. (ralg)


Lesenswert?

Schon mal mit den vorgegebenen Ganzzahlen probiert?

von Alexander (Gast)


Lesenswert?

Hi,

Ralf G. schrieb:
> Schon mal mit den vorgegebenen Ganzzahlen probiert?

ich nehme an, dass du die Lookup Tabelle const int sineLookup[] meinst, 
richtig?

Falls ja, damit funktioniert es super. Keine Fehlermeldung (obgleich die 
Warnungen weiterhin vorhanden sind), die DSP Pins wackeln in dem 
gewünschten Muster. Mein Wechselrichter funktioniert tadellos damit.

Falls du die Multiplikation mit 100 meinst, wie hier vorgeschlagen:
Beitrag "Re: Fließkommazahlen in Ganzzahlen umrechnen für Register"

Nein, das habe ich noch nicht ausprobiert. Muss gerade ein paar 
Studenten betreuen.

von Ralf G. (ralg)


Lesenswert?

Alexander schrieb:
> ich nehme an, dass du die Lookup Tabelle const int sineLookup[] meinst

Ja.

Alexander schrieb:
> Wie kann ich das am geschicktesten in einen Int umwandeln oder runden?

Damit hat sich das Problem ja erstmal deutlich verkleinert. Jetzt geht's 
an die Warnungen...

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.