forked from wespo/analogShield
-
Notifications
You must be signed in to change notification settings - Fork 6
/
analogShield.cpp
948 lines (821 loc) · 25.1 KB
/
analogShield.cpp
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
/************************************************************************/
/* */
/* analogShield.h -- Library for Analog Shield */
/* Version - 2.1 */
/* */
/************************************************************************/
/* Author: William J. Esposito */
/* Copyright 2014, Digilent Inc. */
/************************************************************************/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/************************************************************************/
/* Module Description: */
/* */
/* This module contains the implementation of the object class that */
/* forms a chipKIT/Aruino interface to the Analog Shield functions of */
/* the Texas Instrument chipKit/arduino shield. */
/* */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 04/22/2014(WilliamE): Created */
/* 05/27/2014(MarshallW): Modified for readability and content */
/* 02/16/2015(MarshallW): ChipKIT Efficiency update! */
/* */
/* Todo: */
/* - Framework for DUE added but not tested */
/* */
/************************************************************************/
/* ------------------------------------------------------------ */
/* Include File Definitions */
/* ------------------------------------------------------------ */
#include <analogShield.h>
analogShield analog;
/* ------------------------------------------------------------ */
/* analogShield::analogShield
**
** Synopsis:
** none
** Parameters:
** none
** Errors:
** none
**
** Description:
** Default constructor that sets the tristate buffers
** for the ADC and DAC control pins
**
**
*/
#if defined(__PIC32MX__)
analogShield::analogShield() :
ADCCSSet(portOutputRegister(digitalPinToPort(adccs)) + 2),
ADCCSClr(portOutputRegister(digitalPinToPort(adccs)) + 1),
ADCCSPinMask(digitalPinToBitMask(adccs)),
syncPinSet(portOutputRegister(digitalPinToPort(syncPin)) + 2),
syncPinClr(portOutputRegister(digitalPinToPort(syncPin)) + 1),
syncPinPinMask(digitalPinToBitMask(syncPin)),
ldacPinSet(portOutputRegister(digitalPinToPort(ldacPin)) + 2),
ldacPinClr(portOutputRegister(digitalPinToPort(ldacPin)) + 1),
ldacPinPinMask(digitalPinToBitMask(ldacPin)),
ADCBusyPtr(portInputRegister(digitalPinToPort(adcbusy))),
ADCBusyPinMask(digitalPinToBitMask(adcbusy)) {
#elif defined(__SAM3X8E__)
analogShield::analogShield() :
ADCCSPtr(portOutputRegister(digitalPinToPort(adccs))),
SetADCCSPinMask(digitalPinToBitMask(adccs)),
ClrADCCSPinMask(0xffffffff^(digitalPinToBitMask(adccs))),
syncPinPtr(portOutputRegister(digitalPinToPort(syncPin))),
SetsyncPinPinMask(digitalPinToBitMask(syncPin)),
ClrsyncPinPinMask(0xffffffff^(digitalPinToBitMask(syncPin))),
ldacPinPtr(portOutputRegister(digitalPinToPort(ldacPin))),
SetldacPinPinMask(digitalPinToBitMask(ldacPin)),
ClrldacPinPinMask(0xffffffff^(digitalPinToBitMask(ldacPin))),
ADCBusyPtr(portInputRegister(digitalPinToPort(adcbusy))),
ADCBusyPinMask(digitalPinToBitMask(adcbusy)) {
#elif defined(__AVR__)
analogShield::analogShield() {
#endif
//initialize the ADC and DAC pins
//ADC pins
pinMode(adccs, OUTPUT);
digitalWrite(adccs, HIGH);
pinMode(adcbusy, INPUT);
//DAC pins
pinMode(syncPin, OUTPUT);
pinMode(ldacPin, OUTPUT);
digitalWrite(syncPin,HIGH);
digitalWrite(ldacPin,LOW);
shieldMode = 0;
begin();
}
/* ------------------------------------------------------------ */
/* analogShield::begin
**
** Synopsis:
** begin();
**
** Parameters:
** none
**
** Return Values:
** none
**
** Errors:
**
** Description:
** Initializes the SPI interface on the microcontroller
**
**
*/
void analogShield::begin(){
#if defined(__PIC32MX__)
SPI.begin();
#elif defined (__SAM3X8E__)
SPI.begin();
#else //(__AVR__)
// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);
// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
pinMode(SS, OUTPUT);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
#endif
}
/* ------------------------------------------------------------ */
/* analogShield::end
**
** Synopsis:
** end();
**
** Parameters:
** none
**
** Return Values:
** none
**
** Errors:
**
** Description:
** Closes the SPI communication interface
**
**
*/
void analogShield::end(){
#if defined(__PIC32MX__) || defined (__SAM3X8E__)
SPI.end();
#else //(__AVR__)
SPCR &= ~_BV(SPE);
#endif
}
/* ------------------------------------------------------------ */
/* Helper Functions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* analogShield::setChannelAndModeByte
**
** Synopsis:
** setChannelAndModeByte(channel, mode);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** bool mode - boolean to activate differential mode
**
** Return Values:
** none
**
** Errors:
** none
** Description:
** Helper function that sets the channel and mode
** control byte and sent it over SPI
**
**
*/
void analogShield::setChannelAndModeByte(byte channel, bool mode){
//control byte
//S - sentinel, always 1
//A2 - channel select
//A1 - channel select
//A0 - channel select
//- N/C
//Single / Diff pair
//PD1 - power down mode
//PD0 - power down mode
byte control = B10000010; //default to channel 1 '001'
//channel mask
if(channel == 3){
control = control | B11100000;
}
else if(channel == 2){
control = control | B10100000;
}
else if(channel == 1){
control = control | B11010000;
}
else if(channel == 0){
control = control | B00010000;
}
//differential mode active
if(mode){
control = control & B11111011;
}
else{
control = control | B00000100;
}
#if defined(__PIC32MX__) || defined(__SAM3X8E__)
SPI.transfer(control);
#else //(__AVR__)
SPDR = control; //Shift Out the Control Command.
while (!(SPSR & _BV(SPIF)));
#endif
return;
}
/* ------------------------------------------------------------ */
/* ADC Functions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* analogShield::read
**
** Synopsis:
** read(channel, mode);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** bool mode - boolean to activate differential mode
**
** Return Values:
** unsigned int - Raw data from 0-65535 corresponding
** to the voltage presented to the ADC
**
** Errors:
** none
**
** Description:
** Reads a raw data value from the ADC using the
** SPI interface
**
**
*/
unsigned int analogShield::read(int channel, bool mode) {
// initialize SPI:
if(shieldMode != 2){
#if defined (__PIC32MX__)
SPI.setDataMode(SPI_MODE3);
#elif defined (__SAM3X8E__)
SPI.setDataMode(SPI_MODE3);
#else //(__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE3;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 2;
}
#if defined (__PIC32MX__) || (__SAM3X8E__)
// take the SS pin low to select the chip, transfer the command, then bring the bit back high:
#if defined (__PIC32MX__)
*ADCCSClr = ADCCSPinMask; //digitalWrite(adccs,LOW);
#elif defined (__SAM3X8E__)
*ADCCSPtr &= ClrADCCSPinMask;
#endif
setChannelAndModeByte(channel, mode);
#if defined (__PIC32MX__)
*ADCCSSet = ADCCSPinMask; //digitalWrite(adccs,HIGH);
#elif defined (__SAM3X8E__)
*ADCCSPtr |= SetADCCSPinMask;
#endif
//wait for busy signal to rise. If it lasts a while, try resending.
while(!(*ADCBusyPtr & ADCBusyPinMask)); //while(digitalRead(adcbusy) == 0); //wait for pin 3 to == 0
#if defined (__PIC32MX__)
*ADCCSClr = ADCCSPinMask; //digitalWrite(adccs,LOW);
#elif defined (__SAM3X8E__)
*ADCCSPtr &= ClrADCCSPinMask;
#endif
//collect data
byte high = SPI.transfer(0x00);
byte low = SPI.transfer(0x00);
//release chip select
#if defined (__PIC32MX__)
*ADCCSSet = ADCCSPinMask; //digitalWrite(adccs,HIGH);
#elif defined (__SAM3X8E__)
*ADCCSPtr |= SetADCCSPinMask;
#endif
//compile the result into a 32 bit integer.
int result;
result = (int)high<<24;
result+= low<<16;
//make into an unsigned int for compatibility with the DAC used on the analog shield.
if(result < 0)
{
result = result >> 16;
result &= 0x7FFF;
}
else
{
result = result >> 16;
result |= 0x8000;
}
return result;
#else //(__AVR__)
// take the SS pin low to select the chip, transfer the command, then bring the bit back high:
PORTD &= B11111011; //digitalWriteFast(adccs,LOW);
setChannelAndModeByte(channel, mode);
PORTD |= B00000100; //digitalWriteFast(adccs,HIGH);
//wait for busy signal to fall. If it lasts a while, try resending.
while((PIND & B00001000) == 0); //wait for pin 3 to == 0
//Result ready. Read it in
PORTD &= B11111011; //digitalWriteFast(adccs,LOW);
//collect data
SPDR = 0;
while (!(SPSR & _BV(SPIF)));
byte high = SPDR;
SPDR = 0;
while (!(SPSR & _BV(SPIF)));
byte low = SPDR;
//release chip select
PORTD |= B00000100; //digitalWriteFast(adccs,HIGH);
//compile the result into a 16 bit integer.
int result;
//result = (int)word(high, low);
result = (int)high<<8;
result+= low;
//make into an unsigned int for compatibility with the DAC used on the analog shield.
if(result < 0)
{
result &= 0x7FFF;
}
else
{
result |= 0x8000;
}
return result;
#endif
}
/* ------------------------------------------------------------ */
/* analogShield::signedRead
**
** Synopsis:
** signedRead(channel, mode);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** bool mode - boolean to activate differential mode
**
** Return Values:
** int - Signed data from -32768 - 32767 corresponding
** to the voltage presented to the ADC
**
** Errors:
** none
**
** Description:
** Reads a signed data value from the ADC using the
** SPI interface
**
**
*/
int analogShield::signedRead(int channel, bool mode) {
// initialize SPI:
if(shieldMode != 2){
#if defined (__PIC32MX__) || defined (__SAM3X8E__)
SPI.setDataMode(SPI_MODE3);
#else //(__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE3;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 2;
}
#if defined (__PIC32MX__) || defined (__SAM3X8E__)
// take the SS pin low to select the chip, transfer the command, then bring the bit back high:
#if defined (__PIC32MX__)
*ADCCSClr = ADCCSPinMask; //digitalWrite(adccs,LOW);
#elif defined (__SAM3X8E__)
*ADCCSPtr &= ClrADCCSPinMask;
#endif
setChannelAndModeByte(channel, mode);
#if defined (__PIC32MX__)
*ADCCSSet = ADCCSPinMask; //digitalWrite(adccs,HIGH);
#elif defined (__SAM3X8E__)
*ADCCSPtr |= SetADCCSPinMask;
#endif
//wait for busy signal to fall. If it lasts a while, try resending.
while(!(*ADCBusyPtr & ADCBusyPinMask)); //while(digitalRead(adcbusy) == 0); //wait for pin 3 to == 0
#if defined (__PIC32MX__)
*ADCCSClr = ADCCSPinMask; //digitalWrite(adccs,LOW);
#elif defined (__SAM3X8E__)
*ADCCSPtr &= ClrADCCSPinMask;
#endif
//collect data
byte high = SPI.transfer(0x00);
byte low = SPI.transfer(0x00);
//release chip select
#if defined (__PIC32MX__)
*ADCCSSet = ADCCSPinMask; //digitalWrite(adccs,HIGH);
#elif defined (__SAM3X8E__)
*ADCCSPtr |= SetADCCSPinMask;
#endif
#else //(__AVR__)
// take the SS pin low to select the chip, transfer the command, then bring the bit back high:
PORTD &= B11111011; //digitalWriteFast(adccs,LOW);
setChannelAndModeByte(channel, mode);
PORTD |= B00000100; //digitalWriteFast(adccs,HIGH);
//wait for busy signal to fall. If it lasts a while, try resending.
while((PIND & B00001000) == 0); //wait for pin 3 to == 0
//Result ready. Read it in
PORTD &= B11111011; //digitalWriteFast(adccs,LOW);
//collect data
SPDR = 0;
while (!(SPSR & _BV(SPIF)));
byte high = SPDR;
SPDR = 0;
while (!(SPSR & _BV(SPIF)));
byte low = SPDR;
//release chip select
PORTD |= B00000100; //digitalWriteFast(adccs,HIGH);
#endif
//compile the result into a 16 bit integer.
int result;
//result = (int)word(high, low);
result = (int)high<<8;
result+= low;
//make into an unsigned int for compatibility with the DAC used on the analog shield.
return result;
}
/* ------------------------------------------------------------ */
/* DAC Functions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* analogShield::writeNoUpdate
**
** Synopsis:
** writeNoUpdate(channel, value);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** unsigned int value - raw data value to write to
** the DAC
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes a raw data value to the selected DAC data
** buffer but does not update the DAC output
**
**
*/
void analogShield::writeNoUpdate(int channel, unsigned int value){
//prep work, break up the into into two 8 bit bytes
byte high = value >> 8;//highByte(value);
byte low = value & 0x00FF;//lowByte(value);
// take the SS pin low to select the chip:
#if defined(__PIC32MX__)
*syncPinClr = syncPinPinMask;
#elif defined (__SAM3X8E__)
*syncPinPtr &= ClrsyncPinPinMask;
#else
// take the SS pin low to select the chip:
PORTD &= B11011111; //digitalWriteFast(5,LOW);
#endif
// send in the address and value via SPI:
byte call = 0x00;//00
if(channel == 1)
call = 0x02;
else if(channel == 2)
call = 0x04;
else if(channel == 3)
call = 0x06;
//send command byte
#if defined(__PIC32MX__) || (__SAM3X8E__)
SPI.transfer(call);
//send data
SPI.transfer(high);
SPI.transfer(low);
// take the SS pin high to de-select the chip:
#if defined(__PIC32MX__)
*syncPinSet = syncPinPinMask;
#elif defined (__SAM3X8E__)
*syncPinPtr |= SetsyncPinPinMask;
#endif
#else //(__AVR__)
SPDR = call;
while (!(SPSR & _BV(SPIF)));
//send data
SPDR = high;
while (!(SPSR & _BV(SPIF)));
SPDR = low;
while (!(SPSR & _BV(SPIF)));
// take the SS pin high to de-select the chip:
PORTD |= B00100000; ////digitalWriteFast(5,HIGH);
#endif
}
/* ------------------------------------------------------------ */
/* analogShield::writeAllUpdate
**
** Synopsis:
** writeAllUpdate(channel, value);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** unsigned int value - raw data value to write to
** the DAC
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes a raw data value to the selected DAC data
** buffer and updates DAC output
**
**
*/
void analogShield::writeAllUpdate(int channel, unsigned int value){
//prep work, break up the into into two 8 bit bytes
byte high = value >> 8;//highByte(value);
byte low = value & 0x00FF;//lowByte(value);
// take the SS pin low to select the chip:
#if defined(__PIC32MX__)
*syncPinClr = syncPinPinMask;
#elif defined (__SAM3X8E__)
*syncPinPtr &= ClrsyncPinPinMask;
#else
// take the SS pin low to select the chip:
PORTD &= B11011111; //digitalWriteFast(5,LOW);
#endif
// send in the address and value via SPI:
byte call = 0x20;//20
if(channel == 1)
call = 0x22;
else if(channel == 2)
call = 0x24;
else if(channel == 3)
call = 0x26;
//send command byte
#if defined(__PIC32MX__) || defined (__SAM3X8E__)
SPI.transfer(call);
//send data
SPI.transfer(high);
SPI.transfer(low);
// take the SS pin high to de-select the chip:
#if defined(__PIC32MX__)
*syncPinSet = syncPinPinMask;
#elif defined (__SAM3X8E__)
*syncPinPtr |= SetsyncPinPinMask;
#endif
#else //(__AVR__)
SPDR = call;
while (!(SPSR & _BV(SPIF)));
//send data
SPDR = high;
while (!(SPSR & _BV(SPIF)));
SPDR = low;
while (!(SPSR & _BV(SPIF)));
// take the SS pin high to de-select the chip:
PORTD |= B00100000; ////digitalWriteFast(5,HIGH);
#endif
}
/* ------------------------------------------------------------ */
/* analogShield::write
**
** Synopsis:
** write(channel, value);
**
** Parameters:
** int channel - The channel being set from 0 to 3
** unsigned int value - raw data value to write to
** the DAC
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes a raw data value to the selected DAC data
** buffer and updates DAC output
**
**
*/
void analogShield::write(int channel, unsigned int value) {
if(shieldMode != 1)
{
// initialize SPI:
#if defined(__PIC32MX__) || defined (__SAM3X8E__)
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV2);
#else //(__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE1;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 1;
}
//prep work, break up the into into two 8 bit bytes
byte high = value >> 8;//highByte(value);
byte low = value & 0x00FF;//lowByte(value);
// take the SS pin low to select the chip:
#if defined(__PIC32MX__)
*syncPinClr = syncPinPinMask;
*ldacPinClr = ldacPinPinMask;
#elif defined (__SAM3X8E__)
*syncPinPtr &= ClrsyncPinPinMask;
*ldacPinPtr &= ClrldacPinPinMask;
#else
// take the SS pin low to select the chip:
PORTD &= B11011111; //digitalWriteFast(5,LOW);
#endif
// send in the address and value via SPI:
byte call = 0x10;
if(channel == 1)
call = 0x12;
else if(channel == 2)
call = 0x14;
else if(channel == 3)
call = 0x16;
#if defined(__PIC32MX__) || (__SAM3X8E__)
//send command byte
SPI.transfer(call);
//send data
SPI.transfer(high);
SPI.transfer(low);
// take the SS pin high to de-select the chip:
#if defined(__PIC32MX__)
*syncPinSet = syncPinPinMask; //digitalWrite(syncPin,HIGH);
#elif defined (__SAM3X8E__)
*syncPinPtr |= SetsyncPinPinMask;
#endif
//end();
#else //(__AVR__)
//send command byte
SPDR = call;
while (!(SPSR & _BV(SPIF)));
//send data
SPDR = high;
while (!(SPSR & _BV(SPIF)));
SPDR = low;
while (!(SPSR & _BV(SPIF)));
// take the SS pin high to de-select the chip:
PORTD |= B00100000; ////digitalWriteFast(5,HIGH);
//end();
#endif
}
/* ------------------------------------------------------------ */
/* analogShield::write
**
** Synopsis:
** write(value0, value1, simul);
**
** Parameters:
** unsigned int value0 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value1 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** bool simul - used to synchronize the DAC to update
** both channels at once
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes raw data values to the DAC.
** value0 => DAC channel 0
** value1 => DAC channel 1
**
*/
void analogShield::write(unsigned int value0, unsigned int value1, bool simul){
if(shieldMode != 1)
{
// initialize SPI:
#if defined (__PIC32MX__) || (__SAM3X8E__)
SPI.setDataMode(SPI_MODE1);
#else (__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE1;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 1;
}
writeNoUpdate(0,value0);
writeAllUpdate(1,value1);
}
/* ------------------------------------------------------------ */
/* analogShield::write
**
** Synopsis:
** write(value0, value1, value2, simul);
**
** Parameters:
** unsigned int value0 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value1 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value2 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** bool simul - used to synchronize the DAC to update
** both channels at once
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes raw data values to the DAC.
** value0 => DAC channel 0
** value1 => DAC channel 1
** value2 => DAC channel 2
**
**
*/
void analogShield::write(unsigned int value0, unsigned int value1, unsigned int value2, bool simul){
if(shieldMode != 1){
// initialize SPI:
#if defined (__PIC32MX__) || (__SAM3X8E__)
SPI.setDataMode(SPI_MODE1);
#else //(__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE1;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 1;
}
writeNoUpdate(0,value0);
writeNoUpdate(1,value1);
writeAllUpdate(2,value2);
}
/* ------------------------------------------------------------ */
/* analogShield::write
**
** Synopsis:
** write(value0, value1, value2, value3, simul);
**
** Parameters:
** unsigned int value0 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value1 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value2 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** unsigned int value3 - Raw value from 0 to 65535
** corresponding to -5V - 5V
** bool simul - used to synchronize the DAC to update
** both channels at once
**
** Return Values:
** none
**
** Errors:
** none
**
** Description:
** Writes raw data values to the DAC.
** value0 => DAC channel 0
** value1 => DAC channel 1
** value2 => DAC channel 2
** value3 => DAC channel 3
**
**
*/
void analogShield::write(unsigned int value0, unsigned int value1, unsigned int value2, unsigned int value3, bool simul){
if(shieldMode != 1)
{
// initialize SPI:
#if defined (__PIC32MX__) || (__SAM3X8E__)
SPI.setDataMode(SPI_MODE1);
#else //(__AVR__)
SPCR &= ~(_BV(DORD));
SPCR = (SPCR & ~SPI_MODE_MASK) | SPI_MODE1;
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPI_CLOCK_DIV2 & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((SPI_CLOCK_DIV2 >> 2) & SPI_2XCLOCK_MASK);
#endif
shieldMode = 1;
}
writeNoUpdate(0,value0);
writeNoUpdate(1,value1);
writeNoUpdate(2,value2);
writeAllUpdate(3,value3);
}