-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdc1080.c
111 lines (70 loc) · 2.61 KB
/
hdc1080.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Arduino Library for Texas Instruments HDC1080 Digital Humidity and Temperature Sensor
Written by AA for ClosedCube
---
The MIT License (MIT)
Copyright (c) 2016-2017 ClosedCube Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hdc1080.h"
#include "transfer_handler.h"
uint8_t hdc1080_conf_register;
static void hdc1080_write(uint8_t reg, uint16_t data)
{
uint8_t sendbuf[3] = {reg, (data >> 8), data};
iic_send(HDC1080_ADDR, sendbuf, 3, false);
}
static uint16_t hdc1080_read(uint8_t reg)
{
uint8_t recvbuf[2];
iic_send(HDC1080_ADDR, ®, 1, false);
delay(9);
iic_read(HDC1080_ADDR, recvbuf, 2);
return (recvbuf[0] << 8) | recvbuf[1];
}
uint16_t hdc1080_readManufacturerId(void){
return hdc1080_read(HDC1080_MANUFACTURER_ID);
}
uint16_t hdc1080_readDeviceId(void){
return hdc1080_read(HDC1080_DEVICE_ID);
}
uint16_t hdc1080_readSerialNumber(uint8_t snpos){
return hdc1080_read(snpos);
}
void hdc1080_begin(uint16_t resolution){
ret_code_t err_code;
hdc1080_write(HDC1080_CONFIGURATION, HDC1080_CONF_RESET);
delay(2);
hdc1080_conf_register = resolution & HDC1080_RESOLUTION_MASK;
hdc1080_write(HDC1080_CONFIGURATION, hdc1080_conf_register);
}
void hdc1080_heater(bool en) {
uint16_t sendbuf = hdc1080_conf_register;
if(en)
sendbuf |= HDC1080_CONF_HEAT;
else
sendbuf &= (~HDC1080_CONF_HEAT);
hdc1080_write(HDC1080_CONFIGURATION, sendbuf);
}
float hdc1080_readTemperature(void){
int16_t raw = hdc1080_read(HDC1080_TEMPERATURE);
return (raw / 65536.0f) * 165.0f - 40.0f;
}
float hdc1080_readHumidity(void){
int16_t raw = hdc1080_read(HDC1080_HUMIDITY);
return (raw / 65536.0f) * 100.0f;
}