-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2998 lines (2316 loc) · 94 KB
/
main.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
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ******************************************************************************
* VSCP (Very Simple Control Protocol)
* http://www.vscp.org
*
* Kelvin NTC10KA Module
* =====================
*
* Copyright (C) 2000-2020 Ake Hedman, Grodans Paradis AB
* http://www.grodansparadis.com
* <akhe@grodansparadis.com>
*
* This work is licensed under the Creative Common
* Attribution-NonCommercial-ShareAlike 3.0 Unported license. The full
* license is available in the top folder of this project (LICENSE) or here
* http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
* It is also available in a human readable form here
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* This file is part of VSCP - Very Simple Control Protocol
* http://www.vscp.org
*
* ******************************************************************************
*/
#include "vscp_compiler.h"
#include "vscp_projdefs.h"
#include <p18cxxx.h>
#include <timers.h>
#include <adc.h>
#include <math.h>
#include <vscp_inttypes.h>
#include <ECAN.h>
#include <vscp-firmware.h>
#include <vscp_class.h>
#include <vscp_type.h>
#include "main.h"
#include "version.h"
#include "ntc.h"
#if defined(_18F2580)
#if defined(RELEASE)
#pragma config WDT = ON, WDTPS = 128
#pragma config OSC = HSPLL
#pragma config BOREN = BOACTIVE
#pragma config STVREN = ON
#pragma config BORV = 0 // 4.6V
#pragma config LVP = ON
#pragma config CPB = ON
#pragma config BBSIZ = 2048
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
#else
#pragma config WDT = OFF
#pragma config OSC = HSPLL
#pragma config PWRT = ON
#pragma config BOREN = BOACTIVE
#pragma config STVREN = ON
#pragma config BORV = 0 // 4.6V
#pragma config LVP = OFF
#pragma config CPB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
#endif
#else if defined(_18F25K80) || defined(_18F26K80) || defined(_18F45K80) || defined(_18F46K80) || defined(_18F65K80) || defined(_18F66K80)
// CONFIG1L
#pragma config SOSCSEL = DIG // RC0/RC is I/O
#pragma config RETEN = OFF // Ultra low-power regulator is Disabled (Controlled by REGSLP bit).
#pragma config INTOSCSEL = HIGH // LF-INTOSC in High-power mode during Sleep.
#pragma config XINST = OFF // No extended instruction set
// CONFIG1H
#pragma config FOSC = HS2 // Crystal 10 MHz
#pragma config PLLCFG = ON // 4 x PLL
// CONFIG2H
#pragma config WDTPS = 1048576 // Watchdog prescaler
#pragma config BOREN = SBORDIS // Brown out enabled
#pragma config BORV = 0 // 3V
// CONFIG3H
#pragma config CANMX = PORTB // ECAN TX and RX pins are located on RB2 and RB3, respectively.
#pragma config MSSPMSK = MSK7 // 7 Bit address masking mode.
#pragma config MCLRE = ON // MCLR Enabled, RE3 Disabled.
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset enabled
#pragma config BBSIZ = BB2K // Boot block size 2K
#ifdef DEBUG
#pragma config WDTEN = OFF // WDT disabled in hardware; SWDTEN bit disabled.
#else
#pragma config WDTEN = ON // WDT enabled in hardware;
#endif
#endif
// The device URL (max 32 characters including null termination)
const uint8_t vscp_deviceURL[] = "www.eurosource.se/ntc10KA_3.xml";
// Global Variable Declarations
int16_t current_temp[6]; // Current temperature
uint8_t adc[NUMBER_OF_TEMP_SERIES * 12]; // Current ADC values
uint8_t adc_conversion_flags; // Bits to flag new ADC values
uint8_t adc_series_counter; // Series counter
volatile uint16_t sendTimer; // Timer for CAN send
volatile uint32_t measurement_clock; // Clock for measurements
volatile uint32_t timeout_clock; // Clock used for timeouts
uint8_t seconds; // counter for seconds
uint8_t seconds_temp[6]; // timers for temp event
// Alarm flag bits
uint8_t low_alarm;
uint8_t high_alarm;
// Thermistor coefficients
double sh_coefficients[6*3]; // 6 sensors each with 3 32-bit constants
///////////////////////////////////////////////////////////////////////////////
// Isr() - Interrupt Service Routine
// - Services Timer0 Overflow
// - Services GP3 Pin Change
//////////////////////////////////////////////////////////////////////////////
void interrupt low_priority interrupt_at_low_vector( void )
{
// Check timer
if (INTCONbits.TMR0IF) { // If A Timer0 Interrupt, Then
// Reload value for 1 ms resolution
WriteTimer0(TIMER0_RELOAD_VALUE);
vscp_timer++;
vscp_configtimer++;
measurement_clock++;
timeout_clock++;
sendTimer++;
// Check for init. button
if (!(PORTC & 0x01)) {
// Active
vscp_initbtncnt++;
}
else {
vscp_initbtncnt = 0;
}
// Status LED
vscp_statuscnt++;
if ( ( VSCP_LED_BLINK1 ==
vscp_initledfunc) && (vscp_statuscnt > 100)) {
if ( PORTC & 0x02 ) {
PORTC &= ~0x02;
}
else {
PORTC |= 0x02;
}
vscp_statuscnt = 0;
}
else if (VSCP_LED_ON == vscp_initledfunc) {
PORTC |= 0x02;
vscp_statuscnt = 0;
}
else if (VSCP_LED_OFF == vscp_initledfunc) {
PORTC &= ~0x02;
vscp_statuscnt = 0;
}
INTCONbits.TMR0IF = 0; // Clear Timer0 Interrupt Flag
} // Timer & button
// Check ADC
if ( PIR1bits.ADIF ) {
#if defined(_18F2580)
switch (0x3C & ADCON0) {
#else
switch (0x7C & ADCON0) {
#endif
case SELECT_ADC_TEMP0:
// Read conversion
adc[(12 * adc_series_counter) + 0] = ADRESH;
adc[(12 * adc_series_counter) + 1] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP1 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1;
}
break;
case SELECT_ADC_TEMP1:
// Read conversion
adc[(12 * adc_series_counter) + 2] = ADRESH;
adc[(12 * adc_series_counter) + 3] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP2 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1 << 1;
}
break;
case SELECT_ADC_TEMP2:
// Read conversion
adc[(12 * adc_series_counter) + 4] = ADRESH;
adc[(12 * adc_series_counter) + 5] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP3 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1 << 2;
}
break;
case SELECT_ADC_TEMP3:
// Read conversion
adc[(12 * adc_series_counter) + 6] = ADRESH;
adc[(12 * adc_series_counter) + 7] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP4 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1 << 3;
}
break;
case SELECT_ADC_TEMP4:
// Read conversion
adc[(12 * adc_series_counter) + 8] = ADRESH;
adc[(12 * adc_series_counter) + 9] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP5 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1 << 4;
}
break;
case SELECT_ADC_TEMP5:
// Read conversion
adc[(12 * adc_series_counter) + 10] = ADRESH;
adc[(12 * adc_series_counter) + 11] = ADRESL;
// Start new conversion
ADCON0 = SELECT_ADC_TEMP0 + 1;
// Mark that a new adc value is available if a full series
// has been completed.
if ((NUMBER_OF_TEMP_SERIES - 1) == adc_series_counter) {
adc_conversion_flags |= 1 << 5;
}
// Fill next series
adc_series_counter++;
if (adc_series_counter >= NUMBER_OF_TEMP_SERIES) {
adc_series_counter = 0;
}
break;
default:
// Start new conversion
ADCON0 = SELECT_ADC_TEMP0 + 1;
adc_series_counter = 0;
break;
}
// Start conversion
ConvertADC();
PIR1bits.ADIF = 0; // Reset interrupt flag
} // ADC
return;
}
//***************************************************************************
// Main() - Main Routine
//***************************************************************************
void main()
{
uint32_t i;
init();
// Check VSCP persistent storage and
// restore if needed
if ( !vscp_check_pstorage() ) {
// Spoiled or not initialised - reinitialise
init_app_eeprom();
}
vscp_init(); // Initialise the VSCP functionality
while (TRUE) { // Loop Forever
ClrWdt(); // Feed the dog
if ( ( vscp_initbtncnt > 2500 ) &&
( VSCP_STATE_INIT != vscp_node_state ) ) {
// Init. button pressed
vscp_nickname = VSCP_ADDRESS_FREE;
eeprom_write(VSCP_EEPROM_NICKNAME, VSCP_ADDRESS_FREE);
vscp_init();
}
// Check for a valid event
vscp_imsg.flags = 0;
vscp_getEvent();
switch (vscp_node_state) {
case VSCP_STATE_STARTUP: // Cold/warm reset
// Get nickname from EEPROM
if (VSCP_ADDRESS_FREE == vscp_nickname) {
// new on segment need a nickname
vscp_node_state = VSCP_STATE_INIT;
}
else {
// been here before - go on
vscp_node_state = VSCP_STATE_ACTIVE;
vscp_goActiveState();
}
break;
case VSCP_STATE_INIT: // Assigning nickname
vscp_handleProbeState();
break;
case VSCP_STATE_PREACTIVE: // Waiting for host initialization
vscp_goActiveState();
break;
case VSCP_STATE_ACTIVE: // The normal state
// Check for incoming message?
if (vscp_imsg.flags & VSCP_VALID_MSG) {
if ( VSCP_CLASS1_PROTOCOL == vscp_imsg.vscp_class ) {
// Handle protocol event
vscp_handleProtocolEvent();
}
else if ( ( VSCP_CLASS1_CONTROL == vscp_imsg.vscp_class ) &&
( VSCP_TYPE_CONTROL_SYNC == vscp_imsg.vscp_type ) ) {
handle_sync();
}
}
break;
case VSCP_STATE_ERROR: // Everything is *very* *very* bad.
vscp_error();
break;
default: // Should not be here...
vscp_node_state = VSCP_STATE_STARTUP;
break;
}
// do a measurement if needed
if ( measurement_clock > 1000 ) {
measurement_clock = 0;
doOneSecondWork();
seconds++;
// Temperature report timers are only updated if in active
// state
if (VSCP_STATE_ACTIVE == vscp_node_state) {
for (i = 0; i < 6; i++) {
seconds_temp[i]++;
}
}
if (seconds > 59) {
seconds = 0;
}
// Do VSCP one second jobs
// Check if alarm events should be sent
if (VSCP_STATE_ACTIVE == vscp_node_state) {
vscp_doOneSecondWork();
}
// Also do some work
doWork();
}
} // while
}
///////////////////////////////////////////////////////////////////////////////
// doWork
//
// The actual work is done here.
//
void doWork(void)
{
uint8_t i, j;
uint16_t B;
double avarage;
double resistance;
double Rinf;
double temp;
double v;
double calVoltage;
calVoltage = (double)construct_unsigned16( eeprom_read(EEPROM_CALIBRATED_VOLTAGE_MSB),
eeprom_read(EEPROM_CALIBRATED_VOLTAGE_LSB) ) / 10000.0;
// Check if there are new adc values to
// convert to temperatures
for (i = 0; i < 6; i++) {
if (adc_conversion_flags & 1 << i) {
// Calculate mean value for this adc
avarage = 0;
for (j = 0; j < NUMBER_OF_TEMP_SERIES; j++) {
avarage += construct_unsigned16( adc[12 * j + 2 * i],
adc[12 * j + 2 * i + 1] );
}
avarage = avarage / NUMBER_OF_TEMP_SERIES;
if (1) {
// Use B-constant
// ==============
// http://en.wikipedia.org/wiki/Thermistor
// R1 = (R2V - R2V2) / V2 R2= 10K, V = 5V, V2 = adc * voltage/1024
// T = B / ln(r/Rinf)
// Rinf = R0 e (-B/T0), R0=10K, T0 = 273.15 + 25 = 298.15
B = construct_unsigned16( eeprom_read(2 * i + EEPROM_B_CONSTANT0_MSB ),
eeprom_read(2 * i + EEPROM_B_CONSTANT0_LSB) );
Rinf = 10000.0 * exp(B/-298.15);
#if defined(_18F2580)
// V2 = adc * voltage/1024
v = calVoltage * (double)avarage/1024;
#else
// V2 = adc * voltage/4096
v = calVoltage * (double)avarage/4096;
#endif
// R1 = (R2V - R2V2) / V2 R2= 10K, V = 5V, V2 = adc * voltage/1024
resistance = ( 10000.0*(calVoltage - v) ) / v;
//itemp = r;
temp = ((double) B) / log(resistance / Rinf);
//itemp = log(r/Rinf);
temp -= 273.15; // Convert Kelvin to Celsius
//avarage = testadc;
/* https://learn.adafruit.com/thermistor/using-a-thermistor
avarage = (1023/avarage) - 1;
avarage = 10000 / avarage; // Resistance of termistor
//temp = avarage/10000; // (R/Ro)
temp = 10000/avarage;
temp = log(temp); // ln(R/Ro)
temp /= B; // 1/B * ln(R/Ro)
temp += 1.0 / (25 + 273.15); // + (1/To)
temp = 1.0 / temp; // Invert
temp -= 273.15;
*/
current_temp[ i ] = ( current_temp[ i ] + ( (long)(temp * 100) + getCalibrationValue( i ) ) ) / 2;
}
else {
// Use S-H equation
// ================
// Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024/ADC)
#if defined(_18F2580)
resistance = ((10240000 / adc[2 * i + 1]) - 10000);
#else
// Assuming a 10k Thermistor. Calculation is actually: Resistance = (4096/ADC)
resistance = ((40960000 / adc[2 * i + 1]) - 10000);
#endif
/********************************************************************/
/* Utilises the Steinhart-Hart Thermistor Equation: */
/* Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3} */
/********************************************************************/
temp = log(resistance);
temp = 1 / (sh_coefficients[i * 3] + (sh_coefficients[i * 3 + 1] * temp) +
(sh_coefficients[i * 3 + 2] * temp * temp * temp));
temp = temp - 273.15; // Convert Kelvin to Celsius
current_temp[ i ] = (current_temp[ i ] + ((long) (temp * 100) + getCalibrationValue(i))) / 2;
}
// Reset flag
adc_conversion_flags &= ~(1 << i);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// doOneSecondWork
//
void doOneSecondWork(void)
{
uint8_t tmp;
uint8_t i;
int16_t setpoint;
for (i = 0; i < 6; i++) {
//*********************************************************************
// Check if this is the lowest temperature ever
//*********************************************************************
if (current_temp[ i ] < construct_signed16( eeprom_read(EEPROM_ABSOLUT_LOW0_MSB + 2*i),
eeprom_read(EEPROM_ABSOLUT_LOW0_LSB + 2*i) ) ) {
// Store new lowest value
eeprom_write(EEPROM_ABSOLUT_LOW0_MSB + 2*i, ((uint16_t)current_temp[ i ]) >> 8);
eeprom_write(EEPROM_ABSOLUT_LOW0_LSB + 2*i, ((uint16_t)current_temp[ i ]) & 0xff);
}
//*********************************************************************
// Check if this is the highest temperature ever
//*********************************************************************
if (current_temp[ i ] > construct_signed16( eeprom_read(EEPROM_ABSOLUT_HIGH0_MSB + 2*i),
eeprom_read(EEPROM_ABSOLUT_HIGH0_LSB + 2*i ) ) ) {
// Store new lowest value
eeprom_write(EEPROM_ABSOLUT_HIGH0_MSB + 2*i, ((uint16_t)current_temp[ i ]) >> 8);
eeprom_write(EEPROM_ABSOLUT_HIGH0_LSB + 2*i, ((uint16_t)current_temp[ i ]) & 0xff );
}
//*********************************************************************
// Check if temperature report events should be sent
//*********************************************************************
tmp = eeprom_read(EEPROM_REPORT_INTERVAL0 + i);
if (tmp && (seconds_temp[i] > tmp)) {
// Send event
if (sendTempEvent(i)) {
seconds_temp[i] = 0;
}
}
//*********************************************************************
// Check for continuous alarm
//*********************************************************************
if (MASK_CONTROL_CONTINUOUS & eeprom_read(EEPROM_CONTROLREG0 + i)) {
// If low alarm active for sensor
if (low_alarm & (1 << i)) {
// Alarm must be enabled
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_LOW_ALARM) {
vscp_omsg.priority = VSCP_PRIORITY_HIGH;
vscp_omsg.flags = VSCP_VALID_MSG + 3;
// Should ALARM or TURNON/TURNOFF events be sent
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNX) {
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNON_INVERT) {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNON;
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNOFF;
}
}
else {
// Alarm event should be sent
vscp_omsg.vscp_class = VSCP_CLASS1_ALARM;
vscp_omsg.vscp_type = VSCP_TYPE_ALARM_ALARM;
}
vscp_omsg.data[ 0 ] = i; // Index = sensor
vscp_omsg.data[ 1 ] =
eeprom_read(EEPROM_SENSOR0_ZONE + 2*i); // Zone
vscp_omsg.data[ 2 ] =
eeprom_read(EEPROM_SENSOR0_SUBZONE + 2*i); // Subzone
// Send event
// We allow for missing to send this event
// as it will be sent next second instead.
vscp_sendEvent();
}
}
// If high alarm active for sensor
if (high_alarm & (1 << i)) {
// Should ALARM or TURNON/TURNOFF events be sent
if ((eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_HIGH_ALARM)) {
vscp_omsg.priority = VSCP_PRIORITY_HIGH;
vscp_omsg.flags = VSCP_VALID_MSG + 3;
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNX) {
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNON_INVERT) {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNOFF;
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNON;
}
}
else {
// Alarm event should be sent
vscp_omsg.vscp_class = VSCP_CLASS1_ALARM;
vscp_omsg.vscp_type = VSCP_TYPE_ALARM_ALARM;
}
vscp_omsg.data[ 0 ] = i; // Index = sensor
vscp_omsg.data[ 1 ] =
eeprom_read(EEPROM_SENSOR0_ZONE + 2*i); // Zone
vscp_omsg.data[ 2 ] =
eeprom_read(EEPROM_SENSOR0_SUBZONE + 2*i); // Sub zone
// Send event
// We allow for missing to send this event
// as it will be sent next second instead.
vscp_sendEvent();
}
}
}
//*********************************************************************
// Check if we have a low alarm condition
//*********************************************************************
if (low_alarm & (1 << i)) {
// We have an alarm condition already
setpoint = construct_signed16(eeprom_read(EEPROM_LOW_ALARM0_MSB + 2*i),
eeprom_read(EEPROM_LOW_ALARM0_LSB + 2*i)) +
(int8_t) eeprom_read(EEPROM_HYSTERESIS_SENSOR0 + i);
// Check if it is no longer valid
// that is under hysteresis so we can rest
// alarm condition
if (current_temp[ i ] > (setpoint * 100)) {
// Reset alarm condition
low_alarm &= ~(1 << i);
}
}
else {
// We do not have a low alarm condition already
// check if we should have
setpoint = construct_signed16( eeprom_read(EEPROM_LOW_ALARM0_MSB + 2*i ),
eeprom_read(EEPROM_LOW_ALARM0_LSB + 2*i ) );
if (current_temp[ i ] < (setpoint * 100)) {
// We have a low alarm condition
low_alarm |= (1 << i);
// Set module alarm flag
// Note that this bit is set even if we are unable
// to send an alarm event.
vscp_alarmstatus |= MODULE_LOW_ALARM;
// Should ALARM events be sent
if ( eeprom_read(i + EEPROM_CONTROLREG0) & CONFIG_ENABLE_LOW_ALARM ) {
vscp_omsg.priority = VSCP_PRIORITY_HIGH;
vscp_omsg.flags = VSCP_VALID_MSG + 3;
// Should TurnOn/TurnOff events be sent
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNX) {
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNON_INVERT) {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNON;
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNOFF;
}
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_ALARM;
vscp_omsg.vscp_type = VSCP_TYPE_ALARM_ALARM;
}
vscp_omsg.data[ 0 ] = i; // Index
vscp_omsg.data[ 1 ] = eeprom_read(EEPROM_SENSOR0_ZONE + 2 * i); // Zone
vscp_omsg.data[ 2 ] = eeprom_read(EEPROM_SENSOR0_SUBZONE + 2 * i); // Sub zone
// Send event
if (!vscp_sendEvent()) {
// Could not send alarm event
// Reset alarm - we try again next round
low_alarm &= ~(1 << i);
}
}
}
}
//*********************************************************************
// Check if we have a high alarm condition
//*********************************************************************
if (high_alarm & (1 << i)) {
// We have an alarm condition already
setpoint = construct_signed16(eeprom_read(EEPROM_HIGH_ALARM0_MSB + 2 * i),
eeprom_read(EEPROM_HIGH_ALARM0_LSB + 2 * i)) -
(int8_t)eeprom_read(EEPROM_HYSTERESIS_SENSOR0 + i);
// Under hysteresis so we can reset condition
if (current_temp[ i ] < (setpoint * 100) ) {
// Reset alarm
high_alarm &= ~(1 << i);
}
}
else {
// We do not have an alarm condition
// check for one
setpoint = construct_signed16(eeprom_read(EEPROM_HIGH_ALARM0_MSB + 2 * i),
eeprom_read(EEPROM_HIGH_ALARM0_LSB + 2 * i));
if (current_temp[ i ] > (setpoint * 100)) {
// We have a low alarm condition
high_alarm |= (1 << i);
// Set module alarm flag
// Note that this bit is set even if we are unable
// to send an alarm event.
vscp_alarmstatus |= MODULE_HIGH_ALARM;
// Should ALARM or TURNON/TURNOFF events be sent
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_HIGH_ALARM) {
vscp_omsg.priority = VSCP_PRIORITY_HIGH;
vscp_omsg.flags = VSCP_VALID_MSG + 3;
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNX) {
if (eeprom_read(EEPROM_CONTROLREG0 + i) & CONFIG_ENABLE_TURNON_INVERT) {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNOFF;
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_CONTROL;
vscp_omsg.vscp_type = VSCP_TYPE_CONTROL_TURNON;
}
}
else {
vscp_omsg.vscp_class = VSCP_CLASS1_ALARM;
vscp_omsg.vscp_type = VSCP_TYPE_ALARM_ALARM;
}
vscp_omsg.data[ 0 ] = i; // Index
vscp_omsg.data[ 1 ] = eeprom_read(EEPROM_SENSOR0_ZONE + 2 * i); // Zone
vscp_omsg.data[ 2 ] = eeprom_read(EEPROM_SENSOR0_SUBZONE + 2 * i); // Sub zone
// Send event
if (!vscp_sendEvent()) {
// Could not send alarm event
// Reset alarm - we try again next round
high_alarm &= ~(1 << i);
}
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// sendTempEvent
//
int8_t sendTempEvent(uint8_t i)
{
vscp_omsg.priority = VSCP_PRIORITY_MEDIUM;
vscp_omsg.flags = VSCP_VALID_MSG + 4;
vscp_omsg.vscp_class = VSCP_CLASS1_MEASUREMENT;
vscp_omsg.vscp_type = VSCP_TYPE_MEASUREMENT_TEMPERATURE;
// Data format
vscp_omsg.data[ 0 ] = 0x80 | // Normalized integer
((0x03 & eeprom_read(i + EEPROM_CONTROLREG0)) << 3) | // Unit
i; // Sensor
// Exponent
vscp_omsg.data[ 1 ] = 0x82;
setEventData( current_temp[i],
( 0x03 & eeprom_read(i + EEPROM_CONTROLREG0 ) ) );
// Send event
if (!vscp_sendEvent()) {
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// setEventData
//
void setEventData(int v, unsigned char unit)
{
double newval;
int ival;
if (TEMP_UNIT_KELVIN == unit) {
// Convert to Kelvin
newval = Celsius2Kelvin(v);
} else if (TEMP_UNIT_FAHRENHEIT == unit) {
// Convert to Fahrenheit
newval = Celsius2Fahrenheit(v);
} else {
// Defaults to Celsius
newval = v;
}
ival = (int) newval;
vscp_omsg.data[ 2 ] = ((ival & 0xff00) >> 8);
vscp_omsg.data[ 3 ] = (ival & 0xff);
}
///////////////////////////////////////////////////////////////////////////////
// getCalibrationValue
//
// Get the calibration value for a specific sensor.
//
int16_t getCalibrationValue(uint8_t i)
{
int16_t cal;
cal = construct_signed16( eeprom_read(2 * i + EEPROM_CALIBRATION_SENSOR0_MSB),
eeprom_read(2 * i + EEPROM_CALIBRATION_SENSOR0_LSB) );
return cal;
}
///////////////////////////////////////////////////////////////////////////////
// Init - Initialization Routine
//
void init()
{
// Initialize data
init_app_ram();
// Initialize the uP
// PORTA
// RA0/AN0 - input
// RA1/AN1 - input
// RA2/AN2 - input
// RA4 input (VCAP for PIC18F26K80))
TRISA = 0b00010111;
// PortB
// RB7 - Not used.
// RB& - Not used.
// RB5 - Not used.
// RB4/AN9 - input
// RB3 CAN RX - input
// RB2 CAN TX - output
// RB1/AN8 - input
// RB0/AN10 -input
TRISB = 0b00011011; // 0x1B;
// RC7 - Output - Not used.
// RC6 - Output - Not used.
// RC5 - Output - Not used.
// RC3 - Output - Not used.
// RC2 - Output - Not used.
// RC1 - Output - Status LED - Default off
// RC0 - Input - Init. button
TRISC = 0b00000001;
PORTC = 0x00;
OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_8);
WriteTimer0(TIMER0_RELOAD_VALUE);
#if defined(_18F2580)
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,
ADC_CH0 & ADC_INT_ON & ADC_11ANA &
ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
15 );
#else if defined(_18F25K80) || defined(_18F26K80) || defined(_18F45K80) || defined(_18F46K80) || defined(_18F65K80) || defined(_18F66K80)
// Disable comparators
CM1CON = 0x00;
CM2CON = 0x00;
CVRCON = 0;
ANCON0bits.ANSEL0=1;
ANCON0bits.ANSEL1=1;
ANCON0bits.ANSEL2=1;
ANCON1bits.ANSEL8=1;
ANCON1bits.ANSEL9=1;
ANCON1bits.ANSEL10=1;
// OpenADC_Page16
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,