-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadt7410.c
229 lines (214 loc) · 4.85 KB
/
adt7410.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* adt7410.c
*
* Created on: Oct 28, 2019
* Author: roessing
*/
#include "pAM-definitions.h"
#include "stat.h"
#include "wait.h"
#include <adt7410.h>
#include <iostructures.h>
#include <msp430x16x.h>
/*!
* @file controls the ADT7410 temperature sensor. The sensor utilizes I2C for cummunication, the protocolls are implemented here.
* For details refer to the manual.
*/
/*!
* setup the ports for the sensor
*/
void temperature_sensor_setup()
{
/*Temprature sensor setup*/
TEMP_PORT_dir set_as_output SCL;
TEMP_PORT_out set_high SCL;
TEMP_PORT_dir set_as_output SDA;
TEMP_PORT_out set_high SCL;
}
/*!
* set SCL state
* @param status 1 for high, 0 for low
*/
void CLOCK(char status)
{
do {
if (status) {
TEMP_PORT_out set_high SCL;
} else {
TEMP_PORT_out set_low SCL;
}
} while (0);
}
/*!
* Used to transfer data to the ADT
* @param byte data to be transfered
*/
void clock_out(char byte)
{
unsigned int mask = 1 << (7);
char bit;
TEMP_PORT_dir set_as_output SDA;
for (int i = 0; i < 8; i++) {
bit = !!(byte & mask);
CLOCK(0);
if (bit) {
TEMP_PORT_out set_high SDA;
} else {
TEMP_PORT_out set_low SDA;
}
CLOCK(1);
mask >>= 1;
}
}
/*!
* used to read data from the ADT
* @return a char representing the message received
*/
char clock_in()
{
TEMP_PORT_dir set_as_input SDA;
char data = 0;
char mask = 1 << (7);
for(int i = 0; i < 8; i++) {
CLOCK(0);
//wait(1);
CLOCK(1);
data += (TEMP_PORT_in & SDA) ? mask : 0;
mask >>= 1;
}
return data;
}
/*!
* checks if the ADT acknowledged the data transmission.
* @return
*/
int check_ack()
{
volatile int ack = 0;
CLOCK(0);
TEMP_PORT_dir set_as_input SDA;
//wait(1);
CLOCK(1);
ack = TEMP_PORT_in & SDA;
return ack;
}
/*!
* Utilizes clock_out() to transfer data to the ADT
* @param reg register to write to
* @param message message to write to reg
* @param length length of the message in bytes
* @return status of the write
* @retval 0 no success
* @retval 1 successfull
*/
int temperature_sensor_write(char reg, int message, int length)
{
unsigned char address = 0x90;
unsigned char write = 0;
char byte;
//sending start signal
TEMP_PORT_out set_low SDA;
//sending data
clock_out(address + write);
if (check_ack())
return 0;
TEMP_PORT_out set_low SDA;
clock_out(reg);
if (check_ack())
return 0;
TEMP_PORT_out set_low SDA;
for (int i = 1; i <= length; i++) {
byte = message >> (length - 1) * 8;
clock_out(byte);
if (check_ack())
return 0;
TEMP_PORT_out set_low SDA;
}
//sending stop signal
TEMP_PORT_dir set_as_output SDA;
CLOCK(0);
//wait(1);
CLOCK(1);
TEMP_PORT_out set_high SDA;
return 1;
}
/*!
* reads data from a register of the ADT
* @param reg register to read from
* @param length how many bytes to read
* @return status of the write
* @retval 0 no success
* @retval 1 successfull
*/
int temperature_sensor_read(int reg, int length)
{
int value = 0;
unsigned char address = 0x90;
const char read = 0x01;
const char write = 0x00;
//send start signal
TEMP_PORT_out set_low SDA;
//send address
clock_out(address + write);
if (check_ack())
return 0;
clock_out(reg);
if (check_ack())
return 0;
//send start signal again
CLOCK(0);
TEMP_PORT_out set_high SDA;
CLOCK(1);
TEMP_PORT_out set_low SDA;
clock_out(address + read);
if (check_ack())
return 0;
TEMP_PORT_out set_low SDA;
for (int i = 1; i <= length; i++) {
value += clock_in();
if (i < length) {
value = value << 8;
CLOCK(0);
TEMP_PORT_out set_low SDA;
TEMP_PORT_dir set_as_output SDA;
CLOCK(1);
CLOCK(0);
} else {
//no ack by master
TEMP_PORT_dir set_as_output SDA;
CLOCK(0);
TEMP_PORT_out set_high SDA;
CLOCK(1);
//wait(1);
CLOCK(0);
//send stop
TEMP_PORT_out set_low SDA;
CLOCK(1);
TEMP_PORT_out set_high SDA;
}
}
return value;
}
void setup_temperature_sensor()
{
temperature_sensor_write(0x03, 0xA0, 1);
}
/*!
* Used to read a temperature value
* @return the temperature measured as a float
*/
stat_t get_temperature()
{
volatile stat_t temperature;
int temp;
temperature_sensor_write(0x03, 0xA0, 1);
wait(250);
if (!(temp = temperature_sensor_read(0x00, 2)))
return 255;
if (temp > 0) {
temperature = temp / 128.;
} else {
temperature = (temp - 65536.) / 128.;
}
return temperature;
}