Sequential operation of two BMP180 (pressure and temperature) sensors

Нет ответов
RusUK
Offline
Зарегистрирован: 08.12.2016

Dobrogo vam vremeni sutok 

Ochen nuzhna pomosh, uchus v univere UK, dali zadanie na temu parallenoi raboty dvuh sensorov BMP180 s ispolsovaniem ISR (Interrupt Service Routine). Ne mogu razobratsya s primeneniem ISR, vot code BMP180, no s "delay".

#include <MyWireLib.h>
#include <avr/interrupt.h>
#include <avr/io.h>
MyWireLib Sens[3];
#define average temperature
#define PRELOAD 0xFF06 
 
 
//vars
int16_t  ac1[3], ac2[3], ac3[3], b1[3], b2[3], mb[3], mc[3], md[3]; // Store sensor PROM values from BMP180
uint16_t ac4[3], ac5[3], ac6[3];                     // Store sensor PROM values from BMP180
const uint8_t oss = 3;                      // Set oversampling setting
const uint8_t osd = 26;                     // with corresponding oversampling delay 
float T[3], P[3];                                 // Set global variables for temperature and pressure 
unsigned int incomingByte = 0;  // for incoming serial data
unsigned int count = 0; // counting interrupts
unsigned int timestamp = 0; // counting seconds
bool lock, enable;
 
void setup() 
Serial.begin(9600);
//declare pins
Sens[0].SCLpinI=13;
Sens[0].SDApinI=12;
Sens[1].SCLpinI=11;
Sens[1].SDApinI=10;
Sens[0].Soss=oss;
Sens[1].Soss=oss;
 
 
 
//> for test
Serial.print("SCLPIN for sensor 0: "); Serial.println(Sens[0].SCLpinI);
Serial.print("SDAPIN for sensor 0: ");Serial.println(Sens[0].SDApinI);
Serial.print("SCLPIN for sensor 1: ");Serial.println(Sens[1].SCLpinI);
Serial.print("SDAPIN for sensor 1: ");Serial.println(Sens[1].SDApinI);
//< for test
 delay(500);
 
 Sens[0].InitWire();
 Sens[1].InitWire();
 
 
 init_SENSOR(0); 
 init_SENSOR(1); 
 
 
}
 
void loop() 
 
 int32_t b5[3];
 
   for(int eachsens = 0; eachsens < 2 ; eachsens++)
   {
       b5[eachsens]= temperature(eachsens);             // Read and calculate temperature (T) 
       P[eachsens] = pressure(b5[eachsens],eachsens);   // Read and calculate pressure (P) 
       
       Serial.print("Sensor ");
       Serial.print(eachsens);
       Serial.print(" => ");
       Serial.print("Temperature: ");
       Serial.print(T[eachsens], 3);
       Serial.print(" C");
       Serial.print(" | Pressure: ");
       Serial.print(P[eachsens], 3);
       Serial.println(" mbar");
       
   }
    delay(200);                               // Delay between each readout
}
 
void init_SENSOR(int sensnr)
{
  ac1[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAA);
  ac2[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAC);
  ac3[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAE);
  ac4[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB0);
  ac5[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB2);
  ac6[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB4);
  b1[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xB6);
  b2[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xB8);
  mb[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBA);
  mc[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBC);
  md[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBE);
 
  Serial.println("");
  Serial.print("Sensor ");  Serial.print(sensnr);  Serial.println(" calibration data:");
  Serial.print(F("AC1 = ")); Serial.println(ac1[sensnr]);
  Serial.print(F("AC2 = ")); Serial.println(ac2[sensnr]);
  Serial.print(F("AC3 = ")); Serial.println(ac3[sensnr]);
  Serial.print(F("AC4 = ")); Serial.println(ac4[sensnr]);
  Serial.print(F("AC5 = ")); Serial.println(ac5[sensnr]);
  Serial.print(F("AC6 = ")); Serial.println(ac6[sensnr]);
  Serial.print(F("B1 = "));  Serial.println(b1[sensnr]);
  Serial.print(F("B2 = "));  Serial.println(b2[sensnr]);
  Serial.print(F("MB = "));  Serial.println(mb[sensnr]);
  Serial.print(F("MC = "));  Serial.println(mc[sensnr]);
  Serial.print(F("MD = "));  Serial.println(md[sensnr]);
  Serial.println("");
}
 
/**********************************************
  Calcualte pressure readings
 **********************************************/
float pressure(int32_t b5, int sensnr)
{
  int32_t x1, x2, x3, b3, b6, p, UP;
  uint32_t b4, b7; 
  
  UP = read_pressure(sensnr);  // Read raw pressure
 
  b6 = b5 - 4000;
  x1 = (b2[sensnr] * (b6 * b6 >> 12)) >> 11; 
  x2 = ac2[sensnr] * b6 >> 11;
  x3 = x1 + x2;
  b3 = (((ac1[sensnr] * 4 + x3) << oss) + 2) >> 2;
  x1 = ac3[sensnr] * b6 >> 13;
  x2 = (b1[sensnr] * (b6 * b6 >> 12)) >> 16;
  x3 = ((x1 + x2) + 2) >> 2;
  b4 = (ac4[sensnr] * (uint32_t)(x3 + 32768)) >> 15;
  b7 = ((uint32_t)UP - b3) * (50000 >> oss);
  if(b7 < 0x80000000) { p = (b7 << 1) / b4; } else { p = (b7 / b4) << 1; } // or p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
  x1 = (p >> 8) * (p >> 8);
  x1 = (x1 * 3038) >> 16;
  x2 = (-7357 * p) >> 16;
  return (p + ((x1 + x2 + 3791) >> 4)) / 100.0f; // Return pressure in mbar
}
 
/**********************************************
  Read uncompensated temperature
 **********************************************/
int32_t temperature(int sensnr)
{
  int32_t x1, x2, b5, UT;
  byte tobesend[2] = {0xf4, 0x2e};
  Sens[sensnr].sendbytes(tobesend,2);
  
  delay(5);
  UT = Sens[sensnr].Get16bitFromRegister(0xf6);
 
  // Calculate true temperature
  x1 = (UT - (int32_t)ac6[sensnr]) * (int32_t)ac5[sensnr] >> 15;
  x2 = ((int32_t)mc[sensnr] << 11) / (x1 + (int32_t)md[sensnr]);
  b5 = x1 + x2;
  T[sensnr]  = (b5 + 8) >> 4;
  T[sensnr] = T[sensnr] / 10.0;                           // Temperature in celsius 
  return b5;  
}
/**********************************************
  Read uncompensated pressure value
 **********************************************/
int32_t read_pressure(int sensnr)
{
  int32_t value; 
  byte tobesend[2] = {0xf4, (0x34 + (oss << 6))};
 
  Sens[sensnr].sendbytes(tobesend,2);
  delay(osd);
  
  value= Sens[sensnr].Get24bitFromRegister(0xf6);
  return (value); // Return value
}
 
Spasibo za vashe vnimanie, budu rad luboi pomoshi!)