-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlinky.c
executable file
·1208 lines (1138 loc) · 30 KB
/
Blinky.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
/* Blinky.C - LED Fla+-
sher for the Keil LPC900 EPM Emulator/Programmer Module */
#include <REG936.H> // register definition
#include <LIMITS.H>
#include <INTRINS.H>
#include <STDIO.H>
#include <CTYPE.H>
#include "power.h"
#include "uart.h"
#include "adc.h"
#include "ee.h"
#include "midi_spec.h"
#include "LUTFreq.h"
#include "LUTsin.h"
#include "itrip.h"
#include "song.h"
/******************************* GLOBALS**************************************/
byte data myLChan;
byte data myVChan;
//this must align with the final linker script to work....
//song is at (C:0x1802)
const byte code myLChanFlash = MY_L_CHAN;//_at_ 0x1800;//
const byte code myVChanFlash = MY_V_CHAN;//_at_ 0x1801;//
volatile byte xdata sysEx[SYS_LEN];
volatile word sysIx = 0;
volatile byte songNum = 0;
volatile word midiClk = 0;
//state flags -- maybe change to sbit????
volatile byte bdata midiFlags;
sbit AUDIO_L_ON = midiFlags^0; //(0x01) //allow wiggling the Laudio (attached to noteOn and sequencer)
sbit TX_VCC_ON = midiFlags^1; //(0x02) //allow wiggle the txVCC (attached to noteOn and sequencer)
sbit STEREO = midiFlags^2; //(0x04) //keep track of tx number of channels
sbit PLAYING = midiFlags^3; //(0x08) //am I playing now? //not Ignoring midiClk
sbit BUTT_EN = midiFlags^4; //(0x10) //todo test
sbit OMNI = midiFlags^5; //(0x20) //todo ?test
//sbit SONG_DONE = midiFlags^6; //0x40
//sbit LOOP_SONGS = midiFlags^7; //instead of naziMidi stop... just one? everyone?? deviant!!!
volatile byte bdata songFlags;
sbit STATE_0 = songFlags^0; //MODE0 or MODE1 for unit#s
sbit STATE_1 = songFlags^1; //TURN ON? SONGS CODE!?!
sbit STATE_2 = songFlags^2;
sbit STATE_3 = songFlags^3;
sbit SONG_DONE = songFlags^4; //0x40
sbit LOOP_SONGS = songFlags^5; //instead of naziMidi stop... just one? everyone?? deviant!!!
sbit AUTO_START = songFlags^6; //you know, start on power up after a delay, for master.... maybe also function as a watchdog
sbit WRAP_FREQ = songFlags^7; //if told to move beyond limits of txFreq, wrap around... maybe break into top and bottom flags...
//try moving to locals!!!
//#define NUM_RIFFS HEAVY_11_SONG_SIZE//SAUCER_VOLCANO_SONG_SIZE//THUMP1_SONG_SIZE//3//FOR_SONG_ALT_SIZE//FIRST_SONG_ALT_SIZE//THIRD_SONG_SIZE//
volatile RIFF_T* curSong;
volatile word xdata nextRiff = 0;
volatile byte xdata curRiffCnt = 0;
volatile word xdata numRiffs = 0;
volatile byte code* riff;
//deltaSongPos ... just ... cant.. be ... a ... byte....
volatile word deltaPos = 0;
volatile byte numNotes = 0;
volatile byte nextNote = 0;
//end exclusive defines
#define SPEED_DIV 1
//#if (MY_L_CHAN > 4)
//#define FREQ_START (882)
//#else
//#define FREQ_START (879)
//#endif
#define FREQ_START (879)// + (MY_L_CHAN*2))
//timing globals
//REMEMBER!!! these are up timers... period = 0x0 - values...
//tempo or txVcc pitch
volatile byte periodH0 = 0;
volatile byte periodL0 = 0;
volatile byte cnt0 = 0;
#ifdef COORD
volatile byte temp0 = 0;
#endif
//pitch
volatile byte periodH1 = 0;
volatile byte periodL1 = 0;
//helpful shadows...
volatile word LPeriod;
volatile word VPeriod;
volatile byte LNote;
volatile byte VNote;
volatile word lDelta = 0;
volatile word txDelta = 0;
volatile bit deltaLUp = 0;
volatile bit deltaTxUp = 0;
////// ADC
//volatile byte adcFlag;
#ifdef ADC_IN
volatile byte newADC0 = 0;
byte oldADC0 = 0;
volatile byte newADC1 = 0;
byte oldADC1 = 0;
#endif
//volatile byte adcVal[2];
///DAC
#if defined(DAC1_OUT_AUDIO) || defined (DAC1_OUT_VCC)
byte dac1LUTdex = 0;
#endif
/****************************BH141* instruction*******************************/
volatile word bdata txState;
sbit stereoTx = txState^3;
sbit phaseMode0 = txState^4;
sbit phaseMode1 = txState^5;
sbit test0 = txState^6;
sbit test1 = txState^7;
//flag between VChan timer and all other functions
volatile bit txOffSwitch = 0;
//flag between CV and uart
volatile bit enableTxCVGate = 1;
volatile word station = FREQ_START;
/***********************************8MAIN????????????????????????*****************/
void main() {
/**************LOCALS****************************/
byte i = 0;
//power_brownoutenable(POWER_BORESET);
/**************SETUP++***************************************/
myLChan = myLChanFlash;
myVChan = myVChanFlash;
//if anything is fishy in flash, adjust the channel values
if (myLChan == myVChan) {
myVChan = myLChan + 1;
}
if (myLChan > 15 || myLChan < 0 || myVChan > 15 || myVChan < 0) {
myLChan = MY_L_CHAN;
myVChan = MY_V_CHAN;
}
station += myLChan*2;
setup();
OMNI = 0;
//Config boot-up
while (midButt == 0) { //debounced midButt switches COORD's PLAYING
LED ^= 1;
delay(UINT_MAX/3);
if (cnt0++ > 80) { //hold middle button at boot for ~2.5s
//add a gang sign to enter flash programming mode, cause it will be hard to recover from...
while (midButt == 0) {};
while (1) {
if (hiButt == 0 ) {
no_touch();
} else if (loButt == 0) {
cnt0 = 0;
break;
}
}
}
//hopefully a graceful recovery from the threat of flash programming
//TODO for later peeps
//read butts
//if butts
// write eeprom
//else read eeprom
}
while (hiButt == 0) {
#ifdef COORD
#else
#endif
}
while (loButt == 0) {
#ifdef COORD
#else
#endif
}
// End Config boot-up
cnt0 = 0;
txVcc = 0; //on
setFreq(station);
uart_init();
//init first song... just incase its the first one we play
//nextRiff = 0; //to be explicit
curSong = songBook[songNum];
numRiffs = (curSong[nextRiff]).rAddy; //grab song length! update will inc nextRiff!!!
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
EA = 1;
BUTT_EN = 1;
//LOOP_SONGS = 0;
//autoStart
AUTO_START = 0;
STATE_0 = 0;
//will get turned on by ADC ATM in Coord
STATE_1 = 1;
STEREO = 0;
#ifdef COORD
TR0 = 0;
TR1 = 0;
// while (1) {
// uart_transmit(CONTROL);
// uart_transmit(GENERAL_SLIDER_1_lo);
// uart_transmit((i++ << 5) & ~(0x80));
// delay(1000);
// }
//makes for a happy synth!!!!
delay(50000);
if (STATE_1 == 1) {
uart_transmit(SONG_SELECT);
uart_transmit(songNum);
delay(50000);
for (i = 0; i < 16; i += 2) {
uart_transmit(CONTROL+i);
uart_transmit(GENERAL_SLIDER_1_lo);
uart_transmit(STATION_TO_LO((879 + (i*2))));
delay(1000);
uart_transmit(CONTROL+i);
uart_transmit(GENERAL_SLIDER_1_hi);
uart_transmit(STATION_TO_HI((879 + (i*2))));
delay(1000);
uart_transmit(CONTROL+i);
uart_transmit(GENERAL_BUTTON_1_on);
uart_transmit(127);
}
LED = 0;
if (AUTO_START == 1) {
curSong = songBook[songNum];
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length and flags! dont inc it.. update will
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
uart_transmit(STOP);
uart_transmit(SONG_SELECT);
uart_transmit(songNum);
uart_transmit(START);
PLAYING = 1;
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
LED = PLAYING;
TR0 = PLAYING;
}
}
#else
delay(65000);
delay(65000);
TR1 = 1;
TR0 = 1;
#endif
//UNIT!! only has onboard LEDS
LED = 1;
setFreq(station);
/****************LOOP*****************************************/
for(;;) {
#ifdef COORD
#ifdef ADC_IN
if (ADCON0 & 0x08) {
// clear ADCI0 flag
ADCON0 &= ~0x08;
// read results from AD0DAT0 - AD0DAT3
newADC0 = AD0DAT0;
#ifdef UNIT_11
//unit11
newADC1 = AD0DAT2;
#endif
#ifdef UNIT_XII
//unitXII TODO test and improve this strawFilter
newADC1 = (newADC1 >> 2) + (AD0DAT3 >> 2);
#endif
}
// if (oldADC1 != newADC1) { //might not be super useful....
// //VPeriod is fixed record, LPeriod is scratch, newADC0 is basically periodH0
// //remember these are UP counters but temp0 is a down count!!!
// LPeriod = VPeriod + newADC1 << 1;
// temp0 = (LPeriod >> 8) & 0xff;
// periodH0 = LPeriod & 0xff;
// }
//or ... both arent super amazing (with one channel)
if (newADC1 > 55) { //TODO needs debugging... coord is ok, but basic isnt stable
if (STATE_0 == 0) { //seems like playing on basic is not getting fired... still responds
//to CV.... no response to coord button twiddlin
//really doesnt like it when you press the buttons.... [tbclear... it responds slowly]
//might be reseting
TR0 = 0; //dont try to throw a clock in here
uart_transmit(SYSTEM_EXCLUSIVE);
uart_transmit(REAL_TIME_ID);
uart_transmit(0x01);
uart_transmit(0x0e);
uart_transmit(0x0d);
uart_transmit(SYS_EX_MODE_2_UNIT);
uart_transmit(EOX);
TR0 = 1;
}
STATE_0 = 1;
} else {
if (STATE_0 == 1) {
TR0 = 0; //dont try to throw a clock in here
uart_transmit(SYSTEM_EXCLUSIVE);
uart_transmit(REAL_TIME_ID);
uart_transmit(0x01);
uart_transmit(0x0e);
uart_transmit(0x0d);
uart_transmit(SYS_EX_MODE_1_UNIT);
uart_transmit(EOX);
TR0 = 1;
}
STATE_0 = 0;
}
//this has to be a mode because it dominates functionality with no signal....
if (STATE_1 == 1) {
if (newADC0 > 55) {
if (PLAYING == 1) {
uart_transmit(STOP);
PLAYING = 0;
}
BUTT_EN = 0;
} else {
if (PLAYING == 0 && midiClk > 0) {
uart_transmit(CONTINUE);
PLAYING = 1;
}
BUTT_EN = 1;
}
LED = PLAYING;
TR0 = PLAYING;
}
// else {
// if (newADC0 > 55) {
// if (STATE_0 == 0) {
// uart_transmit(SYSTEM_EXCLUSIVE);
// uart_transmit(REAL_TIME_ID);
// uart_transmit(0x01);
// uart_transmit(0x0e);
// uart_transmit(0x0d);
// uart_transmit(SYS_EX_MODE_2_UNIT);
// uart_transmit(EOX);
// }
// STATE_0 = 1;
// } else {
// if (STATE_0 == 1) {
// uart_transmit(SYSTEM_EXCLUSIVE);
// uart_transmit(REAL_TIME_ID);
// uart_transmit(0x01);
// uart_transmit(0x0e);
// uart_transmit(0x0d);
// uart_transmit(SYS_EX_MODE_1_UNIT);
// uart_transmit(EOX);
// }
// STATE_0 = 0;
// }
// }
#endif
txVcc = 1; //assert tx off...
if (BUTT_EN == 1 && STATE_1 == 1) {
if (midButt == 0) { //debounced midButt switches COORD's PLAYING
delay(50000); //cant functionalize this says the internet...
if (midButt == 0) {
delay(50000);
if(midButt == 0) {
if (PLAYING) { //we are already playing
uart_transmit(STOP);
PLAYING = 0;
} else if (midiClk == 0) { //were we playing once?
curSong = songBook[songNum];
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length and flags! dont inc it.. update will
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
uart_transmit(STOP);
uart_transmit(SONG_SELECT);
uart_transmit(songNum);
uart_transmit(START);
PLAYING = 1;
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
} else {
uart_transmit(CONTINUE);
PLAYING = 1;
}
}
}
LED = PLAYING;
TR0 = PLAYING;
//TR1 = PLAYING;
delay(50000);
} else if (hiButt == 0) { //switch to previous song
delay(50000); //cant functionalize this says the internet...
if (hiButt == 0) {
delay(50000);
if(hiButt == 0) {
LED = 1;
delay(50000);
if (++songNum >= NUM_SONGS) { //have we gone past song 0 or 127?
songNum = NUM_SONGS-1;
}
midiClk = 0;
PLAYING = 0;
TR0 = 0;
//TR1 = 0;
uart_transmit(STOP);
uart_transmit(SONG_SELECT);
uart_transmit(songNum);
curSong = songBook[songNum];
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length! dont inc it.. update will
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
LED = 0;
}
}
delay(50000);
} else if (loButt == 0) { //switch to previous song
delay(50000); //cant functionalize this says the internet...
if (loButt == 0) {
delay(50000);
if(loButt == 0) {
LED = 1;
delay(50000);
if (--songNum > 127) { //have we gone past song 0 or 127?
songNum = 0;
}
midiClk = 0;
PLAYING = 0;
TR0 = 0;
//TR1 = 0;
uart_transmit(STOP);
uart_transmit(SONG_SELECT);
uart_transmit(songNum);
curSong = songBook[songNum];
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length! dont inc it.. update will
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
LED = 0;
}
}
delay(50000);
}
}
#else
if (curSong != songBook[songNum] && STATE_1 == 1) {
setFreq(station);
curSong = songBook[songNum];
midiClk = 0;
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length! dont inc it.. update will
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
} else { //loops ~every 28us
//low priority stuff
#ifdef ADC_IN
if (ADCON0 & 0x08) {
// clear ADCI0 flag
ADCON0 &= ~0x08;
// read results from AD0DAT0 - AD0DAT3
newADC0 = AD0DAT0 >> 1; //<<results from 0 - 101... 0-30 behind pad...
#ifdef UNIT_11
//unit11
newADC1 = AD0DAT2;
#endif
#ifdef UNIT_XII
//unitXII
newADC1 = AD0DAT3;
#endif
}
if (oldADC0 != newADC0) {
if (STATE_0 == 1) { //try to think of some way to mix this with a +/- detune of midi....
if (newADC0 >= 3) { //~70mV "off"
i = newADC0 - 3;
if (i >= LUT_NUM_NOTES) { //saturate at the top
i = LUT_NUM_NOTES-1;
}
LPeriod = LUTFreq[i];
periodH1 = (0xff & (LUTFreq[i] >> 8));
periodL1 = (0xff & LUTFreq[i]);
i = 0;
AUDIO_L_ON = 1;
} else { // midi pass????
// periodH1 = 0xff;
// periodL1 = 0xff;
// LPeriod = 0xffff;
// AUDIO_L_ON = 0;
}
} else if (txOffSwitch == 0 && (newADC0 >> 1 != (oldADC0 >> 1))) {
setFreq(station + (newADC0 >> 1)); //vary station by 6.0 Mhz max [a 1.5 MHz swing w/ 3V into HVCV]
}
oldADC0 = newADC0;
}
if (enableTxCVGate == 1) { //1.2Volts at input ~ 5V gate through -12dB pad - 3V will trigger at peak too
if (newADC1 > 55) {
txOffSwitch = 1;
LED = 0;
} else {
txOffSwitch = 0;
LED = 1;
}
}
#endif
#ifdef BASIC_TX
//was number of transmit channels changed
if (stereoTx != STEREO) {
stereoTx = STEREO;
txProg();
}
if (BUTT_EN == 1) {
if (midButt == 0) {
// LED ^= 1;
txVcc ^= 1;
if(txVcc == 1) {
txOffSwitch = 1;
LED = 0;
} else {
txOffSwitch = 0;
LED = 1;
delay(50);
setFreq(station);
}
delay(UINT_MAX);
}
if (txVcc == 0) {
if (hiButt == 0) {
LED = 0;
delay(UINT_MAX);
station++;
setFreq(station);
LED = 1;
delay(UINT_MAX);
}
else if (loButt == 0) {
LED = 0;
delay(UINT_MAX);
--station;
setFreq(station);
LED = 1;
delay(UINT_MAX);
}
if (station < MIN_FREQ) {
station = MIN_FREQ;
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
}
if (station > MAX_FREQ) {
station = MAX_FREQ;
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
LED = 0;
delay(UINT_MAX/2);
LED = 1;
delay(UINT_MAX/2);
}
}
}
#endif
}
#endif
}
}
/*******************SETUP FUNCTION*********************/
void setup() {
//power_brownoutenable(POWER_BOINTERRUPT);
//eeprom_init();
P0M1 = 0x07; //input for buttons and audioN, push pull for audioL
P0M2 = 0x08;
P1M1 = 0; //uart_init takes care of TX/RX
// P1M2 |= 0x04; //configure t0 [p1^2] as push pull
P2M1 = 0;
P3M1 = 0;
//adc and dac taken care of later
midButt = 1; //pull up resistors on the button inputs
hiButt = 1;
loButt = 1;
LED = 1; //LED on
txVcc = 1; //transmitter off
CE = 0; //serial input inable for transmitter
stereoTx = STEREO;
audioL = 0;
test0 = 1; //always
test1 = 0; //always
phaseMode0 = 0; //?
phaseMode1 = 0; //?
#if defined(DAC1_OUT_AUDIO) || defined (DAC1_OUT_VCC)
//configure DAC1 out
// set dac1 pin to input only (disables digital output)
P0M1 |= 0x10;
P0M2 &= ~0x10;
// init dac1 value to zero
AD1DAT3 = 0x00;
// enable dac1 output
ADMODB |= 0x08;
// enable adc1 (also enables dac1)
ADCON1 |= 0x04;
#ifdef COORD
AD1DAT3 = 0xFF; //LUTSIN128[dac1LUTdex++ & 0x7F]; //todo add counter to not multiply clk
#endif
#else //set hiZ
// P0M1 |= 0x10;
// P0M2 &= ~0x10;
#endif
// configure timers
TMOD &= 0x00; //clear conf for timers
TAMOD &= 0xEE; //clear conf for timers
TMOD |= 0x11; //16bit mode for both
//TAMOD |= 0x10;
periodH1 = 0;
periodL1 = 0;
// timer values
TH0 = 0;
TL0 = 0;
TH1 = periodH1;
TL1 = periodL1;
//timer 0 interrupt is higher than timer 1 thus timer 0 is tempo and t1 is pitch
#ifdef COORD
//set timer0 priority to 2, below tx
IP0 &= ~(0x02);
IP0H &= ~(0x02);
//CLK OUT
#endif
// set timer 1 isr priority to 2 and timer 0 to priority 3
IP0 &= ~(0x80);
IP0H &= ~(0x00);
// // enable timer 1 interrupt
ET1 = 1;
ET0 = 1;
#ifdef ADC_IN
adc_init();
#ifdef UNIT_11
//unit11
adc_startadc0conversion(ADC_IMMEDIATE, ADC_AUTOSCANCONT, ADC0_CHANNEL0 | ADC0_CHANNEL2);
#endif
#ifdef UNIT_XII
//unit XII
adc_startadc0conversion(ADC_IMMEDIATE, ADC_AUTOSCANCONT, ADC0_CHANNEL0 | ADC0_CHANNEL3);
#endif
#endif
// // set isr priority to 0
// IP1 &= 0x7F;
// IP1H &= 0x7F;
// // enable adc interrupt ???
// EAD = 1;
}
/*********************TIMER interrupts****************************/
void timers_isr1 (void) interrupt 3 using 2
{
#ifdef COORD
//reload with som const todo
//todo explore 8 bit timer mode
// TH1 = 0xfe;//periodH1;
//TL1 = periodL1; //is 0xff - the following
// TL1 = 0x2e; // we think is close to 256us
//TL0++; //didint work....
//TL0 = TL0 + 1; //didnt work either...
//T0 = 0;
// P1^2 ^= 1; //also didnt work in conjunction with count mode...
//todo test...
//#ifdef DAC0_OUT //todo test moving before ifdef
// AD0DAT3 = LUTSIN128[dac0LUTdex++ & 0x7F];
//#endif
#else
//reload
TH1 = periodH1; //remember this only counts up!!!!
TL1 = periodL1;
//Match to other ISR
_nop_();
_nop_();
_nop_();
//generate square waves
if(AUDIO_L_ON) { //could play with nops here
audioL ^= 1;
}
#ifdef DAC1_OUT_AUDIO
AD1DAT3 = LUTSIN[dac1LUTdex++ & LUTSINMASK];
#else
//just for the delay...
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
#endif
#endif
}
void timers_isr0 (void) interrupt 1 using 3
{
#ifdef COORD
if (cnt0-- == 0) {
uart_transmit(TIMING_CLOCK);
if (deltaPos == 0) {
updateNote();
} else { //parse song
--deltaPos;
}
cnt0 = temp0;
TH0 = periodH0;
TL0 = periodL0;
//#ifdef DAC1_OUT
// if ( (++midiClk % 0x0C) == 0 ) {
// AD1DAT3 ^= 0xFF; //LUTSIN128[dac1LUTdex++ & 0x7F]; //todo add counter to not multiply clk
// }
//#else
++midiClk;
//#endif
} else {
TH0 = 0;
TL0 = 0;
}
#else
//reload
TH0 = periodH0;
TL0 = periodL0;
//generate square waves
if (!txOffSwitch) {
if(TX_VCC_ON) {
txVcc ^= 1;
} else {
txVcc = 0; //keep the transmitter ON!!!
}
} else {
txVcc = 1; //tx OFF! station???
}
#ifdef DAC1_OUT_VCC
AD1DAT3 = LUTSIN[dac1LUTdex++ & LUTSINMASK];
#else
//just for the delay...
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
#endif
#endif
}
//Plays first riff one extra time!!!
void updateNote(void) {
byte temp, temp2;
word thisDelta = 0;
bit thisUp = 0, VnotL = 0;
UPDATE_NOTE:
if (nextNote >= numNotes) { //equal to catches init!!!
nextNote = 1; //skip the riff len header
if (curRiffCnt-- == 0) {
if (++nextRiff >= numRiffs) { //next riff is newRiff
//end of SONG!!!!
PLAYING = 0; //midi says stop
midiClk = 0;
LED = 0;
//going silent but leave transmitter on for less static
AUDIO_L_ON = 0;
TX_VCC_ON = 0;
txOffSwitch = 0;
//ensure that we always fall through to this point
nextRiff = numRiffs - 1;
curRiffCnt = 0;
nextNote = numNotes;
SONG_DONE = 1;
if (LOOP_SONGS == 1) { //looP songs, reset everybody, looPSongs only for coord
// uart_transmit(SONG_SELECT);
// uart_transmit(songNum);
// uart_transmit(START);
//curSong = songBook[songNum];
nextRiff = 0;
deltaPos = 0; //trigger update
numRiffs = (curSong[nextRiff]).rAddy; //grab song length! dont inc it.. update will
//loop song is set already, but this would parse other flags... And we will consitently inc here
if ((curSong[nextRiff++]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
numNotes = 0; //numNotes=nextNote skiPs songlength header, maybe hide init info like looPsongs in rePs field
nextNote = 1; //but this breaks that flow so we have to force it forward
riff = (byte*) (curSong[nextRiff]).rAddy;//grab the physical addy of first riff
curRiffCnt = (curSong[nextRiff]).repeats;//grabs number of time riff repeats
numNotes = riff[0]; //grab the length of the riff
SONG_DONE = 0;
PLAYING = 1;
// TR0 = 1; //start playing
LED = 1;
goto UPDATE_NOTE;
}
#ifdef COORD
else {
TR0 = 0; //stop playing
uart_transmit(STOP);
}
#endif
} else {
riff = (byte*) (curSong[nextRiff]).rAddy;//grab the physical addy of first riff
curRiffCnt = (curSong[nextRiff]).repeats;//grabs number of time riff repeats
numNotes = riff[0]; //grab the length of the riff
}
}
}
deltaPos = riff[nextNote++]; //grab deleta T for note
temp = (riff[nextNote++]); //nextNote is now next note
#ifdef COORD
//this can likely be very much more optomized!!!!! todo
if (PLAYING) {
if (temp & 0x80) {
if (temp < 0xf0) {
switch (temp & 0xf0) { //find CC and PC and the future todos...
case CONTROL:
temp2 = riff[nextNote++];
switch(temp2) { //this could become a big switch!!!
case GENERAL_BUTTON_1_on:
//delay(900); //didnt help with bad sync on txToggle
case GENERAL_SLIDER_1_hi: //todo keep thinking about 14 bit values...
case GENERAL_SLIDER_1_lo:
case PAN_POSN_hi:
default:
uart_transmit(temp);
uart_transmit(temp2);
uart_transmit(riff[nextNote++]);
break;
// case SOUND_RELEASE_TIME: //todo a sustain structure!
// break;
}
break;
case PROGRAM:
case PITCH_WHEEL:
case NOTE_ON:
case NOTE_OFF:
case CHAN_PRESSURE:
case KEY_PRESSURE:
default:
break;
}
} else {
switch (temp) {
case SYSTEM_EXCLUSIVE: // variable length until terminated by an EOX or any status byte
sysIx = 0;
//todo in COORD/file sysEx
//uart_transmit(temp);
break;
case SONG_POSITION: //beam out the 14-16 bit songPos LSB first
// uart_transmit(temp);
// uart_transmit(riff[nextNote++]);
// uart_transmit(riff[nextNote++]);
//todo COORD scan and fix for everybody...
break;
case SONG_SELECT: //new Song in this Song!!!!! Untested
curSong = songBook[nextNote]; //nextNote is song number!!!
uart_transmit(temp);
uart_transmit(riff[nextNote++]);
midiClk = 0;
deltaPos = 0;
nextRiff = 0;
numRiffs = (curSong[nextRiff]).rAddy;
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
PLAYING = 0;
TR0 = 0;
break;
case START: //from the beginning
midiClk = 0;
deltaPos = 0;
nextRiff = 0;
numRiffs = (curSong[nextRiff]).rAddy;
if ((curSong[nextRiff]).repeats & LOOP_SONG_F) {
LOOP_SONGS = 1;
} else {
LOOP_SONGS = 0;
}
curRiffCnt = 0;
numNotes = 0;
nextNote = 0;
uart_transmit(temp);
PLAYING = 1;
TR0 = 1;
break;
case STOP: //this will cease the whole #! till the button is pressed again
PLAYING = 0;
uart_transmit(temp);
TR0 = 0;
break;
case CONTINUE:
PLAYING = 1;
uart_transmit(temp);
TR0 = 1;
break;
case SYSTEM_RESET:
uart_transmit(temp);