User | Revision | Line number | New contents of line |
gchaves |
0:7a9b45bd5f22
|
1
|
/*
|
gchaves |
0:7a9b45bd5f22
|
2
|
\file TMP175.h
|
gchaves |
0:7a9b45bd5f22
|
3
|
\version: 1.0
|
gchaves |
0:7a9b45bd5f22
|
4
|
|
gchaves |
0:7a9b45bd5f22
|
5
|
\brief Este fichero contiene funciones para control de TMP175/75
|
gchaves |
0:7a9b45bd5f22
|
6
|
|
gchaves |
0:7a9b45bd5f22
|
7
|
\web www.micros-designs.com.ar
|
gchaves |
0:7a9b45bd5f22
|
8
|
\date 02/02/11
|
gchaves |
0:7a9b45bd5f22
|
9
|
|
gchaves |
0:7a9b45bd5f22
|
10
|
*- Version Log --------------------------------------------------------------*
|
gchaves |
0:7a9b45bd5f22
|
11
|
* Fecha Autor Comentarios *
|
gchaves |
0:7a9b45bd5f22
|
12
|
*----------------------------------------------------------------------------*
|
gchaves |
0:7a9b45bd5f22
|
13
|
* 02/02/11 Suky Original *
|
gchaves |
0:7a9b45bd5f22
|
14
|
*----------------------------------------------------------------------------*/
|
gchaves |
0:7a9b45bd5f22
|
15
|
///////////////////////////////////////////////////////////////////////////
|
gchaves |
0:7a9b45bd5f22
|
16
|
//// ////
|
gchaves |
0:7a9b45bd5f22
|
17
|
//// ////
|
gchaves |
0:7a9b45bd5f22
|
18
|
//// (C) Copyright 2011 www.micros-designs.com.ar ////
|
gchaves |
0:7a9b45bd5f22
|
19
|
//// Este c�digo puede ser usado, modificado y distribuido libremente ////
|
gchaves |
0:7a9b45bd5f22
|
20
|
//// sin eliminar esta cabecera y sin garant�a de ning�n tipo. ////
|
gchaves |
0:7a9b45bd5f22
|
21
|
//// ////
|
gchaves |
0:7a9b45bd5f22
|
22
|
//// ////
|
gchaves |
0:7a9b45bd5f22
|
23
|
///////////////////////////////////////////////////////////////////////////
|
gchaves |
0:7a9b45bd5f22
|
24
|
#include "mbed.h"
|
gchaves |
0:7a9b45bd5f22
|
25
|
|
gchaves |
0:7a9b45bd5f22
|
26
|
|
gchaves |
0:7a9b45bd5f22
|
27
|
class TMP175{
|
gchaves |
0:7a9b45bd5f22
|
28
|
public:
|
gchaves |
0:7a9b45bd5f22
|
29
|
#define SHUTDOWN_MODE_OFF 0x00
|
gchaves |
0:7a9b45bd5f22
|
30
|
#define SHUTDOWN_MODE_ON 0x01
|
gchaves |
0:7a9b45bd5f22
|
31
|
#define COMPARATOR_MODE 0x00
|
gchaves |
0:7a9b45bd5f22
|
32
|
#define INTERRUPT_MODE 0x02
|
gchaves |
0:7a9b45bd5f22
|
33
|
#define POLARITY_0 0x00
|
gchaves |
0:7a9b45bd5f22
|
34
|
#define POLARITY_1 0x04
|
gchaves |
0:7a9b45bd5f22
|
35
|
#define FAULT_QUEUE_1 0x00
|
gchaves |
0:7a9b45bd5f22
|
36
|
#define FAULT_QUEUE_2 0x08
|
gchaves |
0:7a9b45bd5f22
|
37
|
#define FAULT_QUEUE_4 0x10
|
gchaves |
0:7a9b45bd5f22
|
38
|
#define FAULT_QUEUE_6 0x18
|
gchaves |
0:7a9b45bd5f22
|
39
|
#define RESOLUTION_9 0x00
|
gchaves |
0:7a9b45bd5f22
|
40
|
#define RESOLUTION_10 0x20
|
gchaves |
0:7a9b45bd5f22
|
41
|
#define RESOLUTION_11 0x40
|
gchaves |
0:7a9b45bd5f22
|
42
|
#define RESOLUTION_12 0x60
|
gchaves |
0:7a9b45bd5f22
|
43
|
#define ONE_SHOT 0x80
|
gchaves |
0:7a9b45bd5f22
|
44
|
TMP175(PinName PIN_SDA, PinName PIN_SCL, PinName PIN_ALERT=NC);
|
gchaves |
0:7a9b45bd5f22
|
45
|
void vSetConfigurationTMP175(unsigned char Config,unsigned char Address);
|
gchaves |
0:7a9b45bd5f22
|
46
|
void vStartSingleConversionTMP175(void);
|
gchaves |
0:7a9b45bd5f22
|
47
|
float fReadTemperatureTMP175(void);
|
gchaves |
0:7a9b45bd5f22
|
48
|
void vSetTemperatureLowTMP175(float Value);
|
gchaves |
0:7a9b45bd5f22
|
49
|
void vSetTemperatureHighTMP175(float Value);
|
gchaves |
0:7a9b45bd5f22
|
50
|
float fReadTemperatureHighTMP175(void);
|
gchaves |
0:7a9b45bd5f22
|
51
|
float fReadTemperatureLowTMP175(void);
|
gchaves |
0:7a9b45bd5f22
|
52
|
bool bReadPinAlertTMP175(void);
|
gchaves |
0:7a9b45bd5f22
|
53
|
#ifdef MBED_OPERATORS
|
gchaves |
0:7a9b45bd5f22
|
54
|
operator float();
|
gchaves |
0:7a9b45bd5f22
|
55
|
#endif
|
gchaves |
0:7a9b45bd5f22
|
56
|
protected:
|
gchaves |
0:7a9b45bd5f22
|
57
|
unsigned char _Address, Rotar;
|
gchaves |
0:7a9b45bd5f22
|
58
|
float Factor;
|
gchaves |
0:7a9b45bd5f22
|
59
|
unsigned short Mascara;
|
gchaves |
0:7a9b45bd5f22
|
60
|
I2C Bus;
|
gchaves |
0:7a9b45bd5f22
|
61
|
DigitalIn _PIN_ALERT;
|
gchaves |
0:7a9b45bd5f22
|
62
|
};
|
gchaves |
0:7a9b45bd5f22
|
63
|
|
gchaves |
0:7a9b45bd5f22
|
64
|
TMP175::TMP175(PinName PIN_SDA, PinName PIN_SCL, PinName PIN_ALERT)
|
gchaves |
0:7a9b45bd5f22
|
65
|
:Bus(PIN_SDA,PIN_SCL),_PIN_ALERT(PIN_ALERT){
|
gchaves |
0:7a9b45bd5f22
|
66
|
}
|
gchaves |
0:7a9b45bd5f22
|
67
|
|
gchaves |
0:7a9b45bd5f22
|
68
|
void TMP175::vSetConfigurationTMP175(unsigned char Config,unsigned char Address){
|
gchaves |
0:7a9b45bd5f22
|
69
|
|
gchaves |
0:7a9b45bd5f22
|
70
|
_Address=Address<<1;
|
gchaves |
0:7a9b45bd5f22
|
71
|
switch(Config&0x60){
|
gchaves |
0:7a9b45bd5f22
|
72
|
case 0x00:
|
gchaves |
0:7a9b45bd5f22
|
73
|
Factor=0.5;
|
gchaves |
0:7a9b45bd5f22
|
74
|
Mascara=0x00FF;
|
gchaves |
0:7a9b45bd5f22
|
75
|
Rotar=3;
|
gchaves |
0:7a9b45bd5f22
|
76
|
break;
|
gchaves |
0:7a9b45bd5f22
|
77
|
case 0x20:
|
gchaves |
0:7a9b45bd5f22
|
78
|
Factor=0.25;
|
gchaves |
0:7a9b45bd5f22
|
79
|
Mascara=0x01FF;
|
gchaves |
0:7a9b45bd5f22
|
80
|
Rotar=2;
|
gchaves |
0:7a9b45bd5f22
|
81
|
break;
|
gchaves |
0:7a9b45bd5f22
|
82
|
case 0x40:
|
gchaves |
0:7a9b45bd5f22
|
83
|
Factor=0.125;
|
gchaves |
0:7a9b45bd5f22
|
84
|
Mascara=0x03FF;
|
gchaves |
0:7a9b45bd5f22
|
85
|
Rotar=1;
|
gchaves |
0:7a9b45bd5f22
|
86
|
break;
|
gchaves |
0:7a9b45bd5f22
|
87
|
case 0x60:
|
gchaves |
0:7a9b45bd5f22
|
88
|
Factor=0.0625;
|
gchaves |
0:7a9b45bd5f22
|
89
|
Mascara=0x07FF;
|
gchaves |
0:7a9b45bd5f22
|
90
|
Rotar=0;
|
gchaves |
0:7a9b45bd5f22
|
91
|
break;
|
gchaves |
0:7a9b45bd5f22
|
92
|
}
|
gchaves |
0:7a9b45bd5f22
|
93
|
|
gchaves |
0:7a9b45bd5f22
|
94
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
95
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
96
|
Bus.write(0x01);
|
gchaves |
0:7a9b45bd5f22
|
97
|
Bus.write(Config);
|
gchaves |
0:7a9b45bd5f22
|
98
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
99
|
}
|
gchaves |
0:7a9b45bd5f22
|
100
|
|
gchaves |
0:7a9b45bd5f22
|
101
|
void TMP175::vSetTemperatureLowTMP175(float Value){
|
gchaves |
0:7a9b45bd5f22
|
102
|
unsigned short Temp;
|
gchaves |
0:7a9b45bd5f22
|
103
|
|
gchaves |
0:7a9b45bd5f22
|
104
|
if(Value>=0.0){
|
gchaves |
0:7a9b45bd5f22
|
105
|
Temp=Value/0.0625;
|
gchaves |
0:7a9b45bd5f22
|
106
|
}else{
|
gchaves |
0:7a9b45bd5f22
|
107
|
Temp=(Value+128.0)/0.0625;
|
gchaves |
0:7a9b45bd5f22
|
108
|
//Temp!=0x0800;
|
gchaves |
0:7a9b45bd5f22
|
109
|
}
|
gchaves |
0:7a9b45bd5f22
|
110
|
Temp<<=4;
|
gchaves |
0:7a9b45bd5f22
|
111
|
|
gchaves |
0:7a9b45bd5f22
|
112
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
113
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
114
|
Bus.write(0x02);
|
gchaves |
0:7a9b45bd5f22
|
115
|
Bus.write(*((unsigned char *)&Temp+1));
|
gchaves |
0:7a9b45bd5f22
|
116
|
Bus.write(*((unsigned char *)&Temp));
|
gchaves |
0:7a9b45bd5f22
|
117
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
118
|
}
|
gchaves |
0:7a9b45bd5f22
|
119
|
|
gchaves |
0:7a9b45bd5f22
|
120
|
float TMP175::fReadTemperatureLowTMP175(void){
|
gchaves |
0:7a9b45bd5f22
|
121
|
unsigned short Temp;
|
gchaves |
0:7a9b45bd5f22
|
122
|
float Cal;
|
gchaves |
0:7a9b45bd5f22
|
123
|
|
gchaves |
0:7a9b45bd5f22
|
124
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
125
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
126
|
Bus.write(0x02);
|
gchaves |
0:7a9b45bd5f22
|
127
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
128
|
Bus.write(_Address | 0x01);
|
gchaves |
0:7a9b45bd5f22
|
129
|
*((unsigned char *)&Temp+1)=Bus.read(1);
|
gchaves |
0:7a9b45bd5f22
|
130
|
*((unsigned char *)&Temp)=Bus.read(0);
|
gchaves |
0:7a9b45bd5f22
|
131
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
132
|
|
gchaves |
0:7a9b45bd5f22
|
133
|
Temp>>=4;
|
gchaves |
0:7a9b45bd5f22
|
134
|
Cal=Temp*0.0625;
|
gchaves |
0:7a9b45bd5f22
|
135
|
if((0x0800&Temp)!=0x0000){
|
gchaves |
0:7a9b45bd5f22
|
136
|
Cal-=128.0;
|
gchaves |
0:7a9b45bd5f22
|
137
|
}
|
gchaves |
0:7a9b45bd5f22
|
138
|
|
gchaves |
0:7a9b45bd5f22
|
139
|
return(Cal);
|
gchaves |
0:7a9b45bd5f22
|
140
|
}
|
gchaves |
0:7a9b45bd5f22
|
141
|
|
gchaves |
0:7a9b45bd5f22
|
142
|
void TMP175::vSetTemperatureHighTMP175(float Value){
|
gchaves |
0:7a9b45bd5f22
|
143
|
unsigned short Temp;
|
gchaves |
0:7a9b45bd5f22
|
144
|
|
gchaves |
0:7a9b45bd5f22
|
145
|
if(Value>=0.0){
|
gchaves |
0:7a9b45bd5f22
|
146
|
Temp=Value/0.0625;
|
gchaves |
0:7a9b45bd5f22
|
147
|
}else{
|
gchaves |
0:7a9b45bd5f22
|
148
|
Temp=(Value+128.0)/0.0625;
|
gchaves |
0:7a9b45bd5f22
|
149
|
//Temp!=0x0800;
|
gchaves |
0:7a9b45bd5f22
|
150
|
}
|
gchaves |
0:7a9b45bd5f22
|
151
|
Temp<<=4;
|
gchaves |
0:7a9b45bd5f22
|
152
|
|
gchaves |
0:7a9b45bd5f22
|
153
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
154
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
155
|
Bus.write(0x03);
|
gchaves |
0:7a9b45bd5f22
|
156
|
Bus.write(*((unsigned char *)&Temp+1));
|
gchaves |
0:7a9b45bd5f22
|
157
|
Bus.write(*((unsigned char *)&Temp));
|
gchaves |
0:7a9b45bd5f22
|
158
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
159
|
}
|
gchaves |
0:7a9b45bd5f22
|
160
|
|
gchaves |
0:7a9b45bd5f22
|
161
|
float TMP175::fReadTemperatureHighTMP175(void){
|
gchaves |
0:7a9b45bd5f22
|
162
|
unsigned short Temp;
|
gchaves |
0:7a9b45bd5f22
|
163
|
float Cal;
|
gchaves |
0:7a9b45bd5f22
|
164
|
|
gchaves |
0:7a9b45bd5f22
|
165
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
166
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
167
|
Bus.write(0x03);
|
gchaves |
0:7a9b45bd5f22
|
168
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
169
|
Bus.write(_Address | 0x01);
|
gchaves |
0:7a9b45bd5f22
|
170
|
*((unsigned char *)&Temp+1)=Bus.read(1);
|
gchaves |
0:7a9b45bd5f22
|
171
|
*((unsigned char *)&Temp)=Bus.read(0);
|
gchaves |
0:7a9b45bd5f22
|
172
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
173
|
|
gchaves |
0:7a9b45bd5f22
|
174
|
Temp>>=4;
|
gchaves |
0:7a9b45bd5f22
|
175
|
Cal=Temp*0.0625;
|
gchaves |
0:7a9b45bd5f22
|
176
|
if((0x0800&Temp)!=0x0000){
|
gchaves |
0:7a9b45bd5f22
|
177
|
Cal-=128.0;
|
gchaves |
0:7a9b45bd5f22
|
178
|
}
|
gchaves |
0:7a9b45bd5f22
|
179
|
|
gchaves |
0:7a9b45bd5f22
|
180
|
return(Cal);
|
gchaves |
0:7a9b45bd5f22
|
181
|
}
|
gchaves |
0:7a9b45bd5f22
|
182
|
|
gchaves |
0:7a9b45bd5f22
|
183
|
float TMP175::fReadTemperatureTMP175(void){
|
gchaves |
0:7a9b45bd5f22
|
184
|
unsigned short Temp;
|
gchaves |
0:7a9b45bd5f22
|
185
|
float Cal;
|
gchaves |
0:7a9b45bd5f22
|
186
|
|
gchaves |
0:7a9b45bd5f22
|
187
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
188
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
189
|
Bus.write(0x00);
|
gchaves |
0:7a9b45bd5f22
|
190
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
191
|
Bus.write(_Address | 0x01);
|
gchaves |
0:7a9b45bd5f22
|
192
|
*((unsigned char *)&Temp+1)=Bus.read(1);
|
gchaves |
0:7a9b45bd5f22
|
193
|
*((unsigned char *)&Temp)=Bus.read(0);
|
gchaves |
0:7a9b45bd5f22
|
194
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
195
|
|
gchaves |
0:7a9b45bd5f22
|
196
|
Temp>>=(Rotar+4);
|
gchaves |
0:7a9b45bd5f22
|
197
|
Cal=((float)Factor*(Temp&Mascara));
|
gchaves |
0:7a9b45bd5f22
|
198
|
if(((~Mascara)&Temp)!=0x0000){
|
gchaves |
0:7a9b45bd5f22
|
199
|
Cal-=128.0;
|
gchaves |
0:7a9b45bd5f22
|
200
|
}
|
gchaves |
0:7a9b45bd5f22
|
201
|
|
gchaves |
0:7a9b45bd5f22
|
202
|
return(Cal);
|
gchaves |
0:7a9b45bd5f22
|
203
|
}
|
gchaves |
0:7a9b45bd5f22
|
204
|
|
gchaves |
0:7a9b45bd5f22
|
205
|
// Only SHUTDOWN MODE
|
gchaves |
0:7a9b45bd5f22
|
206
|
void TMP175::vStartSingleConversionTMP175(void){
|
gchaves |
0:7a9b45bd5f22
|
207
|
unsigned char Temp;
|
gchaves |
0:7a9b45bd5f22
|
208
|
|
gchaves |
0:7a9b45bd5f22
|
209
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
210
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
211
|
Bus.write(0x01);
|
gchaves |
0:7a9b45bd5f22
|
212
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
213
|
Bus.write(_Address | 0x01);
|
gchaves |
0:7a9b45bd5f22
|
214
|
Temp=Bus.read(0);
|
gchaves |
0:7a9b45bd5f22
|
215
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
216
|
|
gchaves |
0:7a9b45bd5f22
|
217
|
Bus.start();
|
gchaves |
0:7a9b45bd5f22
|
218
|
Bus.write(_Address&0xFE);
|
gchaves |
0:7a9b45bd5f22
|
219
|
Bus.write(0x01);
|
gchaves |
0:7a9b45bd5f22
|
220
|
Bus.write(Temp|0x80);
|
gchaves |
0:7a9b45bd5f22
|
221
|
Bus.stop();
|
gchaves |
0:7a9b45bd5f22
|
222
|
}
|
gchaves |
0:7a9b45bd5f22
|
223
|
|
gchaves |
0:7a9b45bd5f22
|
224
|
TMP175::operator float(){
|
gchaves |
0:7a9b45bd5f22
|
225
|
|
gchaves |
0:7a9b45bd5f22
|
226
|
return(fReadTemperatureTMP175());
|
gchaves |
0:7a9b45bd5f22
|
227
|
}
|
gchaves |
0:7a9b45bd5f22
|
228
|
|
gchaves |
0:7a9b45bd5f22
|
229
|
bool TMP175::bReadPinAlertTMP175(void){
|
gchaves |
0:7a9b45bd5f22
|
230
|
|
gchaves |
0:7a9b45bd5f22
|
231
|
return(_PIN_ALERT);
|
gchaves |
0:7a9b45bd5f22
|
232
|
} |