Pages: [1]   Go Down
Author Topic: MS5611 Höhenwerte mitteln  (Read 1018 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hallo
ich habe mir mit dem MS5611 Sensor und einen Arduino Mini ein Barometer und Höhenmesser gebaut.
Habe einen sketch von _LEO_ benutzt, der auch perfect funktioniert. Siehe hier http://arduino.cc/forum/index.php/topic,103377.0.html

Da der Sensor leider nicht stabile Druckwerte ausgibt, wurden Teile vom Fabio´s sketch eingefügt um die Anzeige des Ortsdruckes
zu stabilisieren. Die Werte des Sensors werden in einen Array gespeichert und gemittelt wieder ausgegeben.

Leider schwankt jetzt die Ausgabe des Höhenwert da ich es nicht geschafft habe, die Ausgabe des Höhenwert mit einen zweiten Array
zu mitteln.

Hier mein sketch:
Code:
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(9, 8, 7, 6, 5, 4);

#define ADDRESS 0x77 //0x76
#define MOVAVG_SIZE 32

float movavg_buff[MOVAVG_SIZE];
int movavg_i=0;

uint32_t D1 = 0;
uint32_t D2 = 0;
int64_t dT = 0;
int32_t TEMP = 0;
int64_t OFF = 0;
int64_t SENS = 0;
int32_t P = 0;
uint16_t C[7];

float Temperature;
float Pressure;
const float sea_press = 1013.25;

void setup() {
  // Disable internal pullups, 10Kohms are on the breakout
  PORTC |= (1 << 4);
  PORTC |= (1 << 5);

  Wire.begin();
  Serial.begin(9600); //9600 changed 'cos of timing?
  delay(100);
  initial(ADDRESS);
  //populate movavg_buff before starting loop
  for(int i=0; i<MOVAVG_SIZE; i++) {
   movavg_buff[i];// = Pressure;
  }
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  {
    lcd.command(0x01);//clear display lcd.command (0x01) / / clear display
    delay(02);
    lcd.command(0x24);//function set RE=1 lcd.command (0x24) / / function set RE = 1
    delay(50);
    lcd.command(0x09);//extendet function set, 4 lines, 5-dot fontwith
    delay(50);
    lcd.command(0x20);//function set RE=0 lcd.command (0x20) / / function set RE = 0
    delay(50);
    lcd.command(0x0C);//control, display on, cursor off, blinken off
    delay(50);
    lcd.command(0x01);//clear display lcd.command (0x01) / / clear display
    delay(20);
    lcd.command(0x06);//entry mode segment bidirectional

  }
}

void loop()
{
  D1 = getVal(ADDRESS, 0x48); // Pressure raw
  D2 = getVal(ADDRESS, 0x58);// Temperature raw

  dT   = D2 - ((uint64_t)C[5] << 8);
  OFF  = ((int64_t)C[2] << 16) + ((dT * C[4]) >> 7);
  SENS = ((int32_t)C[1] << 15) + ((dT * C[3]) >> 8);

  TEMP = (int64_t)dT * (int64_t)C[6] / 8388608 + 2000;

  Temperature = (float)TEMP / 100;

  P  = ((int64_t)D1 * SENS / 2097152 - OFF) / 32768;

  Pressure = (float)P / 100;

  Serial.print("     Actual TEMP= ");
  Serial.print(Temperature);
  Serial.print("      Actual PRESSURE= ");
  pushAvg(Pressure);
  Serial.print(getAvg(movavg_buff, MOVAVG_SIZE));
  Serial.println();
  Serial.println();
 
  Serial.print("     Actual ALTITUDE= ");
  Serial.print(getAltitude(Pressure, Temperature));
  Serial.println("m");
  Serial.println(); 
  Serial.print(" RAW Temp D2=  ");
  Serial.print(D2);
  Serial.print(" RAW Pressure D1=  ");
  Serial.println(D1);
  Serial.println();
  lcd.setCursor(0, 0);
  lcd.print("J");
  lcd.setCursor(1, 0);
  lcd.write(0x7E);
  lcd.setCursor(2, 0);
  lcd.print("rgen Schmidt");
  lcd.setCursor(0, 1);
  lcd.print("Temperatur:");
  lcd.setCursor(12, 1);
  lcd.print(Temperature);
  lcd.setCursor(18, 1);
  lcd.write(0xDC);
  lcd.print("C");
  lcd.setCursor(0, 2);
  lcd.print("Druck hPa:");
  lcd.setCursor(10, 2);
  if ((Pressure)<1000)
  {
    lcd.print(" ");
  }
  lcd.print(getAvg(movavg_buff, MOVAVG_SIZE));
  lcd.setCursor(0, 3);
  lcd.print("H");
  lcd.setCursor(1, 3);
  lcd.write(0x7C);
  lcd.setCursor(2, 3);
  lcd.print("he:");
  lcd.setCursor(7, 3);
  if ((getAltitude(Pressure, Temperature))<10000);
  {
  lcd.print(" ");
  } 
  if ((getAltitude(Pressure, Temperature))<1000);
  {
  lcd.print(" ");
  } 
  if ((getAltitude(Pressure, Temperature))<100);
  {
  lcd.print(" ");
  }
  if ((getAltitude(Pressure, Temperature))<10);
  {
  lcd.print(" ");
  }
  lcd.print(getAltitude(Pressure, Temperature));
  lcd.setCursor(18, 3);
  lcd.print("m");
  //  Serial.print(" dT=  ");
  //  Serial.println(dT); can't print int64_t size values
  //Serial.println();
  //Serial.print(" C1 = ");
  //Serial.println(C[1]);
  //Serial.print(" C2 = ");
  //Serial.println(C[2]);
  //Serial.print(" C3 = ");
  //Serial.println(C[3]);
  //Serial.print(" C4 = ");
  //Serial.print(" C5 = ");
  //Serial.println(C[5]);
  //Serial.print(" C6 = ");
  //Serial.println(C[6]);
  //  Serial.print(" C7 = ");
  //  Serial.println(C[7]);
  //Serial.println();
 
}
float getAltitude(float Pressure, float Temperature) {
  return ((pow((sea_press / Pressure), 1/5.257) - 1.0) * (Temperature + 273.15)) / 0.0065;
}
void pushAvg (float val) {
  movavg_buff[movavg_i] = val;
  movavg_i = (movavg_i + 1) % MOVAVG_SIZE;


float getAvg(float * buff,int size) {
  float sum = 0.0;
  for(int i=0; i<size; i++) {
    sum += buff[i];
  }
  return sum /size;
}

long getVal(int address, byte code)
{
  unsigned long ret = 0;
  Wire.beginTransmission(address);
  Wire.write(code);
  Wire.endTransmission();
  delay(10);
  // start read sequence
  Wire.beginTransmission(address);
  Wire.write((byte) 0x00);
  Wire.endTransmission();
  Wire.beginTransmission(address);
  Wire.requestFrom(address, (int)3);
  if (Wire.available() >= 3)
  {
    ret = Wire.read() * (unsigned long)65536 + Wire.read() * (unsigned long)256 + Wire.read();
  }
  else {
    ret = -1;
  }
  Wire.endTransmission();
  return ret;
}

void initial(uint8_t address)
{

  Serial.println();
  Serial.println("PROM COEFFICIENTS ivan");

  Wire.beginTransmission(address);
  Wire.write(0x1E); // reset
  Wire.endTransmission();
  delay(10);


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

    Wire.beginTransmission(address);
    Wire.write(0xA2 + (i * 2));
    Wire.endTransmission();

    Wire.beginTransmission(address);
    Wire.requestFrom(address, (uint8_t) 6);
    delay(1);
    if(Wire.available())
    {
      C[i+1] = Wire.read() << 8 | Wire.read();
    }
    else {
      Serial.println("Error reading PROM 1"); // error reading the PROM or communicating with the device
    }
    Serial.println(C[i+1]);
  }
  Serial.println();
}

Könnte mir vielleicht jemand helfen

Danke im Voraus

Jürgen
Logged

Pages: [1]   Go Up
Jump to: