1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_MICROCHIP_DAC_SPI_h
11#define _MITOV_MICROCHIP_DAC_SPI_h
12
13#include <Mitov.h>
14#include <Mitov_Basic_SPI.h>
15
16namespace Mitov
17{
18//---------------------------------------------------------------------------
19 class Microchip_DAC_SPI : public Mitov::Basic_MultiChannel_SourceSPI
20 {
21 typedef Mitov::Basic_MultiChannel_SourceSPI inherited;
22
23 public:
24 OpenWire::SourcePin LatchOutputPin;
25
26 public:
27 void SetEnabled( bool AValue )
28 {
29 if( Enabled == AValue )
30 return;
31
32 Enabled = AValue;
33 for( int i =0; i < FChannels.size(); ++i )
34 FChannels[ i ]->InitChannel();
35 }
36
37 protected:
38 void UpdateLatch()
39 {
40 LatchOutputPin.SendValue( false );
41 LatchOutputPin.SendValue( true );
42 }
43
44 protected:
45 virtual void SystemLoopEnd()
46 {
47 inherited::SystemLoopEnd();
48
49 if( ! ClockInputPin.IsConnected() )
50 UpdateLatch();
51 }
52
53 virtual void DoClockReceive( void *_Data )
54 {
55 inherited::DoClockReceive( _Data );
56
57 UpdateLatch();
58 }
59
60 public:
61 void SPI_write( word APacket )
62 {
63 ChipSelectOutputPin.SendValue( false );
64 FSPI.transfer( APacket >> 8 );
65 FSPI.transfer( APacket & 0xFF );
66 ChipSelectOutputPin.SendValue( true );
67 }
68
69 public:
70 using inherited::inherited;
71
72 };
73//---------------------------------------------------------------------------
74 class Microchip_DACSPIChannel : public Mitov::Basic_Typed_SPIChannel<Microchip_DAC_SPI>
75 {
76 typedef Mitov::Basic_Typed_SPIChannel<Microchip_DAC_SPI> inherited;
77
78 public:
79 bool Enabled : 1;
80 bool Gain : 1;
81
82 public:
83 void SendOutput()
84 {
85 if( FNewValue == FValue )
86 return;
87
88 word volt_digits = FNewValue * 4095 + 0.5;
89
90 word packet = volt_digits & 0b111111111111; //shift voltage setting digits
91 if( Enabled )
92 packet |= 1 << 12;
93
94 if( Gain )
95 packet |= 1 << 13; //add gain setting
96
97 if( FIndex )
98 packet |= 1 << 15; //add gain setting
99
100 FOwner.SPI_write( packet );
101 }
102
103 public:
104 void SetEnabled( bool AValue )
105 {
106 if( Enabled == AValue )
107 return;
108
109 Enabled = AValue;
110 InitChannel();
111 }
112
113 void SetGain( bool AValue )
114 {
115 if( Gain == AValue )
116 return;
117
118 Gain = AValue;
119 InitChannel();
120 }
121
122 public:
123 Microchip_DACSPIChannel( Microchip_DAC_SPI &AOwner, int AIndex, int ABits ) :
124 inherited( AOwner, AIndex ),
125 Enabled( true ),
126 Gain( false )
127 {
128 }
129
130 };
131//---------------------------------------------------------------------------
132}
133
134#endif