-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1379 lines (1268 loc) · 65.3 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
// Solar Heater Vent
#ifdef pic688
#include <xc.h>
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Detect (BOR enabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#include <pic16f688.h>
#endif
// PIC16F18426 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINTPLL// Power-up default value for COSC bits (HFINTOSC with 2x PLL, with OSCFRQ = 16 MHz and CDIV = 1:1 (FOSC = 32 MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTS = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCDDIS = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTD = OFF // Data EEPROM write protection bit (Data EEPROM NOT write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pic16f18426.h>
#include "definitions.h"
void uartSetup();
void setupTimer0();
void setupTimer1();
void setupTimer2();
void TXout(char *string);
void setupAtoD();
void printDoorSwitches();
void setupIOC();
//double normsinv( double p);
//int linearCorrelationCoefficient (int numberofSamples, int ConfidenceLevel);
char buf[10];
char clearScreen[] = "\033[2J";
char intro1[] = "\033[?25l\033[H \033[1;27HVentilator Door Software 2018" \
"\033[2;35HApril 9, 2018" \
"\033[4;45HClosed Door Accel:\033[5;45HOpened Door Accel: " \
"\033[4;1HInside Temperature :\033[5;1HOutside Temperature : \033[6;1HTemperature Difference :";
char intro2[] = "\033[6;45HMax. Temperature :\033[8;1HStudent T Inside Change :" \
"\033[9;1HStudent T Outside Change:\033[7;45HDoor Pos. Accel. :" \
"\033[8;45HDoor Percent Open:\033[9;45HMotor Direction :";
char intro3[] = "\033[7;1HAdjustment Process: ";
char Tinside[] = "\033[4;27H";
char Toutside[] = "\033[5;27H";
char Tdiff[] = "\033[6;27H";
char Tmax[] = "\033[6;64H";
char studentTin[] = "\033[8;27H";
char studentTout[] = "\033[9;27H";
char upDoorSw[] = "\033[4;64H";
char lowDoorSw[] = "\033[5;64H";
//char doorTime[] = "\033[7;66H\0";
char doorOFC[] = "\033[7;64H"; // Door COunter
char motorstat[] = "\033[9;64H";
char doorPerOpen[] = "\033[8;64H";
char processstat[] = "\033[7;21H";
char TopCallErrorPosition[] = "\033[10;2H";
char mpu6050Fail[] = "MPU6050 Init. Fail ";
char mxc400Fail[] = "MXC400Xxc Init. Fail ";
char openDoorsFailed[] = "OpenDoors Failure ";
char closeDoorsFailed[] = "CloseDoors Failure ";
char rcbuffer[128];
char *rx = &rcbuffer[0];
//char ibuffer[] = "+32767";
char CelciusUnits[] = {0xC2, 0xB0, 'C', '/', 0}; // "\xC2\xB0C/";
char FarhenheitUnits[] = {0xC2, 0xB0, 'F', 0}; // "\xC2\xB0F";
//unsigned char *ib = &ibuffer[0];
//int Samples[NumberofSamples];
double ADResult;
int T1Counter;
double x;
double dT, odT, idT;
char *buffer;
char thebuffer[100];
char Celsius[100];
int status;
//unsigned long DoorOpenTime; // Time required to open or close the door fully
double volts; // Temperature control knob
int T0Count, T1Count;
int T1Overflows;
double DoorDegree = 50.0;
int DoorMoved;
double Zalpha = 2.630330; // 99% Confidence level z
double sti = -100.0; // student t inside temperature difference
double sto = -100.0; // student t outside temperature difference
double ADPrevious_Inside;
double ADPrevious_Outside = 1000.0;
double stv = -100.0; // student t door voltage
double ADPrevious_DoorVoltage;
//double DoorDegreeOpenMax;
//double DoorPercentOpen = 0;
unsigned char *decimalPoint;
char decPt = '.';
//#define DOORS_NOT_CLOSED PORTAbits.RA4 // bit set means the switch is open
//#define DOORS_NOT_OPENED PORTAbits.RA5
double TMax, TOut, TIn;
int i;
int MPU6050SlaveAddress = 0x68;
int MXC4005XCSlaveAddress = 0x15;
int ESP6288ModuleAddress = 0x8;
double accelerometer_z, accelerometer_y, accelerometer_x; // variables for accelerometer raw data
double gyro_x, gyro_y, gyro_z; // variables for gyro raw data
double temperature; // variables for temperature data
extern double DoorInclination;
extern double OldDoorInclination;
extern double OpenedDoorInclination;
extern double ClosedDoorInclination;
extern int maxDoorCount;
returnStruct rts;
int main() {
// Declare Variables
returnStruct Result;
int j;
buffer = &thebuffer[0];
resetCause();
PCON1bits.nMEMV = 1; // No Memory Violation has occured.
// int DoorTime; // Time used to open the door to its current position
// int HeatDrain; // Calculated Heat loss if the door were fully opened
// int iDoorVoltage, oldiDoorVoltage;;
// int DoorLimit;
// Configure Hardware
#ifdef pic688
CMCON0 = 0x7; // All CxIN pins (13, 12, 10, 9; ISCPDAT, Vref/ISCPCLK,
// Motor Control, Max.Temp.) configured as digital and
// all comparators off.
ANSEL = 0; // Configure all A/D inputs as Digital -RC4 & RC5
// (RX/TX) have no AtoD circuitry.
TRISAbits.TRISA1 = 1; // Vref as Input
ANSELbits.ANS1 = 1; // Vref as Analog
TRISCbits.TRISC1 = 1; // Max Temp. Pot.
ANSELbits.ANS5 = 1; // Max. Temp. Pot. as Analog
TRISCbits.TRISC2 = 1; // Outside NTC as Input
ANSELbits.ANS6 = 1; // Outside NTC as Analog
TRISCbits.TRISC3 = 1; // Inside NTC as Input
ANSELbits.ANS7 = 1; // Inside NTC as Analog
ADCON0 = 0; // No Channel selected the ADC AN1 AN6 AN7 are Max.Temp. Pot., Outside Temp., Inside Temp. Channels of the sample and hold circuit
// OPTION_REGbits.nRAPU = 1; // Enable Pullups
TRISAbits.TRISA4 = 1; // RA4 is for an Upper Door Sensor (Closed)
ANSELbits.ANS3 = 0; // Digital I/O
IOCAbits.IOCA4 = 0; // Interrupt On Change Door Sensor
// WPUAbits.WPUA4 = 1; // Weak Pull Up RA4
TRISAbits.TRISA5 = 1; // RA5 is for IOC for lower door sensors (Opened)
IOCAbits.IOCA5 = 0; // Interrupt On Change Door Sensor
// WPUAbits.WPUA5 = 1; // Weak Pull Up RA5
TRISAbits.TRISA2 = 0; // Motor Control Open
TRISCbits.TRISC0 = 0; // Motor Control Closed
ANSELbits.ANS1 = 1; // Vref is an analog channel after PicKit is removed
// and after 3.3V appears on it.
// Oscillator to 8 MHz
OSCCONbits.SCS = 0;
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
while (!OSCCONbits.HTS) {
}; // Wait until the clock is stable
#else
while (!OSCSTATbits.HFOR) {
}; // Wait until the clock is stable
PMD3 = 0x47; // Disable everything in the register except AtoD
// PMD3bits.CMP1MD = 1; // Disable comparator 1
// PMD3bits.CMP2MD = 1; // Disable comparator 2
// PMD3bits.DAC1MD = 1; // Disable Digital to Analog Converter
// PMD3bits.ZCDMD = 1; // Disable Zero cross detector
PMD4 = 0xFF; // Disable Pulse width modulators
PMD5 = 0xFF; // Disable continuous wave modules
PMD6 = 0xff; // Disable serial devices
#ifdef UART
PMD6bits.U1MD = 0; // Enable EUSart
#else
PMD6bits.MSSP2MD = 0; // Enable
PMD6bits.U1MD = 0; // Enable uart
PMD6bits.MSSP1MD = 1; // Emab;e
#endif
PMD7 = 0xFF; // Disable
// AtoD Converters
TRISCbits.TRISC1 = 1; // Pot/knob - max temp analog
ANSELCbits.ANSC1 = 1; // analog Input
TRISCbits.TRISC2 = 1; // Outside NTC
ANSELCbits.ANSC2 = 1; // analog Input
TRISCbits.TRISC3 = 1; // Inside NTC
ANSELCbits.ANSC3 = 1; // analog Input
ANSELAbits.ANSA4 = 0; // Back Switch, Lower Pin (Was Lower Door switch - AtoD)
TRISAbits.TRISA4 = 1; // Now Output to gate ESP8266EX
ANSELAbits.ANSA5 = 0; // Upper Door switch
TRISAbits.TRISA5 = 1;
LATAbits.LATA2 = 0;
LATCbits.LATC0 = 0;
ANSELAbits.ANSA2 = 0;
TRISAbits.TRISA2 = 0; // Motor Control Output
ANSELCbits.ANSC0 = 0;
TRISCbits.TRISC0 = 0; // Motor Control Output
PORTAbits.RA2 = 0;
PORTCbits.RC0 = 0;
#endif
setupAtoD();
#ifdef pic688
setupTimer1();
#else
setupTimer0();
setupTimer1();
setupTimer2();
#endif
// setupIOC();
uartSetup();
// while(1) {
// TXout("Todd Matin Miller");
// INTCONbits.GIE=0;
// *rx = 0;
//
// rx = &rcbuffer[0];
// for (int q = 0; q< 10; q++)
// rcbuffer[q] = 0;
//// TXout("aaa");
// rx = &rcbuffer[0];
// INTCONbits.GIE = 1;
// __delay_ms(2000);
// }
// TXout("Initializing");
TXout(clearScreen);
TXout(intro1); // Title print
TXout(intro2);
TXout(intro3);
__delay_ms(100);
// I2C_Out(ESP6288ModuleAddress,"Initializing\n");
// I2C_Out(ESP6288ModuleAddress,clearScreen);
// I2C_Out(ESP6288ModuleAddress,intro1);
// I2C_Out(ESP6288ModuleAddress,intro2);
// I2C_Out(ESP6288ModuleAddress,intro3);
setupI2C();
#ifdef NEW_TILT_SENSOR
rts = init_MXC400();
if (rts.level0 < 0) {
TXout(processstat); // Position
TXout(mxc400Fail); // 0 is success, -1 is failure
buffer = formatString(rts.level0, 0, ",");
TXout(buffer);
buffer = formatString(rts.level1, 0, ";");
TXout(buffer);
__delay_ms(4000);
RESET();
}
#endif
rts = init_MPU6050();
if (rts.level0 != 0) {
TXout(TopCallErrorPosition); // Position
TXout(mpu6050Fail); // 0 is success, -1 is failure
buffer = formatString(rts.level0, 0, ",");
TXout(buffer);
buffer = formatString(rts.level1, 0, ";");
TXout(buffer);
// if(I2CBusCollision) { // I2C reset, the SSP2CON bits are all cleared - idle state
// I2CBusCollision = 0;
// BusCollision(13);
// }
__delay_ms(1000);
RESET();
}
// int Result;
// while(1) {
// Result = I2C_ReadMPU6050 (0x3B);
// accelerometer_x = (double) Result/16384.0; /// *9.80665;
// Result = I2C_ReadMPU6050 (0x3D);
// accelerometer_y = (double) Result/16384.0; // *9.80665;
// Result = I2C_ReadMPU6050 (0x3F);
// accelerometer_z = (double) Result/16384.0; // *9.80665;
// Result = I2C_ReadMPU6050 (0x41);
// temperature = Result/340.00+36.53;
// Result = I2C_ReadMPU6050 (0x43);
// gyro_x = Result/131.0;
// Result = I2C_ReadMPU6050 (0x45);
// gyro_y = Result/131.0;
// Result = I2C_ReadMPU6050 (0x47);
// gyro_z = Result/131.0;
// self_test_g();
// }
// while(1) {
// I2C_SetAddress ( ESP6288ModuleAddress ); // No write - No Device
// I2C_Out(ESP6288ModuleAddress,clearScreen);
// I2C_Out(ESP6288ModuleAddress,intro1);
// I2C_Out(ESP6288ModuleAddress,intro2);
// I2C_Out(ESP6288ModuleAddress,intro3);
// }
// int q = 0xB0;
// while(1) {
// x = 25.00;
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
//// *decimalPoint++ = 260;
// *decimalPoint = '\0';
// TXout(buffer);
//}
// while(1) {
// printDoorSwitches();
rts = setupDoors();
if ((rts.level0) < 0) {
__delay_ms(1000);
RESET();
}
int firstpass = 1;
// Operate the Ventilator
int iteration = 0;
while (1) {
// I2C_SetAddress ( ESP6288ModuleAddress ); // No write - No Device
// Time the doors
if (iteration == 250) {
INTCONbits.GIE = 0;
#ifdef USE_TX_ESP8266
TXout(clearScreen);
TXout(intro1); // Title print
TXout(intro2);
TXout(intro3);
#else
I2C_Out(ESP6288ModuleAddress,clearScreen);
I2C_Out(ESP6288ModuleAddress,intro1); // Title print
I2C_Out(ESP6288ModuleAddress,intro2);
I2C_Out(ESP6288ModuleAddress,intro3);
#endif
INTCONbits.GIE = 1;
// printDoorSwitches();
iteration = 0;
}
iteration++;
if (firstpass == 1) {
// INTCONbits.GIE = 0; // Disable interrupts
#ifdef USE_TX_ESP8266
TXout(processstat);
TXout((char *) "Acquire & Analyze");
#else
I2C_Out(ESP6288ModuleAddress,processstat);
I2C_Out(ESP6288ModuleAddress,(char *) "Acquire & Analyze");
#endif
// INTCONbits.GIE = 1; // enable interrupts
}
#if CHARACTERS_INPUT
// Received characters are printed
char *px;
if (rx != &rcbuffer[0]) {
for (px = &rcbuffer[0]; px < rx;) {
// if *px == 's' then different output
while (!PIR3bits.TX1IF) {
};
TXREG = *px++;
}
rx = &rcbuffer[0];
}
#endif // CHARACTERS_INPUT
// Get Maximum Temperature
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
TMax = AtoD(0b010001); // RC1 pin 9
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
x = TMax;
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &CelciusUnits[0]); // removed degree symbol
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0C/");
#endif
// buffer = ftoa(x,&status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'C';
// *decimalPoint++ = '/';
// *decimalPoint = '\0';
strcpy(Celsius, buffer);
x = x * 9.0 / 5.0 + 32.0;
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &FarhenheitUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0F");
#endif
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'F';
// *decimalPoint++ = ' ';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
// I2C_Out(Tmax);
#ifdef USE_TX_ESP8266
TXout(Tmax);
TXout(Celsius);
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,Tmax);
I2C_Out(ESP6288ModuleAddress,Celsius);
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
// Get Inside Temperature
// ENSURE THIS IS NOT A OUT Liner
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
TIn = AtoD(0b010011); // RC3 pin 7
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
x = TIn; // Proceed with the new value
if((x > 1000.0) || (x < -1000.0)) {
while(1)
SLEEP(1);
}
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &CelciusUnits[0]); // STopped Here 2/5/20
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0C/");
#endif
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'C';
// *decimalPoint++ = '/';
// *decimalPoint = '\0';
strcpy(Celsius, buffer);
x = x * 9.0 / 5.0 + 32.0;
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &FarhenheitUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0F");
#endif
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'F';
// *decimalPoint++ = ' ';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
#ifdef USE_TX_ESP8266
TXout(Tinside);
TXout(Celsius);
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,Tinside);
I2C_Out(ESP6288ModuleAddress,Celsius);
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
// Get Outside Temperature
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
TOut = AtoD(0b010010); // RC2 pin 8
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
x = TOut; // Proceed with the new value
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &CelciusUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0C/");
#endif
// buffer = ftoa(x,&status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'C';
// *decimalPoint++ = '/';
// *decimalPoint = '\0';
strcpy(Celsius, buffer);
x = x * 9.0 / 5.0 + 32.0;
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &FarhenheitUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0F");
#endif
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'F';
// *decimalPoint++ = ' ';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
#ifdef USE_TX_ESP8266
TXout(Toutside);
TXout(Celsius);
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,Toutside);
I2C_Out(ESP6288ModuleAddress,Celsius);
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
x = TIn - TOut;
dT = (double) (x);
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &CelciusUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0C/");
#endif
// buffer = ftoa(x,&status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'C';
// *decimalPoint++ = '/';
// *decimalPoint = '\0';
strcpy(Celsius, buffer);
x = x * 9.0 / 5.0;
#ifdef USE_TX_ESP8266
// Telnet can't print degree character
buffer = formatString(x, 2, &FarhenheitUnits[0]);
#else
buffer = formatString(x, 2, (char *) "\xC2\xB0F");
#endif
// buffer = ftoa(x, &status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 0xC2;
// *decimalPoint++ = 0xB0;
// *decimalPoint++ = 'F';
// *decimalPoint++ = ' ';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
#ifdef USE_TX_ESP8266
TXout(Tdiff);
TXout(Celsius);
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,Tdiff);
I2C_Out(ESP6288ModuleAddress,Celsius);
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
// Set the Ventilator Door Positions
// I2C_Out(lowDoorSw); // Position cursor for status message
buffer = formatString(OpenedDoorInclination, 3, "g");
// buffer = ftoa((float)(OpenedDoorVoltage/1000.0),&status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 'V';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
// I2C_Out(DoorPosVolts);
#ifdef USE_TX_ESP8266
TXout(lowDoorSw);
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,lowDoorSw);
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
buffer = formatString(ClosedDoorInclination, 3, "g");
// buffer = ftoa((float)(ClosedDoorVoltage/1000.0),&status);
// decimalPoint = strchr(buffer,decPt);
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// decimalPoint++;
// *decimalPoint++ = 'V';
// *decimalPoint = '\0';
// INTCONbits.GIE=0;
#ifdef USE_TX_ESP8266
TXout(upDoorSw); // Position cursor for status message labeled Open Door Status
TXout(buffer);
#else
I2C_Out(ESP6288ModuleAddress,upDoorSw); // Position cursor for status message labeled Open Door Status
I2C_Out(ESP6288ModuleAddress,buffer);
#endif
// INTCONbits.GIE=1;
TXout(processstat);
if (firstpass == 1)
TXout((char *) "First Pass ");
__delay_ms(1000);
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
if (TIn < TMax) { // Heat the Room
if (TIn < TOut) { //Heat is available
// Equillibrium
// Changes in Temperature
if ((sto > Zalpha) || (firstpass == 1)) { // The outside temperature is rising
// (99% confidence level).
// Open Door Slightly
// INTCONbits.GIE = 0;
#ifdef USE_TX_ESP8266
TXout(processstat);
TXout((char *) "Signif. Outdoor Inc.");
#else
I2C_Out(ESP6288ModuleAddress,processstat);
if (firstpass)
I2C_Out(ESP6288ModuleAddress,(char *) "First Pass ");
else
I2C_Out(ESP6288ModuleAddress,(char *) "Signif. Outdoor Inc.");
#endif
// INTCONbits.GIE = 1;
#ifdef POT
// Adjust Door Positions
DoorPosVolts = AtoD(0b000100); // RA4
iDoorVoltage = DoorPosVolts * 1000.0;
oldiDoorVoltage = 0;
DoorLimit = 0;
#endif
rts = openDoors(0);
if (rts.level0 < 0) {
TXout(TopCallErrorPosition); // Position
TXout(openDoorsFailed); // 0 is success, -1 is failure
buffer = formatString(rts.level0, 0, ", ");
TXout(buffer);
buffer = formatString(rts.level1, 0, ".");
TXout(buffer);
if(I2CBusCollision)
BusCollision(I2CBusCollision);
while(1)
SLEEP();
}
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
// int dDoorCount;
//
// // I2C_SetAddress ( MPU6050SlaveAddress); // Tilt Sensor Address.
// i = I2C_ReadMPU6050(ACCEL_X);
// if(i == -100)
// return -1; // I2C ERROR
// accelerometer_x = (double) i / 16384.0;
//// accelerometer_z = (double) I2C_ReadMPU6050 (ACCEL_Z)/16384.0; // *9.80665; ***
// i = 0;
// do {
// j = (double) I2C_ReadMPU6050(TEMPERATURE);
// if(j == -100)
// return -1;
// temperature = (double) j / 340.00 + 36.53;
// i++;
// } while((temperature == 36.53) && i < 10);
// if(i > 9)
// return -1; // I2C ERROR
//
//// while((temperature = I2C_ReadMPU6050 (0x41)/340.00+36.53) == 36.53 ) {
//// temperature = 0.0;
//// }; // Result = I2C_ReadMPU6050 (0x43);
//// gyro_x = I2C_ReadMPU6050 (0x43)/131.0;
// if(temperature > 20.0)
// maxDoorCount = 6;
// else
// maxDoorCount = 12;
// dDoorCount = 0;
// i = (double) I2C_ReadMPU6050(ACCEL_Z);
// if(i <= -100)
// return -1;
// double DoorInclination = (double) i / 16384.0;
//// double DoorInclination = (double) I2C_ReadMPU6050 (ACCEL_Z)/16384.0;
// double OldDoorInclination = -1.0;
//// while ((iDoorVoltage > OpenedDoorVoltage) && (DoorLimit < 5)) { // Not completely opened
// while((DoorInclination < OpenedDoorInclination ) && dDoorCount < maxDoorCount) {
//// if((oldiDoorVoltage - iDoorVoltage) < 10)
//// DoorLimit++;
//// I2C_SetAddress ( ESP6288ModuleAddress);
//#ifdef USE_TX_ESP8266
// TXout(motorstat);
// TXout((char *) "Opening");
//#else
// I2C_Out(motorstat);
// I2C_Out((char *) "Opening");
//#endif
// DoorMoved = 0;
// T1Count = 2; // add 1 for sub degrees(next statement)
// LATAbits.LATA2 = 1; // open the doors
// T1CONbits.ON = 1; // Turn on the timer
// while (!DoorMoved) {
// }; // Timer1 will turn off both doors when time is ended
// T1CONbits.ON = 0; // Turn off the timer
// // Print the New Door Voltage
// // I2C_SetAddress ( MPU6050SlaveAddress);
// do {
// i = I2C_ReadMPU6050(GYRO_Z);
// if(i <= -100)
// return -1;
// gyro_x = (double) i/131.0;
// } while (gyro_x > 1.000);
//// unsigned int toolong;
//// while ((gyro_x = I2C_ReadMPU6050 (GYRO_Z)/131.0 > 1.000) && (toolong < 0xFE)) { __delay_ms(1); toolong++; } // wait til it stops shaking
// // get the new inclination
// OldDoorInclination = DoorInclination;
// i = I2C_ReadMPU6050(ACCEL_Z);
// if(i == -100)
// return -1;
// DoorInclination = (double) i / 16384.0;
//// DoorInclination = 0.0;
//// for (i = 0; i < 10; i++)
//// DoorInclination += (double) (I2C_ReadMPU6050 (ACCEL_X)/16384.0);
//// DoorInclination /= 10.0;
// // If the change is not significant
// if(OldDoorInclination <= DoorInclination) //* 0.0302) && OldDoorInclination > DoorInclination) {
//// if(OldDoorInclination > DoorInclination)
// dDoorCount++;
// }
//
//
//// DoorPosVolts = AtoD(0b000100); // Get new door position
//// iDoorVoltage = DoorPosVolts * 1000.0;
//// int oldiDoorVoltage = iDoorVoltage;
// // I2C_SetAddress ( ESP6288ModuleAddress); // Tilt Sensor Address
//#ifdef USE_TX_ESP8266
// TXout(doorOFC); // Position cursor for status message
// buffer = formatString(DoorInclination, 3, (char *) "g ");
// TXout(buffer);
//#else
// I2C_Out(doorOFC); // Position cursor for status message
// buffer = formatString(DoorInclination, 3, (char *) "g");
// I2C_Out(buffer);
//#endif
//// INTCONbits.GIE = 1;
//
// // I2C_SetAddress ( ESP6288ModuleAddress );
// #ifdef USE_TX_ESP8266
// TXout(motorstat);
// TXout((char *) "Stopped");
//#else //
// I2C_Out(motorstat);
// I2C_Out((char *) "Stopped");
//#endif
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
} else if ((sto < 0.0) && (sto < (-1.0 * Zalpha)) && sto != -100.0) { // The outside temperature is falling
// (99% confidence level).
// I2C_SetAddress ( ESP6288ModuleAddress );
// INTCONbits.GIE = 0;
#ifdef USE_TX_ESP8266
TXout(processstat);
TXout((char *) "Signif. Outdoor Dec. ");
#else
I2C_Out(ESP6288ModuleAddress,processstat);
I2C_Out(ESP6288ModuleAddress,(char *) "Signif. Outdoor Dec.");
#endif
// INTCONbits.GIE = 1;
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
#ifdef _OLD_CODE_1
DoorPosVolts = AtoD(0b000100);
iDoorVoltage = DoorPosVolts * 1000.0;
if (iDoorVoltage < ClosedDoorVoltage) {
TXout(motorstat);
TXout((char *) "Closing");
DoorMoved = 0;
T1Count = 20; // add 1 for sub degrees(next statement)
LATCbits.LATC0 = 1; // close the doors
T1CONbits.ON = 1; // Turn on the timer
while (!DoorMoved) {
}; // Timer1 will turn off both doors when time is ended
T1CONbits.ON = 0; // Turn off the timer
DoorPosVolts = AtoD(0b000100); // Get new door position
iDoorVoltage = DoorPosVolts * 1000.0;
TXout(doorOFC); // Position cursor for status message
buffer = formatString(DoorPosVolts, 3, (char *) "V");
INTCONbits.GIE = 0;
TXout(buffer);
INTCONbits.GIE = 1;
}
TXout(motorstat);
TXout((char *) "Stopped");
#endif
}
} else { // Heat is not available (In > TOut & TOut < TIn) (Tin < Tout)
// I2C_SetAddress ( ESP6288ModuleAddress );
// INTCONbits.GIE=0;
#ifdef USE_TX_ESP8266
TXout(processstat);
TXout((char *) "No Heat to Heat With");
#else
I2C_Out(ESP6288ModuleAddress,processstat);
I2C_Out(ESP6288ModuleAddress,(char *) "No Heat to Heat with");
#endif
if((firstpass <-3) || (firstpass > 1)) {
while(1)
SLEEP();
}
rts = closeDoors(0);
if(rts.level0)
return -1;
//// INTCONbits.GIE=1;
// // I2C_SetAddress ( MPU6050SlaveAddress); // Tilt Sensor Address.
// i = I2C_ReadMPU6050(ACCEL_Z);
// if(i == -100)
// return -1;
// DoorInclination = (double) i / 16384.0;
// DoorInclination = (double) I2C_ReadMPU6050 (ACCEL_Z)/16384.0;
//// DoorPosVolts = AtoD(0b000100);
//// iDoorVoltage = DoorPosVolts * 1000.0;
//// while ((iDoorVoltage < ClosedDoorVoltage ) && (fabs(stv) > fabs(Zalpha)) ){
// while ((DoorInclination > (ClosedDoorInclination + 0.02) ) && (fabs(stv) > fabs(Zalpha)) ){
//// I2C_SetAddress ( ESP6288ModuleAddress );
//#ifdef USE_TX_ESP8266
// TXout(motorstat);
// TXout((char *) "Closing");
//#else
// I2C_Out(motorstat);
// I2C_Out((char *) "Closing");
//#endif
// DoorMoved = 0;
// T1Count = 2; // add 1 for sub degrees(next statement)
// LATCbits.LATC0 = 1; // close the doors
// T1CONbits.ON = 1; // Turn on the timer
// while(!DoorMoved) {}; // Timer1 will turn off both doors when time is ended
// T1CONbits.ON = 0; // Turn off the timer
// // I2C_SetAddress ( MPU6050SlaveAddress);
// unsigned int toolong;
// while ((gyro_x = I2C_ReadMPU6050 (GYRO_Z)/131.0 > 1.000) && (toolong < 0xFE)) { __delay_ms(1); toolong++; } // wait til it stops shaking
// // get the new inclination
// OldDoorInclination = DoorInclination;
// i = I2C_ReadMPU6050(ACCEL_Z);
// if(i == -100)
// return -1;
// DoorInclination = (double) i / 16384.0;
//
//// DoorInclination = 0.0;
//// for (i = 0; i < 10; i++)
//// DoorInclination += (double) I2C_ReadMPU6050 (0x3F)/16384.0;
//// DoorInclination /= 10.0;
//// DoorPosVolts = AtoD(0b000100); // Get new door position
//// iDoorVoltage = DoorPosVolts * 1000.0;
//// I2C_SetAddress ( ESP6288ModuleAddress );
//
// buffer = formatString(DoorInclination,3,(char *) "g");
//// INTCONbits.GIE=0;
//#ifdef USE_TX_ESP8266
// TXout(doorOFC); // Position cursor for status message
// TXout(buffer);
//#else
// I2C_Out(doorOFC); // Position cursor for status message
// I2C_Out(buffer);
//#endif
//// INTCONbits.GIE=1;
// }