Minimal working example to demonstrate I2C with SI7021 sensor

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 #include <string.h>
00021 #include "mbed.h"
00022 #include "wire.h"
00023 
00024 #define SCL         7  
00025 #define SDA         6
00026 
00027 #define DEV_ADDR    0x80
00028 
00029 TwoWire Wire = TwoWire(NRF_TWI0);
00030 
00031 Serial pc(USBTX, USBRX);
00032 
00033 uint16_t ReadHumidity(void);
00034 int16_t ReadTemperature(void);
00035 
00036 int main(void)
00037 {
00038     uint16_t rh;
00039     int16_t t;
00040     char buf[200];
00041     
00042     pc.baud(9600);
00043     wait(5);
00044     
00045     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
00046 
00047     while(1)
00048     {
00049         rh = ReadHumidity();
00050         t = ReadTemperature();
00051         
00052         sprintf(buf,"RH=%d, T=%d\r\n\0",rh,t);
00053         
00054         pc.printf(buf);
00055         wait(5);
00056     }
00057 }
00058 
00059 
00060 
00061 
00062 uint16_t ReadHumidity(void)
00063 {
00064     uint16_t msbyte=0;
00065     uint16_t lsbyte=0;
00066     uint16_t checksum=0;
00067     uint16_t rhcode=0;
00068         
00069     Wire.beginTransmission(DEV_ADDR);
00070 
00071     Wire.write(0xE5);
00072     
00073     Wire.endTransmission(0);
00074 
00075     Wire.requestFrom(DEV_ADDR+1, 3);
00076     
00077     if(Wire.available())
00078     {
00079         msbyte = Wire.read();
00080     }
00081     if(Wire.available())
00082     {
00083         lsbyte = Wire.read();
00084     }
00085     if(Wire.available())
00086     {
00087         checksum = Wire.read();
00088     }
00089     
00090     rhcode = (((msbyte*256.0)+lsbyte)*125.0/65536.0-6.0)*100.0;
00091     
00092     if(rhcode<=0)
00093     {
00094         rhcode=0;
00095     }
00096     if(rhcode>=100*100)
00097     {
00098         rhcode=100*100;
00099     }
00100     return rhcode;
00101 }
00102 int16_t ReadTemperature(void)
00103 {
00104     int16_t msbyte=0;
00105     int16_t lsbyte=0;
00106     int16_t checksum=0;
00107     
00108     Wire.beginTransmission(DEV_ADDR);
00109 
00110     Wire.write(0xE3);
00111     
00112     Wire.endTransmission(0);
00113 
00114     Wire.requestFrom(DEV_ADDR+1, 3);
00115     
00116     if(Wire.available())
00117     {
00118         msbyte = Wire.read();
00119     }
00120     if(Wire.available())
00121     {
00122         lsbyte = Wire.read();
00123     }
00124     if(Wire.available())
00125     {
00126         checksum = Wire.read();
00127     }
00128     
00129     return (((msbyte*256.0)+lsbyte)*175.72/65536.0-46.85)*100.0;
00130 }
00131 
00132