Mikrocontroller und Digitale Elektronik
›
Thread
Mittelwertbildung C++
Hallo Zusammen,
kann mir jemand auf die Sprünge helfen was ich hier falsch mache:
1 unsigned int cell_voltage [ 2 ][ 26 ] = {{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }};
2 unsigned long cell_current [ 2 ][ 6 ] = {{ 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 }};
3
4
5
6 cell_current [ j ][ 0 ] = ADC_Read ( j + 2 );
7 cell_current [ j ][ 1 ] = ADC_Read ( j + 2 );
8 cell_current [ j ][ 2 ] = ADC_Read ( j + 2 );
9 cell_current [ j ][ 3 ] = ADC_Read ( j + 2 );
10 cell_current [ j ][ 4 ] = ADC_Read ( j + 2 );
11
12 cell_current [ j ][ 5 ] = cell_current [ j ][ 0 ] + cell_current [ j ][ 1 ] + cell_current [ j ][ 2 ] + cell_current [ j ][ 3 ] + cell_current [ j ][ 4 ];
13 cell_current [ j ][ 5 ] = ( cell_current [ j ][ 5 ] / 4 ) & 0xFFFF ;
14
15 cell_voltage [ j ][ 24 ] = cell_current [ j ][ 5 ] & 0xFF ; //Module current lowbyte
16 cell_voltage [ j ][ 25 ] = ( cell_current [ j ][ 5 ] >> 8 ) & 0xFF ; //Module current highbyte
Eigentlich sollte ich in cell_voltage[j][24-25] einen digitalen Rohwert
um die 2050 bekommen, es kommen aber ca. 2560 raus. So schwer kann das
doch nicht sein :/ ?
VG und Danke!
S
Stefan
Gast
02.05.2016 11:08
#4564163
Teil durch 5 wenn du schon 5 Werte summierst
C
Cube_S
Gast
02.05.2016 11:08
#4564164
Nicht durch 4 sondern durch 5 teilen?
Falls cell_voltage und cell_current static storage duration haben sind
die Initialiser dort überflüssig. andernfalls würde ich memset
verwenden. 1 unsigned int cell_voltage [ 2 ][ 26 ];
2 memset ( cell_voltage , 0 , sizeof ( cell_voltage ) );
MP
Marcel P.
Gast
02.05.2016 12:29
#4564235
Noch als Tipp. ich habe so etwas ähnliches mal gemacht. hab das
folgendermassen gelösst:
1 #define N_SAMPLES 5 // geht natürlich auch mit einer Variable...
2
3 unsigned int cell_voltage [ 2 ][ 26 ] = {{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }};
4 unsigned long cell_current [ 2 ] = { 0 , 0 };
5
6 for ( int i = 0 ; i < N_SAMPLES ; i ++ ) {
7 cell_current [ j ] += ADC_Read ( j + 2 );
8 }
9
10 cell_current [ j ] = ( cell_current [ j ] / N_SAMPLES ) & 0xFFFF ;
11
12 cell_voltage [ j ][ 24 ] = cell_current [ j ] & 0xFF ; //Module current lowbyte
13 cell_voltage [ j ][ 25 ] = ( cell_current [ j ] >> 8 ) & 0xFF ; //Module current highbyte
Dabei muss beachtet werden, das die Variable cell_current so gross ist,
das sie die Summe natürlich aufnehmen kann (stichwort int16, int32, etc)
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren