Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.29 KB | None | 0 0
  1.  
  2. #include <MyWireLib.h>
  3. MyWireLib Sens[2];
  4.  
  5. #define PRELOAD 0xFF06 // = 65536 - 250 decimal gives is 1000 interrupts per second
  6. #define S0 0
  7. #define S1 1
  8.  
  9. //vars
  10. int16_t  ac1[2], ac2[2], ac3[2], b1[2], b2[2], mb[2], mc[2], md[2]; // Store sensor PROM values from BMP180
  11. uint16_t ac4[2], ac5[2], ac6[2];                     // Store sensor PROM values from BMP180
  12. const uint8_t oss = 3;                      // Set oversampling setting
  13. const uint8_t osd = 26;                     // with corresponding oversampling delay
  14. float T[2], P[2];                                 // Set global variables for temperature and pressure
  15. unsigned int count = 0; // counting interrupts
  16. unsigned int h,m,oldsec, sec = 0; // counting seconds
  17. bool lock, enable;
  18. int state1 = S0,state2 = S0; // or whatever is the initial state.
  19.  
  20.  
  21.  
  22. void setup()
  23. {
  24. Serial.begin(9600);
  25. //declare pins
  26. Sens[0].SCLpinI=2;
  27. Sens[0].SDApinI=3;
  28. Sens[1].SCLpinI=4;
  29. Sens[1].SDApinI=5;
  30. Sens[0].Soss=oss;
  31. Sens[1].Soss=oss;
  32.  
  33. //> for test
  34. Serial.print("SCLPIN for sensor 0: "); Serial.println(Sens[0].SCLpinI);
  35. Serial.print("SDAPIN for sensor 0: ");Serial.println(Sens[0].SDApinI);
  36. Serial.print("SCLPIN for sensor 1: ");Serial.println(Sens[1].SCLpinI);
  37. Serial.print("SDAPIN for sensor 1: ");Serial.println(Sens[1].SDApinI);
  38. //< for test
  39.  delay(500);
  40.  
  41.  Sens[0].InitWire();
  42.  Sens[1].InitWire();
  43.  
  44.  init_SENSOR(0);
  45.  init_SENSOR(1);
  46.  
  47. }
  48.  
  49. void loop()
  50. {
  51.  
  52.  int32_t b5[2];
  53.  
  54.    for(int eachsens = 0; eachsens < 2 ; eachsens++)
  55.    {
  56.        b5[eachsens]= temperature(eachsens);             // Read and calculate temperature (T)
  57.        P[eachsens] = pressure(b5[eachsens],eachsens);   // Read and calculate pressure (P)
  58.        
  59.        Serial.print("Sensor ");
  60.        Serial.print(eachsens);
  61.        Serial.print(" => ");
  62.        Serial.print("Temperature: ");
  63.        Serial.print(T[eachsens], 2);
  64.        Serial.print(" C");
  65.        Serial.print(" | Pressure: ");
  66.        Serial.print(P[eachsens], 2);
  67.        Serial.println(" mbar");
  68.        
  69.    }
  70.     delay(200);                               // Delay between each readout
  71. }
  72.  
  73. void init_SENSOR(int sensnr)
  74. {
  75.   ac1[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAA);
  76.   ac2[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAC);
  77.   ac3[sensnr] = Sens[sensnr].Get16bitFromRegister(0xAE);
  78.   ac4[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB0);
  79.   ac5[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB2);
  80.   ac6[sensnr] = Sens[sensnr].Get16bitFromRegister(0xB4);
  81.   b1[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xB6);
  82.   b2[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xB8);
  83.   mb[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBA);
  84.   mc[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBC);
  85.   md[sensnr]  = Sens[sensnr].Get16bitFromRegister(0xBE);
  86.  
  87.   Serial.println("");
  88.   Serial.print("Sensor ");  Serial.print(sensnr);  Serial.println(" calibration data:");
  89.   Serial.print(F("AC1 = ")); Serial.println(ac1[sensnr]);
  90.   Serial.print(F("AC2 = ")); Serial.println(ac2[sensnr]);
  91.   Serial.print(F("AC3 = ")); Serial.println(ac3[sensnr]);
  92.   Serial.print(F("AC4 = ")); Serial.println(ac4[sensnr]);
  93.   Serial.print(F("AC5 = ")); Serial.println(ac5[sensnr]);
  94.   Serial.print(F("AC6 = ")); Serial.println(ac6[sensnr]);
  95.   Serial.print(F("B1 = "));  Serial.println(b1[sensnr]);
  96.   Serial.print(F("B2 = "));  Serial.println(b2[sensnr]);
  97.   Serial.print(F("MB = "));  Serial.println(mb[sensnr]);
  98.   Serial.print(F("MC = "));  Serial.println(mc[sensnr]);
  99.   Serial.print(F("MD = "));  Serial.println(md[sensnr]);
  100.   Serial.println("");
  101. }
  102.  
  103. /**********************************************
  104.   Calcualte pressure readings
  105.  **********************************************/
  106. float pressure(int32_t b5, int sensnr)
  107. {
  108.   int32_t x1, x2, x3, b3, b6, p, UP;
  109.   uint32_t b4, b7;
  110.  
  111.   UP = read_pressure(sensnr);  // Read raw pressure
  112.  
  113.   b6 = b5 - 4000;
  114.   x1 = (b2[sensnr] * (b6 * b6 >> 12)) >> 11;
  115.   x2 = ac2[sensnr] * b6 >> 11;
  116.   x3 = x1 + x2;
  117.   b3 = (((ac1[sensnr] * 4 + x3) << oss) + 2) >> 2;
  118.   x1 = ac3[sensnr] * b6 >> 13;
  119.   x2 = (b1[sensnr] * (b6 * b6 >> 12)) >> 16;
  120.   x3 = ((x1 + x2) + 2) >> 2;
  121.   b4 = (ac4[sensnr] * (uint32_t)(x3 + 32768)) >> 15;
  122.   b7 = ((uint32_t)UP - b3) * (50000 >> oss);
  123.   if(b7 < 0x80000000) { p = (b7 << 1) / b4; } else { p = (b7 / b4) << 1; } // or p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
  124.   x1 = (p >> 8) * (p >> 8);
  125.   x1 = (x1 * 3038) >> 16;
  126.   x2 = (-7357 * p) >> 16;
  127.   return (p + ((x1 + x2 + 3791) >> 4)) / 100.0f; // Return pressure in mbar
  128. }
  129.  
  130. /**********************************************
  131.   Read uncompensated temperature
  132.  **********************************************/
  133. int32_t temperature(int sensnr)
  134. {
  135.   int32_t x1, x2, b5, UT;
  136.   byte tobesend[2] = {0xf4, 0x2e};
  137.   Sens[sensnr].sendbytes(tobesend,2);
  138.  
  139.   delay(5);
  140.   UT = Sens[sensnr].Get16bitFromRegister(0xf6);
  141.  
  142.   // Calculate true temperature
  143.   x1 = (UT - (int32_t)ac6[sensnr]) * (int32_t)ac5[sensnr] >> 15;
  144.   x2 = ((int32_t)mc[sensnr] << 11) / (x1 + (int32_t)md[sensnr]);
  145.   b5 = x1 + x2;
  146.   T[sensnr]  = (b5 + 8) >> 4;
  147.   T[sensnr] = T[sensnr] / 10.0;                           // Temperature in celsius
  148.   return b5;  
  149. }
  150. /**********************************************
  151.   Read uncompensated pressure value
  152.  **********************************************/
  153. int32_t read_pressure(int sensnr)
  154. {
  155.   int32_t value;
  156.   byte tobesend[2] = {0xf4, (0x34 + (oss << 6))};
  157.  
  158.   Sens[sensnr].sendbytes(tobesend,2);
  159.   delay(osd);
  160.  
  161.   value= Sens[sensnr].Get24bitFromRegister(0xf6);
  162.   return (value); // Return value
  163.  
  164.   // initialize Timer1
  165. cli();         // disable global interrupts
  166. TCCR1A = 0;    // set entire TCCR1A register to 0
  167. TCCR1B = 0;    // set entire TCCR1B register to 0
  168.                // (as we do not know the initial  values)
  169.  
  170. // enable Timer1 overflow interrupt:
  171. TIMSK1 |= (1 << TOIE1); //Atmega8 has no TIMSK1 but a TIMSK register
  172. // Preload
  173. TCNT1= PRELOAD;
  174. TCCR1B |= (1 << CS11); // Sets bit CS11 in TCCR1B
  175. TCCR1B |= (1 << CS10); // and CS10
  176. //the clock source is divided by 64, i.e. one clock cycle every 64 / (16 * 10^6) = 4 * 10^(-6) = 0.000004s
  177.  
  178. // This is achieved by shifting binary 1 (0b00000001)
  179. // to the left by CS11 or CS10 bits. This is then bitwise
  180. // OR-ed into the current value of TCCR1B, which effectively set
  181. // this one bit high.
  182. // enable global interrupts:
  183. sei();
  184. Serial.begin(9600); // opens serial port, sets data rate to 9600 bps ready for debug.
  185. count = 0;
  186. h=m=sec = 0;oldsec = 10;
  187. lock=enable=true;
  188. }
  189.  
  190. // here is the ISR executed by the CPU when the corresponding interrupt occurs
  191.  
  192.  
  193. {
  194.   TCNT1=PRELOAD; // reload the timer preload
  195.   if (enable) count++;
  196.   if (count >= 1000 && lock) {
  197.     count -= 1000; sec++;
  198.   }
  199. }
  200.  
  201.  
  202.  
  203. void loop() {
  204.  switch (state1) {
  205.  S0:byte tobesend[2] = {0xf4, 0x2e}; Sens[sensnr].sendbytes(tobesend,2);
  206. timestamp = count; state1 = S1; break;
  207.  S1:if (timestamp + 5 <= count) { state1 = S3; break;}
  208. else { state1 = S1; break;}
  209.  S3:UT = Sens[sensnr].Get16bitFromRegister(0xf6);
  210. //calibration calculation
  211. state1 = S0; break;
  212.  }
  213. switch (state2) {
  214.  S0:byte tobesend[2] = {0xf4, 0x2e}; Sens[sensnr].sendbytes(tobesend,2);
  215. timestamp = count; state2 = S1; break;
  216.  S1:if (timestamp + 5 <= count) { state2 = S3; break;}
  217. else { state2 = S1; break;}
  218.  S3:UT = Sens[sensnr].Get16bitFromRegister(0xf6);
  219. //calibration calculation
  220. state2 = S0; break;
  221.  }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement