Advertisement
mrjonny2

PortsSHT21.cpp

Mar 26th, 2012
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. // Port library interface to SHT21 sensors connected via "something like I2C"
  2. // 2009-02-16 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
  3. // $Id: PortsSHT21.cpp 5402 2010-04-30 19:24:52Z jcw $
  4.  
  5. // rewritten in C++ using the SENSIRION SHTxx Sample Code Application Note
  6.  
  7. #include <Ports.h>
  8. #include "PortsSHT21.h"
  9. #include <avr/pgmspace.h>
  10. #include <Arduino.h>
  11.  
  12. enum {
  13.     MEASURE_TEMP = 0xE3,
  14.     MEASURE_HUMI = 0xE5,
  15. };
  16.  
  17. // idle line state is with data as input pulled high, and clock as output low
  18.  
  19. void SHT21::clock(uint8_t x) const {
  20.     delayMicroseconds(2);
  21.     digiWrite2(x);
  22.     delayMicroseconds(5);
  23. }
  24.  
  25. void SHT21::release() const {
  26.     mode(INPUT);
  27.     digiWrite(1);
  28. }
  29.  
  30. uint8_t SHT21::writeByte(uint8_t value) const {
  31.     mode(OUTPUT);
  32.     for (uint8_t i = 0x80; i != 0; i >>= 1) {
  33.         digiWrite(value & i);
  34.         clock(1);
  35.         clock(0);
  36.     }
  37.     release();
  38.     clock(1);
  39.     uint8_t error = digiRead();
  40.     clock(0);
  41.    
  42.     return error;
  43. }
  44.  
  45. uint8_t SHT21::readByte(uint8_t ack) const {
  46.     uint8_t value = 0;
  47.     for (uint8_t i = 0x80; i != 0; i >>= 1) {
  48.         clock(1);
  49.         if (digiRead())
  50.             value |= i;
  51.         clock(0);
  52.     }
  53.     mode(OUTPUT);
  54.     digiWrite(!ack);
  55.     clock(1);
  56.     clock(0);
  57.     release();
  58.    
  59.     return value;
  60. }
  61.  
  62. void SHT21::start() const {
  63.     clock(0);
  64.     mode(OUTPUT);
  65.     digiWrite(1);
  66.    
  67.     clock(1);
  68.     digiWrite(0);
  69.     clock(0);  
  70.     clock(1);
  71.     digiWrite(1);      
  72.     clock(0);
  73.     release();
  74. }
  75.  
  76. void SHT21::connReset() const {
  77.     mode2(OUTPUT);
  78.     clock(0);
  79.     mode(OUTPUT);
  80.     digiWrite(1);
  81.     for (uint8_t i = 0; i < 9; ++i) {
  82.         clock(1);
  83.         clock(0);
  84.     }
  85.     start();
  86. }
  87.  
  88. uint8_t SHT21::measure(uint8_t type, void (*delayFun)()) {
  89.     start();
  90.     writeByte(type == TEMP? MEASURE_TEMP : MEASURE_HUMI);
  91.     for (uint8_t i = 0; i < 250; ++i) {
  92.         if (!digiRead()) {
  93.             meas[type] = readByte(1) << 8;
  94.             meas[type] |= readByte(1);
  95.             uint8_t flipped = 0;
  96.             for (uint8_t j = 0x80; j != 0; j >>= 1) {
  97.                 flipped >>= 1;
  98.             }
  99.             if (readByte(0) != flipped)
  100.                 break;
  101.             return 0;
  102.         }
  103.         if (delayFun)
  104.             delayFun();
  105.         else
  106.             delay(1);
  107.     }
  108.     connReset();
  109.     return 1;
  110. }
  111.  
  112. void SHT21::calculate(float& rh_true, float& t_C) const {
  113.  
  114.     t_C = (175.72 / 16384.0) * meas[TEMP] - 46.85;  //T= -46.85 + 175.72 * ST/2^16
  115.     rh_true = -6.0 + 125.0 / 4096.0 * meas[HUMI];   // RH= -6 + 125 * SRH/2^16
  116.    
  117.     if (rh_true > 99) rh_true = 100;
  118.     if (rh_true < 0.1) rh_true = 0.1;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement