-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPS.c
1168 lines (1035 loc) · 26.5 KB
/
EPS.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
// EPS_Software.c
//Libraries and Global Initialization
#define F_CPU 16000000UL
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/twi.h>
#include <avr/interrupt.h>
#include <util/crc16.h>
#define CLK1 PC4
#define CS1 PC5
#define DIN1 PC6
#define DOUT1 PC7
#define CLK2 PC0
#define CS2 PC1
#define DIN2 PC2
#define DOUT2 PC3
enum
{
X,
Y,
Z
};
//******------------SSP/I2C------------******//
uint8_t data_send = 0x00, data_receive = 0x00, data_receive_main = 0x00, data_send_main = 0x00;
uint8_t I2C_ADDR = 0x20;
// Function to initialize slave
void TWI_init_slave(void)
{
// Fill slave address to TWAR
TWBR = 1;
//Setting up the SCL frequency by writing a value in TWBR
TWSR |= (0 << TWPS1) | (0 << TWPS0);
//Fscl= Fcpu/(16+2*(TWBR)*(4^TWPS))
TWAR = 0x20;
//The first seven bits indicate the slave address
TWCR |= (1 << TWINT);
TWCR |= (1 << TWEN) | (1 << TWEA) | (0 << TWSTA) | (0 << TWSTO);
//Enabling Acknowledge function
while (!(TWCR & (1 << TWINT)))
;
//Wait for the interrupt to be cleared as it will indicate the successful reception
while (TWCR & (0xF8) != (0x60))
;
//Checking if the self-address+W has been received and ACK has been sent
}
void TWI_write_slave(void) // Function to write data
{
// Fill TWDR register with the data to be sent
TWDR = data_send;
// Enable TWI, Clear TWI interrupt flag
TWCR = (1 << TWEN) | (1 << TWINT);
// Wait for the acknowledgement
while ((TWSR & 0xF8) != 0xC0)
;
}
// Function to match the slave address and slave
void TWI_match_write_slave(void)
{
// Loop till correct acknowledgement have been received
while ((TWSR & 0xF8) != 0xA8)
{
// Get acknowledgment, Enable TWI, Clear TWI interrupt flag
TWCR = (1 << TWEA) | (1 << TWEN) | (1 << TWINT);
while (!(TWCR & (1 << TWINT)))
; // Wait for TWINT flag
}
}
void TWI_read_slave(void)
{
// Clear TWI interrupt flag,Get acknowledgement, Enable TWI
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
// Wait for TWINT flag
while (!(TWCR & (1 << TWINT)))
;
// Wait for acknowledgement
while ((TWSR & 0xF8) != 0x80)
;
// Get value from TWDR
data_receive = TWDR;
}
//Function to match the slave address and slave direction bit(read)
void TWI_match_read_slave(void) //Function to match the slave address and slave dirction bit(read)
{
while ((TWSR & 0xF8) != 0x60) // Loop till correct acknoledgement have been received
{
// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
TWCR = (1 << TWEA) | (1 << TWEN) | (1 << TWINT);
while (!(TWCR & (1 << TWINT)))
; // Wait for TWINT flag
}
}
int setup(void)
{
TWI_init_slave(); // Function to initialize slave
while (1)
{
//Function to match the slave address and slave direction bit(read)
TWI_match_read_slave();
// Function to read data
TWI_read_slave();
//Function to match the slave address and slave direction bit(write)
TWI_match_write_slave();
// Function to write data
TWI_write_slave();
}
}
//1 FEND 0xc0
//2 Destination 8 bits
//3 Source 8 bits
//4 Type 2 bits
//5 Type 6 bits
//6 Data 8 bits
//7 CRC 16 bits
//8 CRC 16 bits
//9 FEND 0xc0
//FEND in raw data then
//FESC 0xDB
//TFEND 0xDC
//FESC in raw data then
//FESC 0xDB
//TFESC 0xDD
void SSP_I2C_receive()
{
uint16_t CRC = 0;
uint8_t CRCH = 0;
uint8_t CRCL = 0;
uint8_t temp_data_crc1 = 0;
uint16_t temp_data_crc2 = 0;
uint8_t i = 0;
uint8_t FLAG_FEND = 0;
uint8_t FLAG_DEST = 0;
uint8_t FLAG_SRC = 0;
uint8_t FLAG_TYPE = 0;
uint8_t FLAG_DATA = 0;
uint8_t FLAG_CRC1 = 0;
uint8_t FLAG_CRC2 = 0;
for (i = 0; i < 8; i++)
{
setup();
switch (i)
{
case 0:
if (data_receive == 0xc0)
{
FLAG_FEND = 1; //else error;
}
break;
case 1:
if (data_receive == I2C_ADDR)
{
FLAG_DEST = 1; //EPS is DEST //else error;
}
break;
case 2:
if (data_receive == 0x03)
{
FLAG_SRC = 1; //OBC is SRC //else error;
}
break;
case 3:
if (data_receive == 0x70)
{
FLAG_TYPE = 1; //TYPE Command is Correct //else error;
}
case 4:
if (data_receive == 0xf1)
{
temp_data_crc1 = data_receive;
data_receive_main = data_receive;
FLAG_DATA = 1; //DATA Command is Correct //else error;
}
else if (data_receive = 0xdd)
{
data_receive = 0xdb;
temp_data_crc1 = data_receive;
FLAG_DATA = 1; //else error;
}
else if (data_receive = 0xdc)
{
data_receive = 0xc0;
temp_data_crc1 = data_receive;
FLAG_DATA = 1; //else error;
}
break;
case 5:
CRC = CRCL | data_receive;
CRC = 8 << CRC;
setup();
CRC = CRCH | data_receive;
CRC = _crc_ccitt_update(CRC, temp_data_crc1);
if (CRC == 0)
FLAG_CRC1 = 1; //else error;
CRC = temp_data_crc2;
break;
case 6:
CRC = CRCL | data_receive;
CRC = 8 << CRC;
setup();
CRC = CRCH | data_receive;
CRC = _crc_ccitt_update(CRC, temp_data_crc2); //CRC FROM CRC itself need work
if (CRC == 0)
FLAG_CRC2 = 1; //else error;
break;
case 7:
if (data_receive == 0xc0 && FLAG_FEND == 1)
{
FLAG_FEND = 0; //else error; //sentence is arbitrary doesn't effect
}
break;
}
}
}
void SSP_I2C_send()
{
uint16_t CRC = 0;
uint8_t CRCH = 0;
uint8_t CRCL = 0;
uint8_t temp_data_crc1 = 0;
uint16_t temp_data_crc2 = 0;
uint8_t i = 0;
for (i = 0; i < 8; i++)
{
setup();
switch (i)
{
case 0:
data_send = 0xc0;
break;
case 1:
data_send = 0x03; //OBC is DEST
break;
case 2:
data_send = I2C_ADDR; //EPS is SRC
break;
case 3:
data_send = 0x01; //TYPE Command
break;
case 4:
data_send = 0x02; //Telemetry Data according to type
data_send_main = data_send;
if (data_send = 0xc0)
{
data_send = 0xdb;
setup();
data_send = 0xdc;
}
else if (data_send = 0xdb)
{
data_send = 0xdb;
setup();
data_send = 0xdd;
}
temp_data_crc1 = data_send;
break;
case 5:
CRC = _crc_ccitt_update(0xffff, temp_data_crc1);
CRCL = CRC & 0xff;
CRCH = CRC >> 8;
data_send = CRCL;
setup();
data_send = CRCH;
CRC = temp_data_crc2;
break;
case 6:
CRC = _crc_ccitt_update(0xffff, temp_data_crc2); //CRC FROM CRC itself need work
CRCL = CRC & 0xff;
CRCH = CRC >> 8;
data_send = CRCL;
setup();
data_send = CRCH;
break;
case 7:
data_send = 0xc0;
break;
}
}
}
//******-------------- -END------------------******//
//******-----------ADC_INTERNAL------------******//
void ADC_init()
{
ADMUX |= (1 << REFS0); // AVcc with external capacitor at AREF
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}
//******************************************************//
uint8_t ADC_read(uint8_t ch)
{
if (ch < 8)
{
ch = ch & 0b00000111;
ADMUX |= ch;
ADCSRA |= (1 << ADSC); // start conversion
ADCSRB &= 0b11110111;
while (!(ADCSRA & (1 << ADIF)))
; // waiting for ADIF, conversion complete
ADCSRA |= (1 << ADIF); // clearing of ADIF, it is done by writing 1 to it
uint8_t x = ADCL;
ADMUX &= 0b11111000;
return x;
}
else
{
ch = ch - 8;
ch = ch & 0b00000111;
ADMUX |= ch;
ADCSRB |= 0b00001000;
ADCSRA |= (1 << ADSC);
// start conversion
while (!(ADCSRA & (1 << ADIF)))
; // waiting for ADIF, conversion complete
ADCSRA |= (1 << ADIF); // clearing of ADIF, it is done by writing 1 to it
uint8_t x = ADCL;
ADMUX &= 0b11111000;
return x;
}
}
//******----------------END------------------******//
//******------------EXTERNAL_ADC2------------******//
int readADC_2(uint8_t adcnum)
{
if ((adcnum > 7) || (adcnum < 0))
return -1; // Wrong adc address return -1
PORTC |= (1 << CS2);
PORTC &= ~(1 << CLK2); // low
PORTC &= ~(1 << CS2);
uint8_t commanDOUT1 = adcnum;
commanDOUT1 |= 0x18; // # start bit + single-ended bit
commanDOUT1 <<= 3; // # we only need to send 5 bits here
uint8_t i;
for (i = 0; i < 5; i++)
{
if (commanDOUT1 & 0x80)
PORTC |= (1 << DIN2);
else
PORTC &= ~(1 << DIN2);
commanDOUT1 <<= 1;
PORTC |= (1 << CLK2);
PORTC &= ~(1 << CLK2);
}
uint8_t adcout = 0;
// read in one empty bit, one null bit and 10 ADC bits
i = 0;
for (i = 0; i < 12; i++)
{
PORTC |= (1 << CLK2);
PORTC &= ~(1 << CLK2);
adcout <<= 1;
if (PINC & 0b00001000) //CHECKING DOUT11111111
adcout |= 0x1;
}
PORTC |= (1 << CS2);
adcout >>= 1; // # first bit is 'null' so drop it
return adcout;
}
//******-------------- -END------------------******//
//******------------EXTERNAL_ADC1------------******//
int readADC_1(uint8_t adcnum)
{
if ((adcnum > 7) || (adcnum < 0))
return -1; // Wrong adc address return -1
PORTC |= (1 << CS1);
PORTC &= ~(1 << CLK1); // low
PORTC &= ~(1 << CS1);
int commanDOUT1 = adcnum;
commanDOUT1 |= 0x18; // # start bit + single-ended bit
commanDOUT1 <<= 3; // # we only need to send 5 bits here
uint8_t i;
for (i = 0; i < 5; i++)
{
if (commanDOUT1 & 0x80)
PORTC |= (1 << DIN1);
else
PORTC &= ~(1 << DIN1);
commanDOUT1 <<= 1;
PORTC |= (1 << CLK1);
PORTC &= ~(1 << CLK1);
}
uint8_t adcout = 0;
// read in one empty bit, one null bit and 10 ADC bits
for (uint8_t i = 0; i < 12; i++)
{
PORTC |= (1 << CLK1);
PORTC &= ~(1 << CLK1);
adcout <<= 1;
if (PINC & 0b10000000) //CHECKING DOUT11111111
adcout |= 0x1;
}
PORTC |= (1 << CS1);
adcout >>= 1; // # first bit is 'null' so drop it
return adcout;
}
//******-------------- -END------------------******//
//16-bit Timer/Counter (Timer/Counter 1, 3, 4, and 5)
//******------------PWM Frequency------------******//
void setPwmFrequency(uint16_t divisor)
{
uint8_t mode;
{
switch (divisor)
{
case 1:
mode = 0x01;
break;
case 8:
mode = 0x02;
break;
case 32:
mode = 0x03;
break;
case 64:
mode = 0x04;
break;
case 128:
mode = 0x05;
break;
case 256:
mode = 0x06;
break;
case 1024:
mode = 0x07;
break;
default:
return;
TCCR3B = TCCR3B & 0b11111000 | mode;
// set frequency PWM
TCCR3A |= (1 << WGM33) | (1 << WGM32) | (1 << WGM31) | (1 << WGM30);
// set fast PWM
}
}
}
//******------------PWM------------******//
void PWM_(uint8_t d, uint8_t l)
{
if (l == X)
{
DDRE |= (1 << PE3);
// PB3 is now an output
TCCR3A |= (1 << COM3A1);
// set none-inverting mode
OCR3A = d;
// set PWM duty cycle
}
else if (l == Y)
{
DDRE |= (1 << PE4);
// PB4 is now an output
TCCR3A |= (1 << COM3B1);
// set none-inverting mode
OCR3B = d;
// set PWM duty cycle
}
else if (l == Z)
{
DDRE |= (1 << PE5);
// PB4 is now an output
TCCR3A |= (1 << COM3C1);
// set none-inverting mode
OCR3C = d;
// set PWM duty cycle
}
}
//******-------------- -END------------------******//
//*******************MAIN CORE***********************//
int main(void)
{
//*******************LOADS CONTROL INITALIZATION***********************//
TWAR = 0b10010000;
SSP_I2C_receive();
ADC_init();
DDRA = 0xff;
DDRC = 0b01110111; // SETTING APPROPRIATE INPUTS AND OUTPUTS
//*******************MPPT INITALIZATION********************************//
/*
Pin ADC internal numbers:
IIN_X is 8 input
IIN_Y is 9 input
IIN_Z is 10 input
Pin ADC1 external numbers:
M_SA_X is 7 output
Pin ADC2 external numbers:
M_SA_Y is 0 output
M_SA_Z is 1 output
ISA_X is 2 output
ISA_Y is 3 output
ISA_Z is 4 output
VDC_X is 5 input
VDC_Y is 6 input
VDC_Z is 7 input
*/
float Iout;
float Vout;
float nextpower;
float nextVin;
float nextIin;
uint8_t duty;
int stepsize;
int valueread_vout;
int valueread_iout;
int sample;
uint8_t pinread_vout;
uint8_t pinread_iout;
int interval;
float stepsize_factor;
uint8_t LOAD;
const uint8_t pinread_vin_x = 5; //VDC_X is 5 input
const uint8_t pinread_vout_x = 7; //M_SA_X is 7 output only ADC1 external
const uint8_t pinread_iin_x = 8; //IIN_X is 8 input
const uint8_t pinread_iout_x = 2; //ISA_X is 2 output
const uint8_t pinread_vin_y = 6; //VDC_Y is 6 input
const uint8_t pinread_vout_y = 0; //M_SA_Y is 0 output
const uint8_t pinread_iin_y = 9; //IIN_Y is 9 input
const uint8_t pinread_iout_y = 3; //ISA_Y is 3 output
const uint8_t pinread_vin_z = 7; //VDC_Z is 7 input
const uint8_t pinread_vout_z = 1; //M_SA_Z is 1 output
const uint8_t pinread_iin_z = 10; //IIN_Z is 10 input
const uint8_t pinread_iout_z = 4; //ISA_Z is 4 output
uint8_t duty_x = 100;
uint16_t divisor_x = 1;
uint8_t duty_y = 100;
uint16_t divisor_y = 1;
uint8_t duty_z = 100;
uint16_t divisor_z = 1;
int sample_x = 1;
int interval_x = 1000;
int sample_y = 1;
int interval_y = 1000;
int sample_z = 1;
int interval_z = 1000;
int valueread_vin_x = 200;
int valueread_vout_x = 200;
int valueread_iin_x = 200;
int valueread_iout_x = 200;
int valueread_vin_y = 200;
int valueread_vout_y = 200;
int valueread_iin_y = 200;
int valueread_iout_y = 200;
int valueread_vin_z = 200;
int valueread_vout_z = 200;
int valueread_iin_z = 200;
int valueread_iout_z = 200;
float Iin_x = 1.0;
float nextIin_x = 1.0;
float prevIin_x = 2.0;
float Iin_y = 1.0;
float nextIin_y = 1.0;
float prevIin_y = 2.0;
float Iin_z = 1.0;
float nextIin_z = 1.0;
float prevIin_z = 2.0;
float Vin_x = 2.0;
float prevVin_x = 3.0;
float nextVin_x = 1.0;
float Vin_y = 2.0;
float prevVin_y = 3.0;
float nextVin_y = 1.0;
float Vin_z = 2.0;
float prevVin_z = 3.0;
float nextVin_z = 1.0;
float Vout_x = 1.0;
float Iout_x = 1.0;
float Vout_y = 1.0;
float Iout_y = 1.0;
float Vout_z = 1.0;
float Iout_z = 1.0;
float Pin_x = 2.0;
float prevpower_x = 3.0;
float nextpower_x = 1.0;
float Pin_y = 2.0;
float prevpower_y = 3.0;
float nextpower_y = 1.0;
float Pin_z = 2.0;
float prevpower_z = 3.0;
float nextpower_z = 1.0;
float Pout_x = 1.0;
float Pout_y = 1.0;
float Pout_z = 1.0;
float offsetvoltage_i = 2.5;
float scale_i = 1.75;
float scale_v = 0.5;
float stepsize_factor_x = 1.5;
int stepsize_x = 1;
float stepsize_factor_y = 1.5;
int stepsize_y = 1;
float stepsize_factor_z = 1.5;
int stepsize_z = 1;
setPwmFrequency(1);
//****************************END**************************************//
//*********************MAIN LOOP*************************//
while (1)
{
//*********************CORE LOAD CONTROL*************************//
SSP_I2C_receive();
if (TWDR == 'J')
{
uint8_t i = 0;
for (i = 0; i < 8; i++)
{
int val = 0;
uint8_t ADC_ext = 0;
SSP_I2C_receive();
val = readADC_1(i);
ADC_ext = val / 4;
data_send_main = ADC_ext;
}
}
if (TWDR == 'B')
{
uint8_t i = 0;
for (i = 0; i < 16; i++)
{
SSP_I2C_receive();
ADC_init();
ADC_read(i);
data_send_main = ADCL;
}
}
else if (TWDR == 'F')
{
uint8_t i = 0;
for (i = 0; i < 24; i++)
{
SSP_I2C_receive();
ADC_init();
ADC_read(i);
data_send_main = ADCL;
}
i = 0;
for (i = 0; i < 8; i++)
{
int val = 0;
uint8_t ADC_ext = 0;
SSP_I2C_receive();
val = readADC_1(i);
ADC_ext = val / 4;
data_send_main = ADC_ext;
}
}
else if (TWDR == 'G')
{
uint8_t i = 0;
for (i = 0; i < 16; i++)
{
SSP_I2C_receive();
ADC_init();
ADC_read(i);
data_send_main = ADCH;
}
i = 0;
for (i = 0; i < 8; i++)
{
int val = 0;
uint8_t ADC_ext = 0;
SSP_I2C_receive();
val = readADC_1(i);
ADC_ext = val / 4;
data_send_main = ADC_ext;
}
i = 0;
for (i = 0; i < 8; i++)
{
int val = 0;
uint8_t ADC_ext = 0;
val = readADC_2(i);
ADC_ext = val / 4;
data_send_main = ADC_ext;
}
}
//////***********Digital pins**********//////
else if (TWDR == 'R') //*******PA0*******//ON
{
PORTA |= (1 << PA0);
}
else if (TWDR == 'r') //*******PA0*******//Off
{
PORTA &= ~(1 << PA0);
}
//***********************************//
else if (TWDR == 'L')
{
PORTA |= (1 << PA1);
}
else if (TWDR == 'l')
{
PORTA &= ~(1 << PA1);
}
//***********************************//
else if (TWDR == 'K')
{
PORTA |= (1 << PA2);
}
else if (TWDR == 'k')
{
PORTA &= ~(1 << PA2);
}
//***********************************//
else if (TWDR == 'Q')
{
PORTA |= (1 << PA3);
}
else if (TWDR == 'q')
{
PORTA &= ~(1 << PA3);
}
//***********************************//
else if (TWDR == 'W')
{
PORTA |= (1 << PA4);
}
else if (TWDR == 'w')
{
PORTA &= ~(1 << PA4);
}
//***********************************//
else if (TWDR == 'M')
{
PORTA |= (1 << PA5);
}
else if (TWDR == 'm')
{
PORTA &= ~(1 << PA5);
}
//***********************************//
else if (TWDR == 'N')
{
PORTA |= (1 << PA6);
}
else if (TWDR == 'n')
{
PORTA &= ~(1 << PA6);
}
//***********************************//
else if (TWDR == 'T')
{
PORTA |= (1 << PA7);
}
else if (TWDR == 't')
{
PORTA &= ~(1 << PA7);
}
//***********************************//
else if (TWDR == 'H')
{
PORTA = 0xff;
}
else if (TWDR == 'h')
{
PORTA = 0x00;
}
//***********************************//
//****************************END**************************************//
//*********************CORE MPPT CONTROL*************************//
ADC_init();
//Line_X
valueread_vin_x = 0;
valueread_vout_x = 0;
valueread_iin_x = 0;
valueread_iout_x = 0;
//Line_Y
valueread_vin_y = 0;
valueread_vout_y = 0;
valueread_iin_y = 0;
valueread_iout_y = 0;
//Line_Z
valueread_vin_z = 0;
valueread_vout_z = 0;
valueread_iin_z = 0;
valueread_iout_z = 0;
for (int i = 0; i < sample_x; i++)
{
//Line_X
valueread_vin_x += readADC_2(pinread_vin_x); // read and accumulate the solar panel voltage
_delay_us(interval_x);
valueread_vout_x += readADC_1(pinread_vout_x); // read and accumulate the load voltage
_delay_us(interval_x);
ADC_init();
valueread_iin_x += ADC_read(pinread_iin_x); // read and accumulate the solar panel current
_delay_us(interval_x);
valueread_iout_x += readADC_2(pinread_iout_x); // read and accumulate the load voltage
_delay_us(interval_x);
}
for (int i = 0; i < sample_y; i++)
{
//Line_Y
valueread_vin_y += readADC_2(pinread_vin_y); // read and accumulate the solar panel voltage
_delay_us(interval_y);
valueread_vout_y += readADC_2(pinread_vout_y); // read and accumulate the load voltage
_delay_us(interval_y);
ADC_init();
valueread_iin_y += ADC_read(pinread_iin_y); // read and accumulate the solar panel current
_delay_us(interval_y);
valueread_iout_y += readADC_2(pinread_iout_y); // read and accumulate the load voltage
_delay_us(interval_y);
}
for (int i = 0; i < sample_z; i++)
{
//Line_Z
valueread_vin_z += readADC_2(pinread_vin_z); // read and accumulate the solar panel voltage
_delay_us(interval_z);
valueread_vout_z += readADC_2(pinread_vout_z); // read and accumulate the load voltage
_delay_us(interval_z);
ADC_init();
valueread_iin_z += ADC_read(pinread_iin_z); // read and accumulate the solar panel current
_delay_us(interval_z);
valueread_iout_z += readADC_2(pinread_iout_z); // read and accumulate the load voltage
_delay_us(interval_z);
}
///////////////////////
//Line_X
Vin_x = ((valueread_vin_x * 5) / (1024.0 * sample_x)) / scale_v;
Vout_x = ((valueread_vout_x * 5) / (1024.0 * sample_x)) / scale_v;
Iin_x = (valueread_iin_x / (1024.0 * sample_x)) * (5 / scale_i);
Iout_x = (valueread_iout_x / (1024.0 * sample_x)) * (5 / scale_i);
//Line_Y
Vin_y = ((valueread_vin_y * 5) / (1024.0 * sample_y)) / scale_v;
Vout_y = ((valueread_vout_y * 5) / (1024.0 * sample_y)) / scale_v;
Iin_y = (valueread_iin_y / (1024.0 * sample_y)) * (5 / scale_i);
Iout_y = (valueread_iout_y / (1024.0 * sample_y)) * (5 / scale_i);
//Line_Z
Vin_z = ((valueread_vin_z * 5) / (1024.0 * sample_z)) / scale_v;
Vout_z = ((valueread_vout_z * 5) / (1024.0 * sample_z)) / scale_v;
Iin_z = (valueread_iin_z / (1024.0 * sample_z)) * (5 / scale_i);
Iout_z = (valueread_iout_z / (1024.0 * sample_z)) * (5 / scale_i);
stepsize_x = ((abs(Vout - 7.5) * stepsize_factor_x)) + 1;
stepsize_y = ((abs(Vout - 7.5) * stepsize_factor_y)) + 1;
stepsize_z = ((abs(Vout - 7.5) * stepsize_factor_z)) + 1;
//Line_X
Pout_x = Vout_x * Iout_x;
//Line_Y
Pout_y = Vout_y * Iout_y;
//Line_Z
Pout_z = Vout_z * Iout_z;
//Line_X
Pin_x = Vin_x * Iin_x;
nextpower_x = Pin_x - prevpower_x;
prevpower_x = Pin_x;
nextIin_x = Iin_x - prevIin_x;
prevIin_x = Iin_x;
nextVin_x = Vin_x - prevVin_x;
prevVin_x = Vin_x;
//Line_Y
Pin_y = Vin_y * Iin_y;
nextpower_y = Pin_y - prevpower_y;
prevpower_y = Pin_y;
nextIin_y = Iin_y - prevIin_y;
prevIin_y = Iin_y;
nextVin_y = Vin_y - prevVin_y;
prevVin_y = Vin_y;
//Line_Z
Pin_z = Vin_z * Iin_z;
nextpower_z = Pin_z - prevpower_z;
prevpower_z = Pin_z;
nextIin_z = Iin_z - prevIin_z;
prevIin_z = Iin_z;
nextVin_z = Vin_z - prevVin_z;
prevVin_z = Vin_z;
//////////////////////
for (int LINES = 1; LINES < 4; LINES++)
{
switch (LINES)
{
case 1: //Line_X
Iout = Iout_x;
Vout = Vout_x;
nextpower = nextpower_x;
nextVin = nextVin_x;
nextIin = nextIin_x;
duty = duty_x;