forked from Seeed-Studio/Seeed_MCP9600
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seeed_MCP9600.cpp
706 lines (631 loc) · 16.7 KB
/
Seeed_MCP9600.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
* Seeed_MCP9600.cpp
* Driver for MCP9600
*
* Copyright (c) 2018 Seeed Technology Co., Ltd.
* Website : www.seeed.cc
* Author : downey
* Create Time: May 2018
* Change Log :
*
* The MIT License (MIT)
*
* 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 "Seeed_MCP9600.h"
MCP9600::MCP9600(u8 IIC_ADDR)
{
set_iic_addr(IIC_ADDR);
}
/**@brief set type of thermocouple.read version.
* @param therm_type
* @return 0 if successed.
* */
err_t MCP9600::init(u8 therm_type)
{
err_t ret=NO_ERROR;
u16 ver;
IIC_begin();
ret=read_version(&ver);
if(!ret)
{
Serial.print("version =");
Serial.println(ver,HEX);
}
if(ret=set_therm_type(therm_type))
{
return ret;
}
return ret;
}
/**@brief read version.
* @param ver.
* @return 0 if successed.
* */
err_t MCP9600::read_version(u16 *ver)
{
if(IIC_read_16bit(VERSION_ID_REG_ADDR,ver))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief read hot-junction,the temperature result.
* @param value: result.
* @return 0 if successed.
* */
err_t MCP9600::read_hot_junc(float *value)
{
*value=0;
u16 read_value=0;
if(IIC_read_16bit(HOT_JUNCTION_REG_ADDR,&read_value))
{
return ERROR_COMM;
}
// Serial.print("read hot junc value=");
// Serial.println(read_value,HEX);
if(read_value&0x8000)
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0-4096.0;
}
else
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0;
}
return NO_ERROR;
}
/**@brief read junction delta.
* @param value: result.
* @return 0 if successed.
* */
err_t MCP9600::read_junc_temp_delta(float *value)
{
*value=0;
u16 read_value=0;
if(IIC_read_16bit(JUNCTION_TEMP_DELTA_REG_ADDR,&read_value))
{
return ERROR_COMM;
}
if(read_value&0x8000)
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0-4096.0;
}
else
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0;
}
return NO_ERROR;
}
/**@brief read cold-junction.
* @param value: result.
* @return 0 if successed.
* */
err_t MCP9600::read_cold_junc(float *value)
{
*value=0;
u16 read_value=0;
if(IIC_read_16bit(COLD_JUNCTION_TEMP_REG_ADDR,&read_value))
{
return ERROR_COMM;
}
if(read_value&0xf000)
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0-4096;
}
else
{
*value = (read_value>>8)*16.0+(read_value&0x00ff)/16.0;
}
return NO_ERROR;
}
/**@brief read raw ADC value.
* @param data
* @param data_len
* @return 0 if successed.
* */
err_t MCP9600::read_ADC_data(u8* data,u32 data_len)
{
if(IIC_read_bytes(RAW_ADC_DATA_REG_ADDR,data,data_len))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief read sensor status.
* @param byte: result.
* @return 0 if successed.
* */
err_t MCP9600::read_status(u8* byte)
{
*byte=0;
if(IIC_read_byte(STAT_REG_ADDR,byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief set_therm_cfg
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_therm_cfg(u8 set_byte)
{
if(IIC_write_byte(THERM_SENS_CFG_REG_ADDR,set_byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief read thermocouple status.
* @param byte: result.
* @return 0 if successed.
* */
err_t MCP9600::read_therm_cfg(u8* byte)
{
if(IIC_read_byte(THERM_SENS_CFG_REG_ADDR,byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief set_therm_type
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_therm_type(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(THERM_SENS_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0x8f)|set_byte;
return IIC_write_byte(THERM_SENS_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_filt_coefficients
* The content of filt-coefficients register are:code-junction-resolution/ADC measurement resolution/burst mode temp samples/sensor mode
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_filt_coefficients(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(THERM_SENS_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xf8)|set_byte;
return IIC_write_byte(THERM_SENS_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_dev_cfg
* The content of device configuration register are:interrupt clear/monitor TH or TC/inerrupt pin rise or FALL/
* Active-high or low/compare mode or int mode /alert enable or not
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_dev_cfg(u8 set_byte)
{
if(IIC_write_byte(DEVICE_CFG_REG_ADDR,set_byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief read_dev_cfg
* @param byte: the byte to be read in.
* @return 0 if successed.
* */
err_t MCP9600::read_dev_cfg(u8* byte)
{
if(IIC_read_byte(DEVICE_CFG_REG_ADDR,byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief set_sensor_mode
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_sensor_mode(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(DEVICE_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xfc)|set_byte;
return IIC_write_byte(DEVICE_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_burst_mode_samp
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_burst_mode_samp(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(DEVICE_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xe3)|set_byte;
return IIC_write_byte(DEVICE_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_ADC_meas_resolution
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_ADC_meas_resolution(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(DEVICE_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0x9f)|set_byte;
return IIC_write_byte(DEVICE_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_cold_junc_resolution
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_cold_junc_resolution(u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(DEVICE_CFG_REG_ADDR,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0x7f)|set_byte;
return IIC_write_byte(DEVICE_CFG_REG_ADDR,byte_to_set);
}
/**@brief set_alert_limitation
* @param alert num the channel of alert
* @param value: the 16bit value to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_limit(u8 alert_num,u16 value)
{
if(IIC_write_16bit(TEMP_ALERT1_LIMIT_REG_ADDR+alert_num,value))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief set_alert_hysteresis
* @param alert num the channel of alert
* @param set_byte: the byte to be set..
* @return 0 if successed.
* */
err_t MCP9600::set_alert_hys(u8 alert_num,u16 value)
{
if(IIC_write_byte(ALERT1_HYS_REG_ADDR+alert_num,value))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief set_alert_cfg
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_cfg(u8 alert_num,u8 set_byte)
{
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,set_byte);
}
/**@brief read_alert_cfg
* @param alert num the channel of alert
* @param byte: the byte to be read in.
* @return 0 if successed.
* */
err_t MCP9600::read_alert_cfg(u8 alert_num,u8 *byte)
{
*byte=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,byte))
{
return ERROR_COMM;
}
return NO_ERROR;
}
/**@brief clear_int_flag
* @param alert num the channel of alert
* @return 0 if successed.
* */
err_t MCP9600::clear_int_flag(u8 alert_num)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data|0x80);
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief set_alert_for_TH_or_TC
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_for_TH_or_TC(u8 alert_num,u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xef)|set_byte;
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief set_alert_limit_direction
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_limit_direction(u8 alert_num,u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xf7)|set_byte;
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief set_alert_bit
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_bit(u8 alert_num,u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xfb)|set_byte;
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief set_alert_mode_bit
* set alert mode:comparator mode or INT mode.
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_mode_bit(u8 alert_num,u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xfd)|set_byte;
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief set_alert_enable
* Eable alert pin or not.
* @param alert num the channel of alert
* @param set_byte: the byte to be set.
* @return 0 if successed.
* */
err_t MCP9600::set_alert_enable(u8 alert_num,u8 set_byte)
{
u8 therm_cfg_data=0;
u8 byte_to_set=0;
if(IIC_read_byte(ALERT1_CFG_REG_ADDR+alert_num,&therm_cfg_data))
{
return ERROR_COMM;
}
byte_to_set=(therm_cfg_data&0xfe)|set_byte;
return IIC_write_byte(ALERT1_CFG_REG_ADDR+alert_num,byte_to_set);
}
/**@brief check_data_update
* check if data ready.
* @param stat :indicate if data ready
* @return 0 if successed.
* */
err_t MCP9600::check_data_update(bool *stat)
{
*stat=0;
err_t ret=NO_ERROR;
u8 byte=0;
CHECK_RESULT(ret,read_status(&byte));
if(byte&0x40)
{
*stat=true;
}
else
{
*stat=false;
}
return NO_ERROR;
}
/**@brief read_INT_stat
* check if any interruption is generated.
* @param stat :indicate if any interruption is generated
* @return 0 if successed.
* */
err_t MCP9600::read_INT_stat(u8 *stat)
{
*stat=0;
err_t ret=NO_ERROR;
u8 byte=0;
CHECK_RESULT(ret,read_status(&byte));
for(int i=0;i<4;i++)
{
if(byte & 1<<i)
{
Serial.print("channel ");
Serial.print(i);
Serial.println("generate interruption!!!");
}
}
*stat=byte;
return NO_ERROR;
}
/******************************************************************************************************/
u16 MCP9600::covert_temp_to_reg_form(float temp)
{
u8 negetive=0;
if(temp<0) negetive=1;
temp=abs(temp);
u16 dest_temp=0;
u8 temp_H=0,temp_L=0;
u16 interger=(u16)temp;
float decimal=temp-interger;
temp_H=interger/16;
temp_L|=(interger%16)<<4;
temp_L|=(u8)(decimal/0.25)<<2;
if(negetive)
temp_H|=0x80;
dest_temp=(u16)temp_H<<8|temp_L;
return dest_temp;
}
/**********************************************************************************************************/
/************************************************IIC PART************************************************/
/**********************************************************************************************************/
/**@brief I2C write byte
* @param reg :Register address of operation object
* @param byte :The byte to be wrote.
* @return result of operation,non-zero if failed.
* */
err_t IIC_OPRTS::IIC_write_byte(u8 reg,u8 byte)
{
s32 ret=0;
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
Wire.write(byte);
ret=Wire.endTransmission();
if(!ret)
return NO_ERROR;
else
return ERROR_COMM;
}
/**@brief I2C write 16bit value
* @param reg: Register address of operation object
* @param value: The 16bit value to be wrote .
* @return result of operation,non-zero if failed.
* */
err_t IIC_OPRTS::IIC_write_16bit(u8 reg,u16 value)
{
s32 ret=0;
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
Wire.write((u8)(value>>8));
Wire.write((u8)value);
ret=Wire.endTransmission();
if(!ret)
return NO_ERROR;
else
return ERROR_COMM;
}
/**@brief I2C read byte
* @param reg: Register address of operation object
* @param byte: The byte to be read in.
* @return result of operation,non-zero if failed.
* */
err_t IIC_OPRTS::IIC_read_byte(u8 reg,u8* byte)
{
err_t ret=NO_ERROR;
u32 time_out_count=0;
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(_IIC_ADDR,(u8)1);
while(1!=Wire.available())
{
time_out_count++;
if(time_out_count>10) return ERROR_COMM;
delay(1);
}
*byte=Wire.read();
return NO_ERROR;
}
/**@brief I2C read 16bit value
* @param reg: Register address of operation object
* @param byte: The 16bit value to be read in.
* @return result of operation,non-zero if failed.
* */
err_t IIC_OPRTS::IIC_read_16bit(u8 start_reg,u16 *value)
{
err_t ret=NO_ERROR;
u32 time_out_count=0;
u8 val=0;
*value=0;
Wire.beginTransmission(_IIC_ADDR);
Wire.write(start_reg);
Wire.endTransmission(false);
Wire.requestFrom(_IIC_ADDR,sizeof(u16));
while(sizeof(u16)!=Wire.available())
{
time_out_count++;
if(time_out_count>10) return ERROR_COMM;
delay(1);
}
val=Wire.read();
*value|=(u16)val<<8;
val=Wire.read();
*value|=val;
return NO_ERROR;
}
/**@brief I2C read some bytes
* @param reg: Register address of operation object
* @param data: The buf to be read in.
* @param data_len: The length of buf need to read in.
* @return result of operation,non-zero if failed.
* */
err_t IIC_OPRTS::IIC_read_bytes(u8 start_reg,u8 *data,u32 data_len)
{
err_t ret=NO_ERROR;
u32 time_out_count=0;
Wire.beginTransmission(_IIC_ADDR);
Wire.write(start_reg);
Wire.endTransmission(false);
Wire.requestFrom(_IIC_ADDR,data_len);
while(data_len!=Wire.available())
{
time_out_count++;
if(time_out_count>10) return ERROR_COMM;
delay(1);
}
for(int i=0;i<data_len;i++)
{
data[i]=Wire.read();
}
return ret;
}
/**@brief change the I2C address from default.
* @param IIC_ADDR: I2C address to be set
* */
void IIC_OPRTS::set_iic_addr(u8 IIC_ADDR)
{
_IIC_ADDR=IIC_ADDR;
}