-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNFC_Optogenetics_HeadMounted.ino
873 lines (741 loc) · 29.6 KB
/
NFC_Optogenetics_HeadMounted.ino
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
/*
This sript runs the firmware of the NFC-Optogenetics on an ATtiny84 @ 1MHz.
It connects to a M24LR04E NFC RFID chip via TinyWireM library using I2C communication protocol.
The NFC memory can be writen wirelessly via NFC, and the microcontroller will
update the working parameters upon interruption (RF WIP).
Four channels have been programed to work independently.
Intensity modulation control on individual operation, 10 us resolution possible.
Indicator has a 1000 ms @ 10 % period of the idle state and
it copies the actual period and dc from the active channel in the active state.
Author: Abraham Vázquez-Guardado.
Center for Bio-Integrated Electronics
Northwestern University
Evanston, IL. 2020.
Last Revision: Nov 3rd 2020
*/
// Include libraries
#include <avr/io.h>
#include <TinyWireM.h>
void InitTimer0(void);
void StopTimer0(void);
void StartTimer0(void);
void InitTimer1(unsigned int T, unsigned int DC);
void StopTimer1(void);
void StartTimer1(void);
void ScreenMode(char MODE0);
void PresentPassword(char DEV_ADDR);
void TinyReadI2CPage(char *DATA, char DEV_ADDR, int DATA_ADDR);
void TinyWriteI2CByte(char DATA, char DEV_ADDR, int DATA_ADDR);
void TinyWriteI2CPage(char *DATA, char DEV_ADDR, int DATA_ADDR);
void SetOperationMode(char *DATA);
void InitPeriodsDutyCycles(void);
void InitMemory(void);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Define device specific addressess for ports
# define DDR_Channels DDRA
# define PORT_Channels PORTA
# define DDR_Indicator DDRB
# define PORT_Indicator PORTB
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Define contant and variables
const byte Indicator = PINB0;
const byte CH3 = PINA3;
const byte CH2 = PINA2;
const byte CH1 = PINA1;
const byte CH0 = PINA0;
unsigned int CH0_T1 = 0, CH0_DC1 = 0, CH0_T2 = 0, CH0_DC2 = 0;
unsigned int CH1_T1 = 0, CH1_DC1 = 0, CH1_T2 = 0, CH1_DC2 = 0;
unsigned int CH2_T1 = 0, CH2_DC1 = 0, CH2_T2 = 0, CH2_DC2 = 0;
unsigned int CH3_T1 = 0, CH3_DC1 = 0, CH3_T2 = 0, CH3_DC2 = 0;
unsigned int T1_inPhase = 0, DC1_inPhase = 0;
unsigned int T2_inPhase = 0, DC2_inPhase = 0;
unsigned int T2_outPhase = 0, DC2_outPhase = 0;
unsigned int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
volatile byte FLAG0 = 0; // This flag will trigger reconfig.
volatile byte MODE = 0; // Mode of operation.
volatile byte MASK = 0; // Mask to output.
volatile byte MASK0 = 0; // Mask used for out of phase calculations
volatile byte MASK1 = 0; // Mask to treat out of phase.
volatile byte MASK2 = 0; // Mask for indicator.
volatile byte PHASE = 0; // 0-out of phase, 1-in phase
volatile int COUNT0 = 0;
volatile int COUNT1 = 0;
volatile int COUNT2 = 0;
volatile int COUNT3 = 0;
volatile int BLINKT = 0;
volatile int BLINKCNT = 0;
volatile byte ONOFF = 0;
volatile byte INDONOFF = 1;
unsigned char I2C_BUFFER[8] = "EMPTYREG"; // I2C read buffer, up to 8 bytes.
char *PTR;
byte INIT = 0;
byte PTEMP = 0;
// M24LR04E Memory addres for config registers
const byte RFID_ADDR = 0x0A; // Device I2C address
const byte RFID_DEV_I2C_ENBL = 0x07; // Device enable I2C Memory, to be used during device address assembly
const byte RFID_DEV_RF_ENBL = 0x03; // Device enable RF Memory, to be used during device address assembly
const byte RFID_DEV_SEL_I2C = (RFID_ADDR << 3) | RFID_DEV_I2C_ENBL;
const byte RFID_DEV_SEL_RF = (RFID_ADDR << 3) | RFID_DEV_RF_ENBL;
// M24LR04E Memory addresses for data
const int RFID_CONF_BYTE_ADDR = 2320; // Memory address of the config register
const int RFID_UID_ADDR = 2324; // Memory address of the device UID
const int RFID_CTRL_REG_ADDR = 2336; // Memory address of the control register
const int RFID_WRT_LOCK_ADDR = 2048; // Memory address of the write lock bit
const int RFID_I2C_PSSW_ADDR = 2304; // Memory address for the I2C password protection
const int MODE_ADDR = 0; // Block address for reading data stored by the RF channel
const int CH0_1_ADDR = 4; // They reside in sector 0
const int CH0_2_ADDR = 8; // They reside in sector 0
const int CH1_1_ADDR = 12;
const int CH1_2_ADDR = 16;
const int CH2_1_ADDR = 20;
const int CH2_2_ADDR = 24;
const int CH3_1_ADDR = 28;
const int CH3_2_ADDR = 32;
const int InPh1_ADDR = 36;
const int InPh2_ADDR = 40;
const int OutPh1_ADDR = 44;
const int OutPh2_ADDR = 48;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//------------------------------------------------------------------
//------------------------------------------------------------------
void setup() {
// delay(100);
MCUCR |= 1 << PUD;
// Set up outpout ports
DDR_Indicator = (1 << Indicator); // Outputs PORTB0-4
DDR_Channels = 0x0F; // Input RF WIP (PA7), Indicator PORTA-0
TIMSK1 = 0x00;
TCCR1A = 0x00;
TCCR1B = 0x00;
// Initializes the I2C communication channel
delay(250);
TinyWireM.begin();
// for (int i = 0; i <5; i++){
// bitClear(PORT_Indicator, Indicator); delay(150);
// bitSet(PORT_Indicator, Indicator); delay(50);
// }
// Config RF/WIP output.
PresentPassword(RFID_DEV_SEL_I2C);
TinyWriteI2CByte(0x00, RFID_DEV_SEL_I2C, RFID_CTRL_REG_ADDR);
TinyWriteI2CByte(0xF8, RFID_DEV_SEL_I2C, RFID_CONF_BYTE_ADDR);
// Check if the memory is empty. This will be true when the first four bytes are 0xFF
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, MODE_ADDR);
if ((I2C_BUFFER[3] == 0xFF)) {
bitClear(PORT_Indicator, Indicator); delay(50);
bitSet(PORT_Indicator, Indicator); delay(50);
bitClear(PORT_Indicator, Indicator); delay(50);
bitSet(PORT_Indicator, Indicator);
InitMemory();
delay(1000);
bitClear(PORT_Indicator, Indicator); delay(50);
bitSet(PORT_Indicator, Indicator); delay(50);
bitClear(PORT_Indicator, Indicator); delay(50);
bitSet(PORT_Indicator, Indicator);
}
// Load M24LR04E-R stored variables to the program, always before SetOperationMode
// InitPeriodsDutyCycles();
// Set the mode of operation.
// TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, MODE_ADDR);
// SetOperationMode(I2C_BUFFER);
// Sets up an interruption on pin PINA7/PCINT7
MCUCR = 0x00;
GIMSK |= 1 << PCIE0;
PCMSK0 |= 1 << PCINT7; //<<---------------------------------------|||||
GIFR = 0x00; // Clear flags
// Initializes TIMER0 with 1 ms resolution
InitTimer0();
ONOFF = 0xFF;
FLAG0 = 1;
MASK = 1;
MCUCR |= _BV(BODS) | _BV(BODSE);
ADCSRA &= ~ bit(ADEN); // disable the ADC
bitSet(PRR, PRADC); // power down the ADC
}
// Outputs were adjusgted to demo board
//------------------------------------------------------------------
//------------------------------------------------------------------
void loop() {
while (MASK) {
// PORT_Channels = 0x00;
StopTimer0();
StopTimer1();
delay(250);
bitClear(PRR, PRUSI); // enable USI h/w
TinyWireM.begin(); // start wire library
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, MODE_ADDR);
SetOperationMode(I2C_BUFFER);
INDONOFF = I2C_BUFFER[2]; // Sets indicator blinking or not
StartTimer0();
ScreenMode(I2C_BUFFER[1]);
bitSet(PRR, PRUSI); // disable USI h/w
DDR_Channels = 0x0F; // leave only channel pins as output
MASK = 0;
COUNT0 = 0;
COUNT1 = 0;
COUNT2 = 0;
COUNT3 = 0;
BLINKCNT = 0;
TCNT1H = 0x00;
TCNT1L = 0x00;
TCNT0 = 0x00;
PORT_Channels = 0xF0;
PORT_Channels |= MASK1;
sei();
}
while (FLAG0) {
switch (MODE) {
case 0:
if (COUNT0 == 100) {
bitSet(PORT_Indicator, Indicator);
}
if (COUNT0 == 1000) {
PORT_Indicator &= ~((1 << Indicator) & (INDONOFF)); // Indicator/Optional
COUNT0 = 0;
}
break;
case 1:
// Channel 0
if (COUNT0 == CH0_DC1) {
TIMSK1 = 0x00; // Dissable interrupt timer 1
bitClear(PORT_Channels, CH0);
PORT_Indicator &= ~((1 << Indicator) & (INDONOFF)); // Indicator/Optional
ONOFF = 0x00;
}
if (COUNT0 == CH0_T1) {
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B); // Enables interruption A and B.
bitSet(PORT_Channels, CH0);
bitSet(PORT_Indicator, Indicator);
TCNT1H = 0x00; TCNT1L = 0x00;
COUNT0 = 0; ONOFF = 0xFF;
}
break;
case 2:
// Channel 1
if (COUNT0 == CH1_DC1) {
TIMSK1 = 0x00; // Dissable interrupt timer 1
bitClear(PORT_Channels, CH1);
PORT_Indicator &= ~((1 << Indicator) & (INDONOFF)); // Indicator/Optional
ONOFF = 0x00;
}
if (COUNT0 == CH1_T1) {
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B); // Enables interruption A and B.
bitSet(PORT_Channels, CH1);
bitSet(PORT_Indicator, Indicator);
TCNT1H = 0x00; TCNT1L = 0x00;
COUNT0 = 0; ONOFF = 0xFF;
}
break;
// Update dual mode with indicator
case 3: case 5: case 6: case 9: case 10: case 12:
if (PHASE == 0) { // Out of Phase
if (COUNT0 == t1) {
PORT_Channels = 0xF0;
// PORT_Channels &= !MASK0;
bitSet(PORT_Indicator, Indicator); // Indicator
MASK1 = MASK0;
ONOFF = 0x00;
}
if (COUNT1 == t2) {
PORT_Channels = 0xF0 + (MODE - MASK0);
// PORT_Channels |= MODE - MASK0;
MASK1 = MODE - MASK0;
TCNT1H = 0x00; TCNT1L = 0x00;
ONOFF = 0xFF;
}
if (COUNT2 == t3) {
PORT_Channels = 0xF0;
// PORT_Channels &= !(MODE - MASK0);
PORT_Indicator &= ~((1 << Indicator) & (INDONOFF)); // Indicator/Optional
MASK1 = MODE - MASK0;
ONOFF = 0x00;
}
if (COUNT3 == t4) {
PORT_Channels = 0xF0 + MASK0;
// PORT_Channels |= MASK0;
MASK1 = MASK0;
COUNT0 = 0; COUNT1 = 0;
COUNT2 = 0; COUNT3 = 0;
TCNT1H = 0x00; TCNT1L = 0x00;
ONOFF = 0xFF;
}
} else { // In phase
if (COUNT0 == DC1_inPhase) {
TIMSK1 = 0x00; // Dissable interrupt timer 1
PORT_Channels = 0xF0;
bitSet(PORT_Indicator, Indicator); // Indicator
ONOFF = 0x00;
}
if (COUNT0 == T1_inPhase) {
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B); // Enables interruption A and B.
MASK1 = MODE;
PORT_Channels |= MODE - MASK0;
PORT_Indicator &= ~((1 << Indicator) & (INDONOFF)); // Indicator/Optional
TCNT1H = 0x00; TCNT1L = 0x00;
COUNT0 = 0; ONOFF = 0xFF;
}
}
break;
}
FLAG0 = 0;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Initialize memory.
void InitMemory(void) {
unsigned int P;
I2C_BUFFER[0] = 0;
I2C_BUFFER[1] = 0;
I2C_BUFFER[2] = 1; // Indicator is active
I2C_BUFFER[3] = 0;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, MODE_ADDR);
// Channel 0 - - - - - - - - - - - - - - - - - - - - - - - - - - -
P = 500;
I2C_BUFFER[0] = (P / 2) & 0xFF; // LSB Duty Cycle [ms]
I2C_BUFFER[1] = (P / 2 >> 8) & 0xFF; // MSB Duty Cycle [ms]
I2C_BUFFER[2] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[3] = (P >> 8) & 0xFF; // MSB Period [ms]
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_1_ADDR);
P = 500;
I2C_BUFFER[0] = 100; // Duty cycle [%]
I2C_BUFFER[1] = P & 0xFF; // LSB Period [us]
I2C_BUFFER[2] = (P >> 8) & 0xFF; // MSB Period [us]
I2C_BUFFER[3] = 0x00;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_2_ADDR);
// Channel 1 - - - - - - - - - - - - - - - - - - - - - - - - - - -
P = 500;
I2C_BUFFER[0] = (P / 2) & 0xFF; // LSB Duty Cycle [ms]
I2C_BUFFER[1] = (P / 2 >> 8) & 0xFF; // MSB Duty Cycle [ms]
I2C_BUFFER[2] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[3] = (P >> 8) & 0xFF; // MSB Period [ms]
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_1_ADDR);
P = 500;
I2C_BUFFER[0] = 100; // Duty cycle [%]
I2C_BUFFER[1] = P & 0xFF; // LSB Period [us]
I2C_BUFFER[2] = (P >> 8) & 0xFF; // MSB Period [us]
I2C_BUFFER[3] = 0x00;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_2_ADDR);
// Channel 2 - - - - - - - - - - - - - - - - - - - - - - - - - - -
P = 500;
I2C_BUFFER[0] = (P / 2) & 0xFF; // LSB Duty Cycle [ms]
I2C_BUFFER[1] = (P / 2 >> 8) & 0xFF; // MSB Duty Cycle [ms]
I2C_BUFFER[2] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[3] = (P >> 8) & 0xFF; // MSB Period [ms]
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_1_ADDR);
P = 500;
I2C_BUFFER[0] = 100; // Duty cycle [%]
I2C_BUFFER[1] = P & 0xFF; // LSB Period [us]
I2C_BUFFER[2] = (P >> 8) & 0xFF; // MSB Period [us]
I2C_BUFFER[3] = 0x00;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_2_ADDR);
// Channel 3 - - - - - - - - - - - - - - - - - - - - - - - - - - -
P = 500;
I2C_BUFFER[0] = (P / 2) & 0xFF; // LSB Duty Cycle [ms]
I2C_BUFFER[1] = (P / 2 >> 8) & 0xFF; // MSB Duty Cycle [ms]
I2C_BUFFER[2] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[3] = (P >> 8) & 0xFF; // MSB Period [ms]
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_1_ADDR);
P = 500;
I2C_BUFFER[0] = 100; // Duty cycle [%]
I2C_BUFFER[1] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[2] = (P >> 8) & 0xFF; // MSB Period [ms]
I2C_BUFFER[3] = 0x00;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_2_ADDR);
// Multichannel - - - - - - - - - - - - - - - - - - - - - - - - -
P = 500;
I2C_BUFFER[0] = 240 & 0xFF; // LSB Duty Cycle [ms]
I2C_BUFFER[1] = (240 >> 8) & 0xFF; // MSB Duty Cycle [ms]
I2C_BUFFER[2] = P & 0xFF; // LSB Period [ms]
I2C_BUFFER[3] = (P >> 8) & 0xFF; // MSB Period [ms]
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh1_ADDR);
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh1_ADDR);
I2C_BUFFER[0] = 100; // Duty cycle [%]
I2C_BUFFER[1] = P & 0xFF; // LSB Period [us]
I2C_BUFFER[2] = (P >> 8) & 0xFF; // MSB Period [us]
I2C_BUFFER[3] = 0x00;
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh2_ADDR);
TinyWriteI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh2_ADDR);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Initializes time values, this will be updated after the RF write
// The period and duty cycle (1) for each channel is encoded in
// two separate bytes. Those corresponding to (2) are DC (one byte)
// and T (two bytes). This only happens at start-up.
void InitPeriodsDutyCycles(void) {
unsigned int T, DC;
// Sets for channel 0 - - - - - - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_1_ADDR);
CH0_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH0_T1 += CH0_T1 / 50;
CH0_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH0_DC1 += CH0_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_2_ADDR);
CH0_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH0_DC2 = I2C_BUFFER[0];
// Sets for channel 1 - - - - - - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_1_ADDR);
CH1_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH1_T1 += CH1_T1 / 50;
CH1_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH1_DC1 += CH1_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_2_ADDR);
CH1_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH1_DC2 = I2C_BUFFER[0];
// Sets for channel 2 - - - - - - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_1_ADDR);
CH2_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH2_T1 += CH2_T1 / 50;
CH2_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH2_DC1 += CH2_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_2_ADDR);
CH2_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH2_DC2 = I2C_BUFFER[0];
// Sets for channel 3 - - - - - - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_1_ADDR);
CH3_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH3_T1 += CH3_T1 / 50;
CH3_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH3_DC1 += CH3_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_2_ADDR);
CH3_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH3_DC2 = I2C_BUFFER[0];
// Sets for in phase config - - - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh1_ADDR);
T1_inPhase = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
T1_inPhase += T1_inPhase / 50;
DC1_inPhase = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
DC1_inPhase += DC1_inPhase / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh2_ADDR);
T2_inPhase = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
DC2_inPhase = I2C_BUFFER[0];
// Sets for out of phase config - - - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh1_ADDR);
T = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
T += T / 50;
DC = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
DC += DC / 50;
t1 = DC;
t2 = T / 2;
t3 = t1 + t2;
t4 = T;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh2_ADDR);
T2_outPhase = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
DC2_outPhase = I2C_BUFFER[0];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Set Operation mode. This will scan the I2C Buffer and set up the
// corresponding parameters.
void SetOperationMode(char *DATA) {
DATA += 1; // Increases to position two the array
MODE = *DATA; // Gets the mode of operation
DATA -= 1; // Returns to position one in the array
MASK1 = MODE;
// Check if individual operation
switch (*DATA) {
case 0x00: //Individual operation mode
break;
case 0x01: // Multiple but out of phase
PHASE = 0;
MASK0 = (MODE - 1) & 0x0E;
if (MODE == 12) {
MASK0 = 0x08;
}
MASK1 = MASK0;
break;
case 0x11: // Multiple but in phase
PHASE = 1;
MASK0 = (MODE - 1) & 0x0E;
if (MODE == 12) {
MASK0 = 0x08;
}
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Initializes timer 1 with the proper values for T and DC. Still
// needs the function StartTimer1(). It will also update the
// corresponding timing for the selected channel combination.
void ScreenMode(char MODE0) {
unsigned int T, DC;
switch (MODE0) {
case 1: // Updates for channel 0 - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_1_ADDR);
CH0_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH0_T1 += CH0_T1 / 50;
CH0_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH0_DC1 += CH0_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH0_2_ADDR);
CH0_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH0_DC2 = I2C_BUFFER[0];
if (CH0_DC2 < 100) {
InitTimer1(CH0_T2, CH0_DC2);
}
if (CH0_DC1 >= CH0_T1) {
StopTimer0();
}
break;
case 2: // Updates for channel 1 - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_1_ADDR);
CH1_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH1_T1 += CH1_T1 / 50;
CH1_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH1_DC1 += CH1_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH1_2_ADDR);
CH1_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH1_DC2 = I2C_BUFFER[0];
if (CH1_DC2 < 100) {
InitTimer1(CH1_T2, CH1_DC2);
}
if (CH1_DC1 >= CH1_T1) {
StopTimer0();
}
break;
case 4: // Updates for channel 2 - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_1_ADDR);
CH2_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH2_T1 += CH2_T1 / 50;
CH2_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH2_DC1 += CH2_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH2_2_ADDR);
CH2_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH2_DC2 = I2C_BUFFER[0];
if (CH2_DC2 < 100) {
InitTimer1(CH2_T2, CH2_DC2);
}
if (CH2_DC1 >= CH2_T1) {
StopTimer0();
}
break;
case 8: // Updates for channel 3 - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_1_ADDR);
CH3_T1 = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
CH3_T1 += CH3_T1 / 50;
CH3_DC1 = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
CH3_DC1 += CH3_DC1 / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, CH3_2_ADDR);
CH3_T2 = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
CH3_DC2 = I2C_BUFFER[0];
if (CH3_DC2 < 100) {
InitTimer1(CH3_T2, CH3_DC2);
}
if (CH3_DC1 >= CH3_T1) {
StopTimer0();
}
break;
case 3: case 5: case 6: case 9: case 10: case 12:
if (PHASE == 0) { // Updates for out of phase - - - - - - - - - - - - - - - -
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh1_ADDR);
T = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
T += T / 50;
DC = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
DC += DC / 50;
t1 = DC;
t2 = T / 2;
t3 = t1 + t2;
t4 = T;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, OutPh2_ADDR);
T2_outPhase = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
DC2_outPhase = I2C_BUFFER[0];
if (DC2_outPhase < 100) {
InitTimer1(T2_outPhase, DC2_outPhase);
}
break;
} else { // Updates for in phase
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh1_ADDR);
T1_inPhase = (I2C_BUFFER[3] << 8) + I2C_BUFFER[2] + 1;
T1_inPhase += T1_inPhase / 50;
DC1_inPhase = (I2C_BUFFER[1] << 8) + I2C_BUFFER[0] + 1;
DC1_inPhase += DC1_inPhase / 50;
TinyReadI2CPage(I2C_BUFFER, RFID_DEV_SEL_RF, InPh2_ADDR);
T2_inPhase = (I2C_BUFFER[2] << 8) + I2C_BUFFER[1];
DC2_inPhase = I2C_BUFFER[0];
if (DC2_inPhase < 100) {
InitTimer1(T2_inPhase, DC2_inPhase);
} else {
MASK0 = 0;
}
break;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// This function writes 4 bytes of data from the I2C channel
void TinyWriteI2CPage(char *DATA, char DEV_ADDR, int DATA_ADDR) {
byte ADDR0;
// First give the device address, the function adds RW bit.
TinyWireM.beginTransmission(DEV_ADDR); // Addresses and enables the device.
// Sends the 16 bit address location to write to.
ADDR0 = DATA_ADDR >> 8;
TinyWireM.send(ADDR0); // Send MSB first
ADDR0 = DATA_ADDR & 0xFF;
TinyWireM.send(ADDR0); // Send LSB at the end.
TinyWireM.send(*DATA); DATA++;
TinyWireM.send(*DATA); DATA++;
TinyWireM.send(*DATA); DATA++;
TinyWireM.send(*DATA);
TinyWireM.endTransmission(); // End tramission but sends a restart
// endTransmission(0/1) sends a restart/stop commands
//End transmission and give some delay.
delay(10);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// This function writes 1 byte of data from the I2C channel
void TinyWriteI2CByte(char DATA, char DEV_ADDR, int DATA_ADDR) {
byte ADDR0;
// First give the device address, the function adds RW bit.
TinyWireM.beginTransmission(DEV_ADDR); // Addresses and enables the device.
// Sends the 16 bit address location to write to.
ADDR0 = DATA_ADDR >> 8;
TinyWireM.send(ADDR0); // Send MSB first
ADDR0 = DATA_ADDR & 0xFF;
TinyWireM.send(ADDR0); // Send LSB at the end.
TinyWireM.send(DATA);
TinyWireM.endTransmission(); // End tramission but sends a restart
// endTransmission(0/1) sends a restart/stop commands
//End transmission and give some delay.
delay(10);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// This function reads 4 bytes of data from the I2C channel
void TinyReadI2CPage(char *DATA, char DEV_ADDR, int DATA_ADDR) {
byte ADDR0;
// First give the device address, the function adds RW bit.
// Give write command to load the address to read.
TinyWireM.beginTransmission(DEV_ADDR); // Addresses and enables the device.
ADDR0 = DATA_ADDR >> 8;
TinyWireM.send(ADDR0); // Send MSB first
ADDR0 = DATA_ADDR & 0xFF;
TinyWireM.send(ADDR0); // Send LSB at the end.
TinyWireM.endTransmission();
// Evoke the read command, which will read the previously sent address.
DATA = I2C_BUFFER;
TinyWireM.requestFrom(DEV_ADDR, 4); // Requests 8 bytes from slave
while (TinyWireM.available()) {
*DATA = 0; // Clears the reading buffer.
*DATA = TinyWireM.receive();
DATA++;
}
delay(10);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// This function writes 4 bytes of data from the I2C channel
void PresentPassword(char DEV_ADDR) {
// First give the device address, the function adds RW bit.
TinyWireM.beginTransmission(DEV_ADDR); // Addresses and enables the device.
// Sends the 16 bit address location to write to.
TinyWireM.send(0x09); // Send MSB first
TinyWireM.send(0x00); // Send LSB at the end.
// Default password is 0x00 00 00 00
TinyWireM.send(0x00);
TinyWireM.send(0x00);
TinyWireM.send(0x00);
TinyWireM.send(0x00);
// Confirm password
TinyWireM.send(0x09);
TinyWireM.send(0x00);
TinyWireM.send(0x00);
TinyWireM.send(0x00);
TinyWireM.send(0x00);
TinyWireM.endTransmission(); // End tramission but sends a restart
// endTransmission(0/1) sends a restart/stop commands
//End transmission and give some delay.
delay(100);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Set up TIMER0 to work inthe CTC mode at 1MHz, 8 prescaler, and
// provide 1 ms interruption.
void InitTimer0(void) {
TCCR0A = 0x02; // Clear timer register, set CTC on A
TCCR0B = 0x02; // Sets prescaler 1/8
OCR0A = 125; // With 1MHz and 1/8 prescaler, 125 gives 1 ms.
COUNT0 = 0; // Start software counter
COUNT1 = 0;
COUNT2 = 0;
COUNT3 = 0;
TCNT0 = 0x00;
}
void StopTimer0(void) {
TIMSK0 &= !(1 << OCIE0A);// Disables interruption A.
}
void StartTimer0(void) {
TCNT0 = 0x00;
TIMSK0 |= 1 << OCIE0A;
TIFR0 |= 0x02; // Clears interruption flag
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Set up TIMER1 to work inthe CTC mode at 1MHz, and 1 prescaler.
// Two interruptions are programed, one for register A and B. B is
// first and dictates the duty cycle, A is second and dictates the period.
// Default values are 100 ms @ 50%
void InitTimer1(unsigned int T, unsigned int DC) {
unsigned int temp = 0;
temp = DC;
TIMSK1 = 0x00;
TCCR1A = 0x00; // Clear timer register, set CTC on A
TCCR1B = 0x09; // Sets prescaller 1/1, and WGM12, or 1us counter.
TCCR1C = 0x00;
T += T * 0.033;
OCR1AH = (T >> 8) & 0xFF;
OCR1AL = T & 0xFF;
// Changes DC[%] to 1us counts.
T /= 100; DC *= T;
// Compensate for T = 500 us.
DC = DC + 0.14 * temp - 34.92;
OCR1BH = (DC >> 8) & 0xFF;
OCR1BL = DC & 0xFF;
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B);// Enables interruption A and B.
TIFR0 |= 0x06; // Clears interruption flag
TCNT1H = 0x00;
TCNT1L = 0x00;
}
void StopTimer1(void) {
TIMSK1 = 0x00;
TCCR1A = 0x00;
TCCR1B = 0x00;
}
void StartTimer1(void) {
TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B);// Enables interruption A and B.
TCNT1H = 0x00;
TCNT1L = 0x00;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Routine that services TIMER0 compare with OCR0A register.
// It is programmed to work with 1MHz clock, 8 presecaller
// and 1 ms interruption.
ISR(TIM0_COMPA_vect) {
COUNT0++;
COUNT1++;
COUNT2++;
COUNT3++;
BLINKCNT++;
FLAG0 = 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Routine that services TIMER1 (8-bit mode) compare with OCR1AL register.
// It is programmed to work with 1MHz clock, 8 presecaller
// and 1 ms interruption.
ISR(TIM1_COMPA_vect) {
PORT_Channels ^= (MASK1 & ONOFF);
}
ISR(TIM1_COMPB_vect) {
PORT_Channels ^= (MASK1 & ONOFF);
}
ISR(PCINT0_vect) {
cli();
PORT_Channels &= ~0xF0;
MASK = 1;
}