
Minimal working example to demonstrate I2C with SI7021 sensor
Dependencies: BLE_API mbed nRF51822
main.cpp@2:fd94a7e87ac5, 2016-12-10 (annotated)
- Committer:
- ghost22
- Date:
- Sat Dec 10 16:17:12 2016 +0000
- Revision:
- 2:fd94a7e87ac5
- Parent:
- 1:bca7ac3aedac
Fix of I2C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ghost22 | 0:d843c5528596 | 1 | /* |
ghost22 | 0:d843c5528596 | 2 | |
ghost22 | 0:d843c5528596 | 3 | Copyright (c) 2012-2014 RedBearLab |
ghost22 | 0:d843c5528596 | 4 | |
ghost22 | 0:d843c5528596 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ghost22 | 0:d843c5528596 | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
ghost22 | 0:d843c5528596 | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
ghost22 | 0:d843c5528596 | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
ghost22 | 0:d843c5528596 | 9 | subject to the following conditions: |
ghost22 | 0:d843c5528596 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
ghost22 | 0:d843c5528596 | 11 | |
ghost22 | 0:d843c5528596 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
ghost22 | 0:d843c5528596 | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
ghost22 | 0:d843c5528596 | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
ghost22 | 0:d843c5528596 | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
ghost22 | 0:d843c5528596 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ghost22 | 0:d843c5528596 | 17 | |
ghost22 | 0:d843c5528596 | 18 | */ |
ghost22 | 0:d843c5528596 | 19 | |
christoph |
1:bca7ac3aedac | 20 | #include <string.h> |
ghost22 | 0:d843c5528596 | 21 | #include "mbed.h" |
ghost22 | 0:d843c5528596 | 22 | #include "wire.h" |
ghost22 | 0:d843c5528596 | 23 | |
ghost22 | 0:d843c5528596 | 24 | #define SCL 7 |
ghost22 | 0:d843c5528596 | 25 | #define SDA 6 |
ghost22 | 0:d843c5528596 | 26 | |
christoph |
1:bca7ac3aedac | 27 | #define DEV_ADDR 0x80 |
ghost22 | 0:d843c5528596 | 28 | |
ghost22 | 0:d843c5528596 | 29 | TwoWire Wire = TwoWire(NRF_TWI0); |
ghost22 | 0:d843c5528596 | 30 | |
christoph |
1:bca7ac3aedac | 31 | Serial pc(USBTX, USBRX); |
ghost22 | 0:d843c5528596 | 32 | |
christoph |
1:bca7ac3aedac | 33 | uint16_t ReadHumidity(void); |
christoph |
1:bca7ac3aedac | 34 | int16_t ReadTemperature(void); |
ghost22 | 0:d843c5528596 | 35 | |
ghost22 | 0:d843c5528596 | 36 | int main(void) |
ghost22 | 0:d843c5528596 | 37 | { |
christoph |
1:bca7ac3aedac | 38 | uint16_t rh; |
christoph |
1:bca7ac3aedac | 39 | int16_t t; |
christoph |
1:bca7ac3aedac | 40 | char buf[200]; |
christoph |
1:bca7ac3aedac | 41 | |
ghost22 | 0:d843c5528596 | 42 | pc.baud(9600); |
ghost22 | 0:d843c5528596 | 43 | wait(5); |
christoph |
1:bca7ac3aedac | 44 | |
ghost22 | 0:d843c5528596 | 45 | Wire.begin(SCL, SDA, TWI_FREQUENCY_100K); |
christoph |
1:bca7ac3aedac | 46 | |
ghost22 | 0:d843c5528596 | 47 | while(1) |
ghost22 | 0:d843c5528596 | 48 | { |
christoph |
1:bca7ac3aedac | 49 | rh = ReadHumidity(); |
christoph |
1:bca7ac3aedac | 50 | t = ReadTemperature(); |
christoph |
1:bca7ac3aedac | 51 | |
christoph |
1:bca7ac3aedac | 52 | sprintf(buf,"RH=%d, T=%d\r\n\0",rh,t); |
christoph |
1:bca7ac3aedac | 53 | |
christoph |
1:bca7ac3aedac | 54 | pc.printf(buf); |
christoph |
1:bca7ac3aedac | 55 | wait(5); |
ghost22 | 0:d843c5528596 | 56 | } |
ghost22 | 0:d843c5528596 | 57 | } |
ghost22 | 0:d843c5528596 | 58 | |
ghost22 | 0:d843c5528596 | 59 | |
ghost22 | 0:d843c5528596 | 60 | |
ghost22 | 0:d843c5528596 | 61 | |
christoph |
1:bca7ac3aedac | 62 | uint16_t ReadHumidity(void) |
christoph |
1:bca7ac3aedac | 63 | { |
christoph |
1:bca7ac3aedac | 64 | uint16_t msbyte=0; |
christoph |
1:bca7ac3aedac | 65 | uint16_t lsbyte=0; |
christoph |
1:bca7ac3aedac | 66 | uint16_t checksum=0; |
christoph |
1:bca7ac3aedac | 67 | uint16_t rhcode=0; |
christoph |
1:bca7ac3aedac | 68 | |
christoph |
1:bca7ac3aedac | 69 | Wire.beginTransmission(DEV_ADDR); |
ghost22 | 2:fd94a7e87ac5 | 70 | |
ghost22 | 2:fd94a7e87ac5 | 71 | Wire.write(0xE5); |
ghost22 | 2:fd94a7e87ac5 | 72 | |
ghost22 | 2:fd94a7e87ac5 | 73 | Wire.endTransmission(0); |
ghost22 | 0:d843c5528596 | 74 | |
christoph |
1:bca7ac3aedac | 75 | Wire.requestFrom(DEV_ADDR+1, 3); |
christoph |
1:bca7ac3aedac | 76 | |
christoph |
1:bca7ac3aedac | 77 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 78 | { |
christoph |
1:bca7ac3aedac | 79 | msbyte = Wire.read(); |
christoph |
1:bca7ac3aedac | 80 | } |
christoph |
1:bca7ac3aedac | 81 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 82 | { |
christoph |
1:bca7ac3aedac | 83 | lsbyte = Wire.read(); |
christoph |
1:bca7ac3aedac | 84 | } |
christoph |
1:bca7ac3aedac | 85 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 86 | { |
christoph |
1:bca7ac3aedac | 87 | checksum = Wire.read(); |
christoph |
1:bca7ac3aedac | 88 | } |
christoph |
1:bca7ac3aedac | 89 | |
christoph |
1:bca7ac3aedac | 90 | rhcode = (((msbyte*256.0)+lsbyte)*125.0/65536.0-6.0)*100.0; |
christoph |
1:bca7ac3aedac | 91 | |
christoph |
1:bca7ac3aedac | 92 | if(rhcode<=0) |
christoph |
1:bca7ac3aedac | 93 | { |
christoph |
1:bca7ac3aedac | 94 | rhcode=0; |
christoph |
1:bca7ac3aedac | 95 | } |
christoph |
1:bca7ac3aedac | 96 | if(rhcode>=100*100) |
christoph |
1:bca7ac3aedac | 97 | { |
christoph |
1:bca7ac3aedac | 98 | rhcode=100*100; |
christoph |
1:bca7ac3aedac | 99 | } |
christoph |
1:bca7ac3aedac | 100 | return rhcode; |
christoph |
1:bca7ac3aedac | 101 | } |
christoph |
1:bca7ac3aedac | 102 | int16_t ReadTemperature(void) |
christoph |
1:bca7ac3aedac | 103 | { |
christoph |
1:bca7ac3aedac | 104 | int16_t msbyte=0; |
christoph |
1:bca7ac3aedac | 105 | int16_t lsbyte=0; |
christoph |
1:bca7ac3aedac | 106 | int16_t checksum=0; |
christoph |
1:bca7ac3aedac | 107 | |
christoph |
1:bca7ac3aedac | 108 | Wire.beginTransmission(DEV_ADDR); |
ghost22 | 2:fd94a7e87ac5 | 109 | |
ghost22 | 2:fd94a7e87ac5 | 110 | Wire.write(0xE3); |
ghost22 | 2:fd94a7e87ac5 | 111 | |
ghost22 | 2:fd94a7e87ac5 | 112 | Wire.endTransmission(0); |
ghost22 | 0:d843c5528596 | 113 | |
christoph |
1:bca7ac3aedac | 114 | Wire.requestFrom(DEV_ADDR+1, 3); |
christoph |
1:bca7ac3aedac | 115 | |
christoph |
1:bca7ac3aedac | 116 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 117 | { |
christoph |
1:bca7ac3aedac | 118 | msbyte = Wire.read(); |
christoph |
1:bca7ac3aedac | 119 | } |
christoph |
1:bca7ac3aedac | 120 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 121 | { |
christoph |
1:bca7ac3aedac | 122 | lsbyte = Wire.read(); |
christoph |
1:bca7ac3aedac | 123 | } |
christoph |
1:bca7ac3aedac | 124 | if(Wire.available()) |
christoph |
1:bca7ac3aedac | 125 | { |
christoph |
1:bca7ac3aedac | 126 | checksum = Wire.read(); |
christoph |
1:bca7ac3aedac | 127 | } |
christoph |
1:bca7ac3aedac | 128 | |
christoph |
1:bca7ac3aedac | 129 | return (((msbyte*256.0)+lsbyte)*175.72/65536.0-46.85)*100.0; |
christoph |
1:bca7ac3aedac | 130 | } |
ghost22 | 0:d843c5528596 | 131 | |
ghost22 | 0:d843c5528596 | 132 |