BMP Calibration Data Auslesen

Eines vorab: Kann bitte ein Forenadmin diesen Thread löschen? Ich weis nicht was ich da falsch gemacht habe, aber ich kann dort nichts reinschreiben und weg bekomm ich ihn auch nicht.
Danke schonmal im voraus.

Schade, nachdem ich dachte ich habe nun alle Werkzeuge an der Hand um eigenständig mit dem I2C Bus umzugehen, wurde ich nun eines besseren belehrt.

Zielstellung war es Temperatur und Druck aus einem BMP180 zu lesen.
Das lesen der Messwerte war kein Problem aber das Auslesen der Callibrationdata.
Diese sind bei jedem BMP anders und sind im EEPROM des BMP in den Adressen 0xAA-0xBF gespeichert und müssen bei Programmstart einmal abgefragt werden.

Nach anfänglichen Schwierigkeiten habe ich geschafft einen Code zusammenzubasteln der die Callibrationdata aus dem BMP180 rauszieht.... zumindest so fast.

Das ist erstmal der Code der die Calibrationdata aus dem BMP zeihen soll

///////////////////
//GetBMP Function//
///////////////////
void GetBMP (
  short *BMP_AC1=0,
  short *BMP_AC2=0,
  short *BMP_AC3=0,
  short *BMP_B1=0,
  short *BMP_B2=0,
  short *BMP_MB=0,
  short *BMP_MC=0,
  short *BMP_MD=0,
  unsigned short *BMP_AC4=0,
  unsigned short *BMP_AC5=0,
  unsigned short *BMP_AC6=0){
  short MSB=0;
  short LSB=0;
short result=0;


  TWBR = (1<<TWBR7) | (1<<TWBR4) ;                        //Set SCL Clock Speed
  TWSR = 0b00000000;                                      //Set prescaler

for(int a=0xAA;a<=0xBF;a++){
Serial.println(a,HEX);
 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);              //Send StartCondition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = a;                                               //Send per calib data Adress
  //Serial.println(i,HEX);
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK




for(int i=0;i<=1;i++){
   if(i!=(1))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
    
      while  (!(TWCR & (1 << TWINT)));
                                       }                  //Wait for per ACK
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
   
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
    
 result=0;
  if(i==0){MSB=0;MSB=TWDR;Serial.print("IF:     <<");Serial.print(a,HEX);Serial.print(">>  ");Serial.print("MSB");Serial.print("   ");Serial.println(MSB,BIN);
  }
  else{LSB=0;LSB=TWDR;Serial.print("ELSE:   <<");Serial.print(a+i,HEX);Serial.print(">>  ");Serial.print("LSB");Serial.print("  ");Serial.println(LSB,BIN);
      result=MSB<<8;
      result=result+LSB;
      
   Serial.print("RESULT: <<");Serial.print("  ");Serial.print(">>  ");Serial.print(result,BIN);Serial.print("  ");Serial.print(result);Serial.print("  ");
  
   switch(a){
      case   0xAA : *BMP_AC1 = result;Serial.println("AC1");break;
      case   0xAC : *BMP_AC2 = result;Serial.println("AC2"); break;
      case   0xAE : *BMP_AC3 = result;Serial.println("AC3"); break;
      case   0xB0 : *BMP_AC4 = result;Serial.println("AC4"); break;
      case   0xB2 : *BMP_AC5 = result;Serial.println("AC5");  break;
      case   0xB4 : *BMP_AC6 = result;Serial.println("AC6"); break;
      case   0xB6 : *BMP_B1  = result;Serial.println("B1");  break;
      case   0xB8 : *BMP_B2  = result;Serial.println("B2");  break;
      case   0xBA : *BMP_MB  = result;Serial.println("MB");   break;
      case   0xBC : *BMP_MC  = result;Serial.println("MC"); break;
      case   0xBE : *BMP_MD  = result;Serial.println("MD");   break;
      default  :Serial.println("default");  break;
      }//iswitch
   }//if i odd
 }//for i
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition
a++;}//for a

}//GetBMP

Das ergebnis ist dieses

AA
IF:     <<AA>>  MSB   11001
ELSE:   <<AB>>  LSB  11110111
RESULT: <<  >>  1100111110111  6647  AC1
AC
IF:     <<AC>>  MSB   11111011
ELSE:   <<AD>>  LSB  1101011
RESULT: <<  >>  11111111111111111111101101101011  -1173  AC2
AE
IF:     <<AE>>  MSB   11000111
ELSE:   <<AF>>  LSB  10101110
RESULT: <<  >>  11111111111111111100011110101110  -14418  AC3
B0
IF:     <<B0>>  MSB   10000001
ELSE:   <<B1>>  LSB  11010010
RESULT: <<  >>  11111111111111111000000111010010  -32302  0010

Coment:in der Nächten Zeile müsste eigentlich wieder die Adresse gefolgt von einem if kommen. Statt dessen schreibt er irgendwelchen kryptischen Mist der so im Code nicht vorkommt.

RESULT: <���>>  MSB   11111111111111111000000111111111
B2
IF:     <<B2>>  MSB   1100011
ELSE:   <<B3>>  LSB  1001000
RESULT: <<  >>  110001101001000  25416    <<B3>>  LSB  �c�>>  MSB   110001111111111
B4
IF:     <<B4>>  MSB   1010001
ELSE:   <<B5>>  LSB  10010101
RESULT: <<  >>  101000110010101  20885   <<B5>>  LSB  1�Q�>>  MSB   101000111111111
B6
IF:     <<B6>>  MSB   11001
ELSE:   <<B7>>  LSB  1110011
RESULT: <<  >>  1100101110011  6515  B1
B8
IF:     <<B8>>  MSB   0
ELSE:   <<B9>>  LSB  101110
RESULT: <<  >>  101110  46  B2
BA
IF:     <<BA>>  MSB   10000000
ELSE:   <<BB>>  LSB  0
RESULT: <<  >>  11111111111111111000000000000000  -32768  MB
BC
IF:     <<BC>>  MSB   11010001
ELSE:   <<BD>>  LSB  11110110
RESULT: <<  >>  11111111111111111101000111110110  -11786  MC
BE
IF:     <<BE>>  MSB   1010
ELSE:   <<BF>>  LSB  111
RESULT: <<  >>  101000000111  2567  MD

Ich weis nur nicht warum.

Ich habe auch den BMP schonmal getauscht aber das Ergebnis war gleich.

Neben den kryptischen Werten verwirrt mich auch ein wenig das hier

RESULT: <<  >>  11111111111111111100011110101110  -14418  AC3

Die Zahl scheint zu stimmen aber AC3 ist ein short und ein short sind doch nur 16 bit + Vorzeichenbit oder?
also sollte die zahl maximal
1 11000111 10101110
sein statt
11111111 11111111 11000111 10101110
oder versteh ich da was falsch?

nochmal zum Abgleich der gesamte Code


short pres =0;
short temp  =0;

 short BMP_AC1=0;
  short BMP_AC2=0;
  short BMP_AC3=0;
  short BMP_B1=0;
  short BMP_B2=0;
  short BMP_MB=0;
  short BMP_MC=0;
  short BMP_MD=0;
  unsigned short BMP_AC4=0;
  unsigned short BMP_AC5=0;
  unsigned short BMP_AC6=0;


////////////////////
//ReadBMP Function//
////////////////////
void ReadBMP(short*temp,short*pres){
   short MSB=0;
   short LSB=0;
   short result=0; 
///////////////////////////////
//Get uncompensed temperature//
///////////////////////////////
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send StartCondition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xF4;                                            //Send per Control Register Adress
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0x2E;                                           //Send per Control Setting
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK  
  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK

   for(int i=0;i<2;i++){
    if(i!=(2))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
   
    switch(i){
      case 0 : MSB = TWDR;  break;
      case 1 : LSB = TWDR;  break;
   
      }
    
    }//for 
   result=MSB<<8;
   result=result+LSB;
  
Serial.print(" result: "); Serial.println(result); 
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition
  };//ReadBMP














  
///////////////////
//GetBMP Function//
///////////////////
void GetBMP (
  short *BMP_AC1=0,
  short *BMP_AC2=0,
  short *BMP_AC3=0,
  short *BMP_B1=0,
  short *BMP_B2=0,
  short *BMP_MB=0,
  short *BMP_MC=0,
  short *BMP_MD=0,
  unsigned short *BMP_AC4=0,
  unsigned short *BMP_AC5=0,
  unsigned short *BMP_AC6=0){
  short MSB=0;
  short LSB=0;
short result=0;


  TWBR = (1<<TWBR7) | (1<<TWBR4) ;                        //Set SCL Clock Speed
  TWSR = 0b00000000;                                      //Set prescaler

for(int a=0xAA;a<=0xBF;a++){
Serial.println(a,HEX);
 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);              //Send StartCondition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = a;                                               //Send per calib data Adress
  //Serial.println(i,HEX);
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK




for(int i=0;i<=1;i++){
   if(i!=(1))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
    
      while  (!(TWCR & (1 << TWINT)));
                                       }                  //Wait for per ACK
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
   
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
    
 result=0;
  if(i==0){MSB=0;MSB=TWDR;Serial.print("IF:     <<");Serial.print(a,HEX);Serial.print(">>  ");Serial.print("MSB");Serial.print("   ");Serial.println(MSB,BIN);
  }
  else{LSB=0;LSB=TWDR;Serial.print("ELSE:   <<");Serial.print(a+i,HEX);Serial.print(">>  ");Serial.print("LSB");Serial.print("  ");Serial.println(LSB,BIN);
      result=MSB<<8;
      result=result+LSB;
      
   Serial.print("RESULT: <<");Serial.print("  ");Serial.print(">>  ");Serial.print(result,BIN);Serial.print("  ");Serial.print(result);Serial.print("  ");
  
   switch(a){
      case   0xAA : *BMP_AC1 = result;Serial.println("AC1");break;
      case   0xAC : *BMP_AC2 = result;Serial.println("AC2"); break;
      case   0xAE : *BMP_AC3 = result;Serial.println("AC3"); break;
      case   0xB0 : *BMP_AC4 = result;Serial.println("AC4"); break;
      case   0xB2 : *BMP_AC5 = result;Serial.println("AC5");  break;
      case   0xB4 : *BMP_AC6 = result;Serial.println("AC6"); break;
      case   0xB6 : *BMP_B1  = result;Serial.println("B1");  break;
      case   0xB8 : *BMP_B2  = result;Serial.println("B2");  break;
      case   0xBA : *BMP_MB  = result;Serial.println("MB");   break;
      case   0xBC : *BMP_MC  = result;Serial.println("MC"); break;
      case   0xBE : *BMP_MD  = result;Serial.println("MD");   break;
      default  :Serial.println("default");  break;
      }//iswitch
   }//if i odd
 }//for i
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition
a++;}//for a

}//GetBMP
 



























void setup() {
Serial.begin(9600);
Serial.println(" ");
 GetBMP(&BMP_AC1,&BMP_AC2,&BMP_AC3,&BMP_B1,&BMP_B2,&BMP_MB,&BMP_MC,&BMP_MD,BMP_AC4,BMP_AC5,BMP_AC6);
}

void loop() {

ReadBMP(&temp,&pres);
Serial.println(BMP_AC1,BIN);
Serial.println(BMP_AC2,BIN);
Serial.println(BMP_AC3,BIN);
Serial.println(BMP_AC4,BIN);
Serial.println(BMP_AC5,BIN);
Serial.println(BMP_AC6,BIN);
Serial.println(BMP_B1,BIN);
Serial.println(BMP_B2,BIN);
Serial.println(BMP_MB,BIN);
Serial.println(BMP_MC,BIN);
Serial.println(BMP_MD,BIN);
}





//////////////////////////////
//Only for Arduino IDE Debug//
//////////////////////////////
void output(const byte value, const char *suffix)
{
  if (value < 10) {Serial.print('0');}
  Serial.print(value);
  Serial.print(suffix);
}

Anbei mal noch das Datenblat vom BMP
BMP180 Datasheet by SparkFun Electronics _ Digi-Key Electronics.zip (159.1 KB)

Ich habe den Switch ein wenig umgeschrieben

  Serial.print("RESULT: <<");Serial.print("  ");Serial.print(">>  ");Serial.print(result,BIN);Serial.print("  ");Serial.print(result);Serial.print("  ");
  switch(a){
      case   0xAA : BMPf_AC1 = result; break;
      case   0xAC : BMPf_AC2 = result;  break;
      case   0xAE : BMPf_AC3 = result;  break;
      case   0xB0 : BMPf_AC4 = result; break;
      case   0xB2 : BMPf_AC5 = result;  break;
      case   0xB4 : BMPf_AC6 = result;  break;
      case   0xB6 : BMPf_B1  = result;  break;
      case   0xB8 : BMPf_B2  = result;  break;
      case   0xBA : BMPf_MB  = result;  break;
      case   0xBC : BMPf_MC  = result;   break;
      case   0xBE : BMPf_MD  = result;   break;
      default  :Serial.println("default");  break;
      }//iswitch
   
   }//if i odd
 }//for i
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition


a++;}//for a

//*BMP_AC1=BMPf_AC1;*BMP_AC2=BMPf_AC2;*BMP_AC3=BMPf_AC3;*BMP_B1=BMPf_B1;*BMP_B2=BMPf_B2;*BMP_MB=BMPf_MB;*BMP_MC=BMPf_MC;*BMP_MD=BMPf_MD;*BMP_AC4=BMPf_AC4;*BMP_AC5=BMPf_AC5;*BMP_AC6=BMPf_AC6;

}//GetBMP

Er setzt jetzt nicht mehr die Variablen in der Hauptfunktion sondern setzt Variablen innerhalb der Unterfunktion "GetBMP".
Die Zeile die diese Variablen dann in die Hauptfunktion überschreibt

//*BMP_AC1=BMPf_AC1;*BMP_AC2=BMPf_AC2;*BMP_AC3=BMPf_AC3;*BMP_B1=BMPf_B1;*BMP_B2=BMPf_B2;*BMP_MB=BMPf_MB;*BMP_MC=BMPf_MC;*BMP_MD=BMPf_MD;*BMP_AC4=BMPf_AC4;*BMP_AC5=BMPf_AC5;*BMP_AC6=BMPf_AC6;

wurde deaktiviert.
Das Ergebnis ist:

21:25:09.494 -> AA
21:25:09.494 -> RESULT: <<  >>  1100111110111  6647  
21:25:09.494 -> AC
21:25:09.494 -> RESULT: <<  >>  11111111111111111111101101101011  -1173  
21:25:09.594 -> AE
21:25:09.594 -> RESULT: <<  >>  11111111111111111100011110101110  -14418  
21:25:09.627 -> B0
21:25:09.627 -> RESULT: <<  >>  11111111111111111000000111010010  -32302  
21:25:09.694 -> B2
21:25:09.694 -> RESULT: <<  >>  110001101001000  25416  
21:25:09.727 -> B4
21:25:09.727 -> RESULT: <<  >>  101000110010101  20885  
21:25:09.794 -> B6
21:25:09.794 -> RESULT: <<  >>  1100101110011  6515  
21:25:09.827 -> B8
21:25:09.827 -> RESULT: <<  >>  101110  46  
21:25:09.860 -> BA
21:25:09.860 -> RESULT: <<  >>  11111111111111111000000000000000  -32768  
21:25:09.927 -> BC
21:25:09.927 -> RESULT: <<  >>  11111111111111111101000111110110  -11786  
21:25:09.993 -> BE
21:25:09.993 -> RESULT: <<  >>  101000000111  2567  

Das sieht erstmal gut aus, auch wenn mir manche Werte komisch erscheinen, sind sie schon mal nicht kryptisch.

Wenn ich nun die Zeile aktiviere, die die Übertragung der Werte auf die Hauptfunktion überträgt.

*BMP_AC1=BMPf_AC1;*BMP_AC2=BMPf_AC2;*BMP_AC3=BMPf_AC3;*BMP_B1=BMPf_B1;*BMP_B2=BMPf_B2;*BMP_MB=BMPf_MB;*BMP_MC=BMPf_MC;*BMP_MD=BMPf_MD;*BMP_AC4=BMPf_AC4;*BMP_AC5=BMPf_AC5;*BMP_AC6=BMPf_AC6;

sieht das Ergebnis plötzlich so aus:

22:34:45.209 -> AA
22:34:45.209 -> RESULT: <<  >>  1100111110111  6647    AC1
22:34:45.242 -> AC
22:34:45.242 -> RESULT: <<  >>  11111111111111111111101101101011  -1173    AC2
22:34:45.308 -> AE
22:34:45.308 -> RESULT: <<  >>  11111111111111111100011110101110  -14418    AC3
22:34:45.375 -> B0
22:34:45.408 -> RESULT: <<  >>  11111111111111111000000111010010  -32302    AC4
22:34:45.475 -> B2
22:34:45.475 -> RESULT: <<  >>  110001101001000  25416    AC5
22:34:45.508 -> B4
22:34:45.508 -> RESULT: <<  >>  101000110010101  20885    AC6
22:34:45.608 -> B6
22:34:45.608 -> RESULT: <<  >>  1100101110011  6515    B1
22:34:45.608 -> B8
22:34:45.608 -> RESULT: <<  >>  101110  46    B2
22:34:45.641 -> BA
22:34:45.674 -> RESULT: <<  >>  11111111111111111000000000000000  -32768    MB
22:34:45.741 -> BC
22:34:45.741 -> RESULT: <<  >>  11111111111111111101000111110110  -11786    MC
22:34:45.841 -> BE
22:34:45.841 -> RESULT: <<  >>  101000000111  2567    MD
22:34:45.841 -> 10  'Q�
22:34:45.841 -> AC2 -1173
22:34:45.874 -> AC3 -14418
22:34:45.874 -> AC4 0
22:34:45.874 -> AC5 0
22:34:45.907 -> AC6 0
22:34:45.907 -> B1 6515
22:34:45.907 -> B2 46
22:34:45.907 -> MB -32768
22:34:45.941 -> MC -11786
22:34:45.941 -> MD 2567

Nun die Frage aller Fragen, warum das denn? 0o

Also aus irgend einem Grund zwematscht er mir den ersten Wert und setzt AC4 AC5 und AC6 auf 0. Der Rest passt soweit ich das beurteilen kann.

ok ich habe jetzt den print Teil in den Loop verfrachtet und mal ein 3 sekunden Delay davor gehangen und nun ist die Ausgabe korrekt.

void setup() {
Serial.begin(9600);
Serial.println(" ");
 GetBMP(&BMP_AC1,&BMP_AC2,&BMP_AC3,BMP_AC4,BMP_AC5,BMP_AC6,&BMP_B1,&BMP_B2,&BMP_MB,&BMP_MC,&BMP_MD);
 

}

void loop() {
delay(3000);
ReadBMP(&temp,&pres);
Serial.print("AC1 ");Serial.println(BMP_AC1);
Serial.print("AC2 ");Serial.println(BMP_AC2);
Serial.print("AC3 ");Serial.println(BMP_AC3);
Serial.print("AC4 ");Serial.println(BMP_AC4);
Serial.print("AC5 ");Serial.println(BMP_AC5);
Serial.print("AC6 ");Serial.println(BMP_AC6);
Serial.print("B1 ");Serial.println(BMP_B1);
Serial.print("B2 ");Serial.println(BMP_B2);
Serial.print("MB ");Serial.println(BMP_MB);
Serial.print("MC ");Serial.println(BMP_MC);
Serial.print("MD ");Serial.println(BMP_MD);
}

Das würde ja bedeuten das das Programm den Serial.print ausführt bevor die Funktion GetBMP richtig durchgelaufen ist.

Aber soweit wie ich weis fährt der doch eigentlich keine befehle gleichzeitig sondern alle in der Reihenfolge ab wie sie im Code stehen oder?
also
start a end a start b end b

und nicht
start a start b end b end a

Eine weitere Verwirrung ist das wenn ich den delay in das void setup packe stoppt das Programm an der Stelle. ich ha schon mal rumgelesen da hieß es aber delay im setup würde eig. gehen.

GetBMP(&BMP_AC1,&BMP_AC2,&BMP_AC3,BMP_AC4,BMP_AC5,BMP_AC6,&BMP_B1,&BMP_B2,&BMP_MB,&BMP_MC,&BMP_MD);

Na wer findet den Fehler^^

bei AC4,5,6 fehlt das & deswegen hat er da brühe reingeschrieben

Was ich aber immer noch nicht verstehe warum bei

uint16_t BMPf_AC4=0;
RESULT: <<  >>  11111111111111111000000111010010  -32302    AC4

rauskommt

weil der sind von uint ist doch eig das die zahl immer positiv ist oder?

ich habe das dann so gelöst:

case   0xB0 : BMPf_AC4 = abs(result);Serial.println("  AC4");  break;

aber so richtig versteh ich nicht warum das notwendig ist

Ich habe so einen Sensor nicht, daher halte ich mich bislang zurück. Aber möglicherweise könnte ich mit einem EEPROM simulieren, dazu benötigte ich aber Dein aktuelles Programm, denn ich habe die Übersicht verloren :crazy_face:

Programm zum Beschreiben des EEPROMs mit den Beispieldaten aus dem Datenblatt
#include <Wire.h>
#include <I2C_EEPROM.h>  // I2C EEPROM Bibliothek nicht vom Bibliotheksmanager, sondern von combie: http://forum.arduino.cc/index.php?topic=369667.0
AT24C256<> eep; // default Adresse 0x50
// AT24C32<0x57> eep; // Das EEProm auf der china üblichen RTC3231

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  delay(500);
  // die Speichergröße des EEProm wird in der Klasse gespeichert
  // kann also ausgelesen werden, ohne dass das EEProm wirlich
  // bereit ist
  Serial.print("EE Prom lenth: ");
  Serial.println(eep.length());

  if (eep.ready()) // eeprom bereit ?
  {
    // eeprom wurde gefunden, und ist bereit
    Serial.println ("EEProm ist bereit!");
  } else
  {
    // eeprom hat nicht geantwortet
    // Auch direkt nach dem schreiben blockiert das eeprom ein paar ms
    Serial.println("EEProm ist nicht bereit, Verkabelung prüfen ");
  }

  // schreiben
  uint8_t param[] = {0x01, 0x98, 0xFF, 0xB8, 0xC7, 0xD1, 0x7F, 0xE5, 0x7F, 0xF5, 0x5A, 0x71, 0x18, 0x2E, 0x00, 0x04, 0x80, 0x00, 0xDD, 0xF9, 0x0B, 0x34};
  const byte ANZAHL = sizeof(param);
  eep.put(0xAA, param);
  // lesen
  uint8_t test[ANZAHL];
  eep.get(0xAA, test);
  for (byte j = 0; j < ANZAHL; j++)
  {
    Serial.print(" 0x");
    if (test[j] < 0xF) Serial.print('0');
    Serial.print(test[j], HEX);
  }
  Serial.println();

  for (byte j = 0; j < ANZAHL; j += 2)
  {
    uint16_t wert_u = (test[j] << 8) + test[j + 1];
    int16_t wert_s = (test[j] << 8) + test[j + 1];
    Serial.print(wert_u); Serial.print('\t'); Serial.println(wert_s);
  }
}

void loop() {}
Ausgabe
12:52:59.305 -> EE Prom lenth: 32768
12:52:59.359 -> EEProm ist bereit!
12:52:59.359 ->  0x01 0x98 0xFF 0xB8 0xC7 0xD1 0x7F 0xE5 0x7F 0xF5 0x5A 0x71 0x18 0x2E 0x00 0x04 0x80 0x00 0xDD 0xF9 0x0B 0x34
12:52:59.459 -> 408	408
12:52:59.506 -> 65464	-72
12:52:59.506 -> 51153	-14383
12:52:59.506 -> 32741	32741
12:52:59.559 -> 32757	32757
12:52:59.559 -> 23153	23153
12:52:59.559 -> 6190	6190
12:52:59.559 -> 4	4
12:52:59.559 -> 32768	-32768
12:52:59.606 -> 56825	-8711
12:52:59.606 -> 2868	2868

Sieht nicht wie eine Lösung aus!
Eher wie ein dirty Hack für einen Fehler im Konzept.

Das muss dieses konzentrieren aufs wesentliche sein von dem du gesprochen hast.... danke das hilft mir wirklich weiter.

Ne im ernst das das nicht DER Weg ist habe ich selbst schon drüber geschrieben da kannst du dir solche kommentare klemmen. Wenn du unbedingt was posten musst in den oberen beiträgen sind 100 Fragestellungen mit denen du dich gerne befassen kannst, abe rdiesen mist kannst du dir sparen

aso ja hier der aktuelle code. momentan läuft die BMP_Setup Funktion auch immer zwei mal durch, wo ich noch nciht rausgefunden habe warum.


i
int16_t pres = 0;
int16_t temp = 0;

int16_t BMP_AC1 = 0;
int16_t BMP_AC2 = 0;
int16_t BMP_AC3 = 0;
int16_t BMP_B1 = 0;
int16_t BMP_B2 = 0;
int16_t BMP_MB = 0;
int16_t BMP_MC = 0;
int16_t BMP_MD = 0;
uint16_t BMP_AC4 = 0;
uint16_t BMP_AC5 = 0;
uint16_t BMP_AC6 = 0;


////////////////////
//ReadBMP Function//
////////////////////
void ReadBMP(int16_t *temp, int16_t *pres) {
  int16_t MSB = 0;
  int16_t LSB = 0;
  int16_t result = 0;

  //Get uncompensed temperature//

  TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);  //Send StartCondition
  while (!(TWCR & (1 << TWINT)))
    ;                                 //Wait for per ACK
  TWDR = 0xEE;                        //Send Slave Adress +Write(0)
  TWCR = (1 << TWINT) | (1 << TWEN);  //Set TWINT 1
  while (!(TWCR & (1 << TWINT)))
    ;                                 //Wait for per ACK
  TWDR = 0xF4;                        //Send per Control Register Adress
  TWCR = (1 << TWINT) | (1 << TWEN);  //Set TWINT 1
  while (!(TWCR & (1 << TWINT)))
    ;                                 //Wait for per ACK
  TWDR = 0x2E;                        //Send per Control Setting
  TWCR = (1 << TWINT) | (1 << TWEN);  //Set TWINT 1
  while (!(TWCR & (1 << TWINT)))
    ;  //Wait for per ACK

  TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);  //Send New Start Condition
  while (!(TWCR & (1 << TWINT)))
    ;                                               //Wait for per ACK
  TWDR = 0xEF;                                      //Send Slave Adress +Read(1)
  TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);  //Set TWINT 1
  while (!(TWCR & (1 << TWINT)))
    ;  //Wait for per ACK

  for (int i = 0; i < 2; i++) {
    if (i != (2)) {
      TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);  //Send ACK
      while (!(TWCR & (1 << TWINT)))
        ;
    }  //Wait for per ACK
    else {
      TWCR = (1 << TWINT) | (1 << TWEN);  //Send NACK
      while (!(TWCR & (1 << TWINT)))
        ;
    }  //Wait for per ACK

    switch (i) {
      case 0: MSB = TWDR; break;
      case 1: LSB = TWDR; break;
    }

  }  //for
  result = MSB << 8;
  result = result + LSB;
  *temp = result;
  Serial.print(" result: ");
  Serial.println(result);
  TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);  //Send Stop Condition

};  //ReadBMP

//////////////////////
//BMP_Setup Function//
//////////////////////
void BMP_Setup(int16_t *BMP_AC1 = 0, int16_t *BMP_AC2 = 0, int16_t *BMP_AC3 = 0, uint16_t *BMP_AC4 = 0, uint16_t *BMP_AC5 = 0, uint16_t *BMP_AC6 = 0, int16_t *BMP_B1 = 0, int16_t *BMP_B2 = 0, int16_t *BMP_MB = 0, int16_t *BMP_MC = 0, int16_t *BMP_MD = 0) {
  int16_t MSB = 0;
  int16_t LSB = 0;
  int16_t result = 0;
  int16_t BMPf_AC1 = 0;
  int16_t BMPf_AC2 = 0;
  int16_t BMPf_AC3 = 0;
  int16_t BMPf_B1 = 0;
  int16_t BMPf_B2 = 0;
  int16_t BMPf_MB = 0;
  int16_t BMPf_MC = 0;
  int16_t BMPf_MD = 0;
  uint16_t BMPf_AC4 = 0;
  uint16_t BMPf_AC5 = 0;
  uint16_t BMPf_AC6 = 0;

  TWBR = (1 << TWBR7) | (1 << TWBR4);  //Set SCL Clock Speed
  TWSR = 0b00000000;                   //Set prescaler

  for (int a = 0xAA; a <= 0xBF; a++) {


    TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);  //Send StartCondition
    while (!(TWCR & (1 << TWINT)))
      ;                                 //Wait for per ACK
    TWDR = 0xEE;                        //Send Slave Adress +Write(0)
    TWCR = (1 << TWINT) | (1 << TWEN);  //Set TWINT 1
    while (!(TWCR & (1 << TWINT)))
      ;        //Wait for per ACK
    TWDR = a;  //Send per calib data Adress
    //Serial.println(i,HEX);
    TWCR = (1 << TWINT) | (1 << TWEN);  //Set TWINT 1
    while (!(TWCR & (1 << TWINT)))
      ;  //Wait for per ACK

    TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);  //Send New Start Condition
    while (!(TWCR & (1 << TWINT)))
      ;                                               //Wait for per ACK
    TWDR = 0xEF;                                      //Send Slave Adress +Read(1)
    TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);  //Set TWINT 1
    while (!(TWCR & (1 << TWINT)))
      ;  //Wait for per ACK

    for (int i = 0; i <= 1; i++) {

      if (i != (1)) {
        TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);  //Send ACK
        while (!(TWCR & (1 << TWINT)))
          ;
      }  //Wait for per ACK
      else {
        TWCR = (1 << TWINT) | (1 << TWEN);  //Send NACK
        while (!(TWCR & (1 << TWINT)))
          ;
      }  //Wait for per ACK

      result = 0;
      if (i == 0) {
        MSB = 0;
        MSB = TWDR;
      } else {
        LSB = 0;
        LSB = TWDR;
        result = MSB << 8;
        result = result + LSB;

        //Serial.print("RESULT: <<");Serial.print("  ");Serial.print(">>  ");Serial.print(result,BIN);Serial.print("  ");Serial.print(result);Serial.print("  ");
        switch (a) {
          case 0xAA: BMPf_AC1 = result; break;
          case 0xAC: BMPf_AC2 = result; break;
          case 0xAE: BMPf_AC3 = result; break;
          case 0xB0: BMPf_AC4 = abs(result); break;
          case 0xB2: BMPf_AC5 = abs(result); break;
          case 0xB4: BMPf_AC6 = abs(result); break;
          case 0xB6: BMPf_B1 = result; break;
          case 0xB8: BMPf_B2 = result; break;
          case 0xBA: BMPf_MB = result; break;
          case 0xBC: BMPf_MC = result; break;
          case 0xBE: BMPf_MD = result; break;
          default:;

        }  //i switch

      }  //else i odd
    }    //for i

    TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);  //Send Stop Condition
    a++;
  }  //for a
  *BMP_AC1 = BMPf_AC1;
  *BMP_AC2 = BMPf_AC2;
  *BMP_AC3 = BMPf_AC3;
  *BMP_AC4 = BMPf_AC4;
  *BMP_AC5 = BMPf_AC5;
  *BMP_AC6 = BMPf_AC6;
  *BMP_B1 = BMPf_B1;
  *BMP_B2 = BMPf_B2;
  *BMP_MB = BMPf_MB;
  *BMP_MC = BMPf_MC;
  *BMP_MD = BMPf_MD;  //
}  //BMP_Setup

//////////////////////////////
//Only for Arduino IDE Debug//
//////////////////////////////
void output(const byte value, const char *suffix) {
  if (value < 10) { Serial.print('0'); }
  Serial.print(value);
  Serial.print(suffix);
}  //output

////////
//MAIN//
////////

void setup() {
  Serial.begin(9600);
  Serial.println(" ");
  BMP_Setup(&BMP_AC1, &BMP_AC2, &BMP_AC3, &BMP_AC4, &BMP_AC5, &BMP_AC6, &BMP_B1, &BMP_B2, &BMP_MB, &BMP_MC, &BMP_MD);
  Serial.print("AC1 ");
  Serial.println(BMP_AC1);
  Serial.print("AC2 ");
  Serial.println(BMP_AC2);
  Serial.print("AC3 ");
  Serial.println(BMP_AC3);
  Serial.print("AC4 ");
  Serial.println(BMP_AC4);
  Serial.print("AC5 ");
  Serial.println(BMP_AC5);
  Serial.print("AC6 ");
  Serial.println(BMP_AC6);
  Serial.print("B1 ");
  Serial.println(BMP_B1);
  Serial.print("B2 ");
  Serial.println(BMP_B2);
  Serial.print("MB ");
  Serial.println(BMP_MB);
  Serial.print("MC ");
  Serial.println(BMP_MC);
  Serial.print("MD ");
  Serial.println(BMP_MD);
}

void loop() {


  Serial.print("AC1 ");
  Serial.println(BMP_AC1);
  Serial.print("AC2 ");
  Serial.println(BMP_AC2);
  Serial.print("AC3 ");
  Serial.println(BMP_AC3);
  Serial.print("AC4 ");
  Serial.println(BMP_AC4);
  Serial.print("AC5 ");
  Serial.println(BMP_AC5);
  Serial.print("AC6 ");
  Serial.println(BMP_AC6);
  Serial.print("B1 ");
  Serial.println(BMP_B1);
  Serial.print("B2 ");
  Serial.println(BMP_B2);
  Serial.print("MB ");
  Serial.println(BMP_MB);
  Serial.print("MC ");
  Serial.println(BMP_MC);
  Serial.print("MD ");
  Serial.println(BMP_MD);
  ReadBMP(&temp, &pres);
  Serial.print("temp: ");
  Serial.println(temp);
  Serial.print("pres: ");
  Serial.println(pres);
}

Beim Versuch, Dein Programm zu verstehen, bin ich über ein paar Dinge gestolpert.

  int16_t MSB = 0;
  int16_t LSB = 0;

Das "B" steht für Byte, sollte daher uint8_t sein.

  for (int i = 0; i < 2; i++) {
...
    for (int i = 0; i <= 1; i++) {

Auf einen 8-Bit-Rechner ist int für eine Zahl, die nur 0 oder 1 sein kann, der falsche Typ. Besser wäre bool, auch mit byte könnte ich mich anfreunden.

  for (int i = 0; i < 2; i++) {
    if (i != (2)) {

Wenn i kleiner 2 sein soll, ist es immer ungleich 2.

      if (i != (1)) {

Wenn i nur 0 oder 1 sein kann, dann fände ich if (i == 0) { klarer.

Leider ist es mir noch nicht gelungen, mit Deinem Programm Daten vom EEPROM zu lesen.

Ich habe den Code angepasst.


int16_t pres =0;
int16_t temp  =0;

int16_t BMP_AC1=0;
  int16_t BMP_AC2=0;
  int16_t BMP_AC3=0;
  int16_t BMP_B1=0;
  int16_t BMP_B2=0;
  int16_t BMP_MB=0;
  int16_t BMP_MC=0;
  int16_t BMP_MD=0;
uint16_t BMP_AC4=0;
uint16_t BMP_AC5=0;
uint16_t BMP_AC6=0;


////////////////////
//ReadBMP Function//
////////////////////
void ReadBMP(int16_t*temp,int16_t*pres){
   uint16_t MSB=0;
   uint8_t LSB=0;
   uint16_t result=0; 

//Get uncompensed temperature//

  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send StartCondition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xF4;                                            //Send per Control Register Adress
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0x2E;                                           //Send per Control Setting
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK  
  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK

   for(uint8_t i=0;i<2;i++){
    if(i==(0))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
      while  (!(TWCR & (1 << TWINT))); }                  //Wait for per ACK
   
    switch(i){
      case 0 : MSB = TWDR;  break;
      case 1 : LSB = TWDR;  break;
   
      }
    
    }//for 
   result=MSB<<8;
   result=result+LSB;
  *temp=result;
Serial.print(" result: "); Serial.println(result); 
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition

  };//ReadBMP

//////////////////////
//BMP_Setup Function//
//////////////////////
void BMP_Setup (int16_t *BMP_AC1=0,int16_t *BMP_AC2=0,int16_t *BMP_AC3=0,uint16_t *BMP_AC4=0,uint16_t *BMP_AC5=0,uint16_t *BMP_AC6=0,int16_t *BMP_B1=0,int16_t *BMP_B2=0,int16_t *BMP_MB=0,int16_t *BMP_MC=0,int16_t*BMP_MD=0){
  Uint16_t MSB=0;
  Uint8_t LSB=0;
int16_t result=0;
int16_t BMPf_AC1=0;
int16_t BMPf_AC2=0;
int16_t BMPf_AC3=0;
int16_t BMPf_B1=0;
int16_t BMPf_B2=0;
int16_t BMPf_MB=0;
int16_t BMPf_MC=0;
int16_t BMPf_MD=0;
uint16_t BMPf_AC4=0;
uint16_t BMPf_AC5=0;
uint16_t BMPf_AC6=0;

  TWBR = (1<<TWBR7) | (1<<TWBR4) ;                        //Set SCL Clock Speed
  TWSR = 0b00000000;                                      //Set prescaler

  for(int a=0xAA;a<=0xBF;a++){
    
   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);              //Send StartCondition
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
   TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = a;                                               //Send per calib data Adress
   //Serial.println(i,HEX);
   TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  
   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
    TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);              //Set TWINT 1
      while  (!(TWCR & (1 << TWINT)));                     //Wait for per ACK

   for(uint8_t i=0;i<=1;i++){

     if(i==(0))
     {TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;                      //Send NACK
       while  (!(TWCR & (1 << TWINT))); Serial.print(a,HEX);Serial.print(" ");Serial.print("TWDR: ");Serial.println(TWDR,BIN);}                  //Wait for per ACK        
     else
      {TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;         //Send ACK
      while  (!(TWCR & (1 << TWINT)));Serial.print(a,HEX);Serial.print(" ");Serial.print("TWDR: ");Serial.println(TWDR,BIN);}                    //Wait for per NACK   
  
    result=0;
    if(i==0){MSB=0;MSB=TWDR;}
    else{LSB=0;LSB=TWDR;
    result=MSB<<8;
    result=result+LSB;
      
     //Serial.print("RESULT: <<");Serial.print("  ");Serial.print(">>  ");Serial.print(result,BIN);Serial.print("  ");Serial.print(result);Serial.print("  ");
      switch(a){
        case   0xAA : BMPf_AC1 = result;       break;
        case   0xAC : BMPf_AC2 = result;       break;
        case   0xAE : BMPf_AC3 = result;       break;
        case   0xB0 : BMPf_AC4 = abs(result);  break;
        case   0xB2 : BMPf_AC5 = abs(result);  break;
        case   0xB4 : BMPf_AC6 = abs(result);  break;
        case   0xB6 : BMPf_B1  = result;       break;
        case   0xB8 : BMPf_B2  = result;       break;
        case   0xBA : BMPf_MB  = result;       break;
        case   0xBC : BMPf_MC  = result;       break;
        case   0xBE : BMPf_MD  = result;       break;
        default :;
      
      }//i switch
    
    }//else i odd
  }//for i

  TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition
  a++;
}//for a
*BMP_AC1=BMPf_AC1;*BMP_AC2=BMPf_AC2;*BMP_AC3=BMPf_AC3;*BMP_AC4=BMPf_AC4;*BMP_AC5=BMPf_AC5;*BMP_AC6=BMPf_AC6;*BMP_B1=BMPf_B1;*BMP_B2=BMPf_B2;*BMP_MB=BMPf_MB;*BMP_MC=BMPf_MC;*BMP_MD=BMPf_MD;//
}//BMP_Setup
 
//////////////////////////////
//Only for Arduino IDE Debug//
//////////////////////////////
void output(const byte value, const char *suffix){
  if (value < 10) {Serial.print('0');}
  Serial.print(value);
  Serial.print(suffix);
} //output

////////
//MAIN//
////////

void setup() {
Serial.begin(9600);
Serial.println(" ");
 BMP_Setup(&BMP_AC1,&BMP_AC2,&BMP_AC3,&BMP_AC4,&BMP_AC5,&BMP_AC6,&BMP_B1,&BMP_B2,&BMP_MB,&BMP_MC,&BMP_MD);
 Serial.print("AC1 ");Serial.println(BMP_AC1);
Serial.print("AC2 ");Serial.println(BMP_AC2);
Serial.print("AC3 ");Serial.println(BMP_AC3);
Serial.print("AC4 ");Serial.println(BMP_AC4);
Serial.print("AC5 ");Serial.println(BMP_AC5);
Serial.print("AC6 ");Serial.println(BMP_AC6);
Serial.print("B1 ");Serial.println(BMP_B1);
Serial.print("B2 ");Serial.println(BMP_B2);
Serial.print("MB ");Serial.println(BMP_MB);
Serial.print("MC ");Serial.println(BMP_MC);
Serial.print("MD ");Serial.println(BMP_MD);
}

void loop() {


 Serial.print("AC1 ");Serial.println(BMP_AC1);
Serial.print("AC2 ");Serial.println(BMP_AC2);
Serial.print("AC3 ");Serial.println(BMP_AC3);
Serial.print("AC4 ");Serial.println(BMP_AC4);
Serial.print("AC5 ");Serial.println(BMP_AC5);
Serial.print("AC6 ");Serial.println(BMP_AC6);
Serial.print("B1 ");Serial.println(BMP_B1);
Serial.print("B2 ");Serial.println(BMP_B2);
Serial.print("MB ");Serial.println(BMP_MB);
Serial.print("MC ");Serial.println(BMP_MC);
Serial.print("MD ");Serial.println(BMP_MD);
ReadBMP(&temp,&pres);
Serial.print("temp: ");Serial.println(temp);
Serial.print("pres: ");Serial.println(pres);
}






Das du damit keine Daten auslesen kannst, kann das daran liegen das das vorwärtszählen nach erhalten des NACK kein standert sondern eine Eigenart des BMP ist? das würde bedeuten das du immer Adresse 0xAA ausliest.

Die variable MSB LSB habe ich in

  Uint16_t MSB=0;
  Uint8_t LSB=0;

geändert. Du hattest ja 2x uint8_t vorgeschlagen aber den MSB muss ich ja 8 bit nach links schieben und das geht in einem uint 8_t nicht oder?

Wäre möglich, aber das Datenblatt verstehe ich anders:

As long as the EEPROM receives an acknowledge, it will continue to increment
the data word address and serially clock out sequential data words.

Tut er denn, was er soll?

Wie sieht die Ausgabe aus?

Oder!

Da mich Bitmanipulationen immer mal wieder überraschen, probiere ich es gene aus:

Programm
uint16_t MSB16 = 0;
uint8_t MSB08 = 0;
uint8_t LSB = 0;
uint16_t result = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("\nStart");
  MSB16 = 0x5A;
  MSB08 = 0x5A;
  LSB = 0x3C;
  result = MSB16 << 8;
  result = result + LSB;
  Serial.print("MSB16: 0x"); Serial.print(MSB16, HEX); Serial.print(" LSB: 0x"); Serial.print(LSB, HEX); Serial.print(" result: 0x"); Serial.println(result, HEX);
  result = MSB08 << 8;
  result = result + LSB;
  Serial.print("MSB08: 0x"); Serial.print(MSB08, HEX); Serial.print(" LSB: 0x"); Serial.print(LSB, HEX); Serial.print(" result: 0x"); Serial.println(result, HEX);
}

void loop() {}

Ausgabe:

10:24:44.288 -> Start
10:24:44.288 -> MSB16: 0x5A LSB: 0x3C result: 0x5A3C
10:24:44.335 -> MSB08: 0x5A LSB: 0x3C result: 0x5A3C
1 Like

Oh ok das mit dem msb8 ist spannend

Liegt das daran das er man erst in result schreibt und dann schiebt?

Nein, daran, dass du keine C/C++ Grundlagen gelernt hast.
Hier fehlt dir der richtige Umgang mit Datentypen.

Merke:
int ist der kleinstmögliche Datentype, mit dem gerechnet, bzw. der geschoben werden kann.

Ganz großes Maul, aber vom Tuten und Blasen keine Ahnung.

Nein, ein häufiger Fehler, der mir, obwohl ich es eigentlich besser weiß, auch mal unterläuft.

Allgemeine Aussagen kann ich nicht machen, ich beziehe mich auf UNO (ATmega328P), Teensy 3.2 und ESP32. Da wird rechts des Gleichheitszeichens zunächst mit dem Typ int gerechnet, bei UNO acht sechzehn Bit, bei den anderen 32 Bit. Das Schieben findet also im Typ int statt. Das Ergebnis wird dann der links stehenden Variablen zugewiesen und dabei, wenn nötig, im Typ gewandelt.

Möchte man rechts mit einem anderen Typ rechnen, kann man dies beeinflussen. Eine Multiplikation mit 1.0 wandelt den Typ in Gleitkomma. Aber das nur am Rande.

1 Like

Beim UNO ist int 16 Bit breit.

Gruß Tommy

1 Like

Dank an @Tommy56 für die Korrektur!

Ein Achtbitter hat int mit sechzehn Bit, ein Zweiunddreißigbitter aber "nur" zweiunddreißig Bit, das nenne ich "historisch gewachsen".

Um dem Compiler eindeutig die von mir gewünschte Bitzahl mitzuteilen, habe ich mir int8_t, int16_t, int32_T, uint8_t, uint16_t und uint32_t als Typen im Umfeld UNO/Nano/ProMini (ATmega328P), Teensy 3.2 (pausiert derzeit) und ESP32 angewöhnt. Wenn ich Zweifel habe, probiere ich mit einem kleinen Testprogramm :wink:

Da Du meine Fragen aus #11 noch nicht beantwortest hast, lese ich jetzt Zeitung :slightly_smiling_face:

Sry mein akk war alle und wir haben nur bedingt strom im Gebäude^^(2024 und so)

Hier der nochmal angepasste code


int16_t pres=0;
int16_t temp=0;

int16_t BMP_AC1=0;
int16_t BMP_AC2=0;
int16_t BMP_AC3=0;
int16_t BMP_B1=0;
int16_t BMP_B2=0;
int16_t BMP_MB=0;
int16_t BMP_MC=0;
int16_t BMP_MD=0;
uint16_t BMP_AC4=0;
uint16_t BMP_AC5=0;
uint16_t BMP_AC6=0;

////////////////////
//ReadBMP Function//
////////////////////
void ReadBMP(int16_t*temp,int16_t*pres){
   uint8_t MSB=0;
   uint8_t LSB=0;
   uint16_t result=0; 

//Get uncompensed data//
  TWCR = (1<<TWINT) | (1<<TWSTA)| (1<<TWSTO) | (1<<TWEN);              //Send StartCondition
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xF4;                                                                        //Send per Control Register Adress
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0x2E;                                           //Send per Control Setting
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   
   for(uint8_t i=0;i<2;i++){
    if(i==(0))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
      while  (!(TWCR & (1 << TWINT)));                    //Wait for per ACK
      MSB = TWDR;Serial.print("T-MSB: ");Serial.println(MSB);}                                         //Set MSB
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
      while  (!(TWCR & (1 << TWINT)));                    //Wait for per ACK
      LSB = TWDR;Serial.print("LT-SB: ");Serial.println(LSB);}                                         //Set LSB   
    }//for i
      result=MSB<<8;
      result=result+LSB;
      *temp=result;

TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition

//READ PRESS//

TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);               //Send StartCondition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xF4;                                            //Send per Control Register Adress
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0x34;                                                                               //Send per Control Setting
  TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK  
  
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
  TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
  TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);               //Set TWINT 1
    while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK

   for(uint8_t i=0;i<2;i++){
    if(i==(0))
    {  TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;        //Send ACK
      while  (!(TWCR & (1 << TWINT)));                    //Wait for per ACK
      MSB = TWDR;LSB = TWDR;Serial.print("P-MSB: ");Serial.println(MSB);  }                                         //Set MSB
    else
    {  TWCR = (1<<TWINT) | (1<<TWEN) ;                    //Send NACK
      while  (!(TWCR & (1 << TWINT)));                    //Wait for per ACK
      LSB = TWDR;LSB = TWDR;Serial.print("P-LSB: ");Serial.println(LSB);}                                          //Set LSB                  
    }//for i
      result=MSB<<8;
      result=result+LSB;
      *pres=result;

TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);               //Send Stop Condition

  };//ReadBMP

//////////////////////
//BMP_Setup Function//
//////////////////////
void BMP_Setup (int16_t *BMP_AC1=0,int16_t *BMP_AC2=0,int16_t *BMP_AC3=0,uint16_t *BMP_AC4=0,uint16_t *BMP_AC5=0,uint16_t *BMP_AC6=0,int16_t *BMP_B1=0,int16_t *BMP_B2=0,int16_t *BMP_MB=0,int16_t *BMP_MC=0,int16_t*BMP_MD=0){
  uint16_t MSB=0;
  uint8_t LSB=0;
int16_t result=0;
int16_t BMPf_AC1=0;
int16_t BMPf_AC2=0;
int16_t BMPf_AC3=0;
int16_t BMPf_B1=0;
int16_t BMPf_B2=0;
int16_t BMPf_MB=0;
int16_t BMPf_MC=0;
int16_t BMPf_MD=0;
uint16_t BMPf_AC4=0;
uint16_t BMPf_AC5=0;
uint16_t BMPf_AC6=0;

  TWBR = (1<<TWBR7) | (1<<TWBR4) ;                        //Set SCL Clock Speed
  TWSR = 0b00000000;                                      //Set prescaler

  for(int a=0xAA;a<=0xBF;a++){

   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);              //Send StartCondition

     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK

   TWDR = 0xEE;                                            //Send Slave Adress +Write(0)
   TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = a;                                               //Send per read EEPROM Adress
   TWCR = (1<<TWINT) | (1<<TWEN);                          //Set TWINT 1
     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
     
   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);             //Send New Start Condition

     while  (!(TWCR & (1 << TWINT)));                      //Wait for per ACK
   TWDR = 0xEF;                                            //Send Slave Adress +Read(1)
    TWCR = (1<<TWINT) | (1<<TWEA)| (1<<TWEN);              //Set TWINT 1
      while  (!(TWCR & (1 << TWINT)));                     //Wait for per ACK

   for(uint8_t i=0;i<=1;i++){                              // first read MSB then LSB


    //Serial.print("TWDR: ");Serial.println(TWDR);Serial.print(" i: ");Serial.println(i);
    if(i==(0))
     {TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) ;          //Send ACK
      //Serial.print("TWSR if 0 ");Serial.println(TWSR,HEX);
       while  (!(TWCR & (1 << TWINT)));}                   //Wait for per ACK        
     else
      {TWCR = (1<<TWINT) | (1<<TWEN) ;                     //Send NACK
       //Serial.print("TWSR else ");Serial.println(TWSR,HEX);
      while  (!(TWCR & (1 << TWINT)));}
     


     
    //Serial.print("-TWDR: ");Serial.println(TWDR);Serial.print(" i: ");Serial.println(i);Serial.print(" a: ");Serial.println(a);
    result=0;
    if(i==0)  {MSB=TWDR;}
    else      {LSB=TWDR;
               result=MSB<<8;
               result=result+LSB;
               //Serial.print("MSB: ");Serial.println(MSB);Serial.print(" LSB: ");Serial.println(LSB); 

      switch(a){
        case   0xAA : BMPf_AC1 = result;       break;
        case   0xAC : BMPf_AC2 = result;       break;
        case   0xAE : BMPf_AC3 = result;       break;
        case   0xB0 : BMPf_AC4 = abs(result);  break;
        case   0xB2 : BMPf_AC5 = abs(result);  break;
        case   0xB4 : BMPf_AC6 = abs(result);  break;
        case   0xB6 : BMPf_B1  = result;       break;
        case   0xB8 : BMPf_B2  = result;       break;
        case   0xBA : BMPf_MB  = result;       break;
        case   0xBC : BMPf_MC  = result;       break;
        case   0xBE : BMPf_MD  = result;       break;
        default :;
      
      }//i switch select export Variable
    
    }//else i odd
  }//for i read MSB LSB
   //Serial.print("TWSR AFTER STOP ");Serial.println(TWCR,BIN);
  a++;
}//for a

*BMP_AC1=BMPf_AC1;*BMP_AC2=BMPf_AC2;*BMP_AC3=BMPf_AC3;*BMP_AC4=BMPf_AC4;*BMP_AC5=BMPf_AC5;*BMP_AC6=BMPf_AC6;*BMP_B1=BMPf_B1;*BMP_B2=BMPf_B2;*BMP_MB=BMPf_MB;*BMP_MC=BMPf_MC;*BMP_MD=BMPf_MD;//

  
}//BMP_Setup
 
//////////////////////////////
//Only for Arduino IDE Debug//
//////////////////////////////
void output(const byte value, const char *suffix){
  if (value < 10) {Serial.print('0');}
  Serial.print(value);
  Serial.print(suffix);
} //output

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//MAIN//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);

Serial.println(" ");
BMP_Setup(&BMP_AC1,&BMP_AC2,&BMP_AC3,&BMP_AC4,&BMP_AC5,&BMP_AC6,&BMP_B1,&BMP_B2,&BMP_MB,&BMP_MC,&BMP_MD);

Serial.print("AC1 ");Serial.println(BMP_AC1);
Serial.print("AC2 ");Serial.println(BMP_AC2);
Serial.print("AC3 ");Serial.println(BMP_AC3);
Serial.print("AC4 ");Serial.println(BMP_AC4);
Serial.print("AC5 ");Serial.println(BMP_AC5);
Serial.print("AC6 ");Serial.println(BMP_AC6);
Serial.print("B1 ");Serial.println(BMP_B1);
Serial.print("B2 ");Serial.println(BMP_B2);
Serial.print("MB ");Serial.println(BMP_MB);
Serial.print("MC ");Serial.println(BMP_MC);
Serial.print("MD ");Serial.println(BMP_MD);
}
void loop() {
ReadBMP(&temp,&pres);
Serial.print("temp: ");Serial.println(temp);
Serial.print("pres: ");Serial.println(pres);
}






Das ist das ergebnis

AC1 6647
AC2 -1173
AC3 -14418
AC4 32302
AC5 25416
AC6 20885
B1 6515
B2 46
MB -32768
MC -11786
MD 2567
T-MSB: 42
LT-SB: 0
P-MSB: 48
P-LSB: 0
temp: 10752
pres: 12288

Ich habe das Datenblatt nochmal gewälzt und da folgendes gefunden

Ich ahbe das dann so aufegabaut

aber da bekomme ich beim TWDR auslesen nur die Refisteradresse aus dem vorherigem schritt raus.

Wenn ich das so aufbaue


bekomme ich ....Werte^^ob die stimmen weis ich noch nicht das finde ich heute abend beim verrechnen raus

Eventuell könntest Du mit der Bibliothek Wire die Daten auslesen, um sie mit denen Deines Programms zu vergleichen. Darüberhinaus könntest Du auch eine Bibliothek für den Sensor verwenden, um auch die berechneten Werte zu überprüfen. Das wäre zumindest meine Vorgehensweise :slightly_smiling_face:

2 Likes

Hallo,

meine Vorgehensweise ist/wäre wie die von agmue. Den Sensor erstmal mit bekannten Libs zum laufen bringen. Heißt mit der Arduino Wire Lib oder der von Peter Fleury. Wie man den Sensor anspricht steht in dessen Manual. Von Bosch Sensortec gibt es auf Github auch weitere Code Beispiele, leider nur von aktuellen Produkten. Am ähnlichsten zum BMP180 ist der BME280. Wenn das alles klappt, kannst du eigene Libs oder Funktionen einsetzen. Desweiteren sollte man halbwegs verstanden haben wie I2C funktioniert. Ich befürchte nämlich du drehst dich aktuell im Kreis. Wenn man zu viele Fehlerquellen hat, weiß man nicht wo man anfangen soll.

2 Likes

Das ist das ergebnis der Callibration Data mit einem Example code

AC1 : 6647
AC2 : -1173
AC3 : -14418
AC4 : -32302
AC5 : 25416
AC6 : 20885
B1 : 6515
B2 : 46
MB : -32768
MC : -11786
MD : 2567
Temp:
113
189
Press:
152
207
0


Altitude : 350.05 m
Pressure : 971.91 hPa
Temperature in Celsius : 23.03 C
Temperature in Fahrenheit : 73.46 F

nochmal zum vergleich mein Ergebnis

AC1 6647
AC2 -1173
AC3 -14418
AC4 -32302
AC5 25416
AC6 20885
B1 6515
B2 46
MB -32768
MC -11786
MD 2567
T-MSB: 42
LT-SB: 0
P-MSB: 48
P-LSB: 0
temp: 10752
pres: 12288

Das Auslesen der Callibration data scheint erstmal richtig zu sein.

Die Daten beim Temperatur und Druck messen stimmen allerdings garnicht das guck ich mir die Tage nochmal an.

@Doc_Arduino @agmue
Danke für die Idee mit dem Auslesen eines anderen Codes. Es hätte mir viel Rätselraten erspart, hätte ich das gleich so gemacht.^^