source: firmware/FSC/src/ad7719_adc.c@ 16071

Last change on this file since 16071 was 10697, checked in by neise, 14 years ago
File size: 5.7 KB
Line 
1//-----------------------------------------------------------------------------
2
3#include "ad7719_adc.h"
4#include "spi_master.h"
5
6//-----------------------------------------------------------------------------
7
8void ad7719_init(void)
9{
10 // setup the SPI interface according to AD7710 specs.
11 // This is needed, because W5100 has different SPI specs.
12 spi_setup_ad7719();
13
14
15 // ADC communiaction works like this:
16 // a write operation to the COM register takes place - telling the device what up next.
17 // a write or read operation to another register takes place
18 // COM register bits have the following meaning:
19 //
20 // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
21 // |#WEN |R/#W | zero| zero| A3 | A2 | A1 | A0 |
22 //
23 // #WEN (inversed write enable) must be zero inorder to clock more bits into the SPI interface.
24 // R/#W (read / not write) must be zero if the next operation will be a WRITE. one if the next is READ.
25 // A3-A0 denote the address of the next register.
26
27
28 CLR_BIT(ADC_PRT,ADC_RST); // Reset ADC (active low)
29 SET_BIT(ADC_PRT,ADC_RST); // Stop Reset ADC
30
31
32 CLR_BIT(PORTD,SPI_AD_CS);
33 spi_transfer_byte(FILTER_RD); // Next operation is a read from filter register
34 SET_BIT(PORTD,SPI_AD_CS);
35#ifdef AD7719_COM_DELAY_NEEDED
36_delay_us(50);
37#endif
38
39 CLR_BIT(PORTD,SPI_AD_CS);
40 spi_transfer_byte(0); // now this read takes place
41 SET_BIT(PORTD,SPI_AD_CS);
42#ifdef AD7719_COM_DELAY_NEEDED
43_delay_us(50);
44#endif
45
46 CLR_BIT(PORTD,SPI_AD_CS);
47 spi_transfer_byte(IOCON_WR); // Next Operation is write to IOCON -- this is a 2byte register
48 SET_BIT(PORTD,SPI_AD_CS);
49
50#ifdef AD7719_COM_DELAY_NEEDED
51_delay_us(50);
52#endif
53
54 CLR_BIT(PORTD,SPI_AD_CS);
55 spi_transfer_byte(IOCON_INIT_HIGH); // Write to IOCON1
56 SET_BIT(PORTD,SPI_AD_CS);
57#ifdef AD7719_COM_DELAY_NEEDED
58_delay_us(50);
59#endif
60 CLR_BIT(PORTD,SPI_AD_CS);
61 spi_transfer_byte(IOCON_INIT_LOWBYTE); // Write to IOCON2
62 SET_BIT(PORTD,SPI_AD_CS);
63
64#ifdef AD7719_COM_DELAY_NEEDED
65_delay_us(50);
66#endif
67
68 CLR_BIT(PORTD,SPI_AD_CS);
69 spi_transfer_byte(FILTER_WR); // Next Operation is write to FILTER
70 SET_BIT(PORTD,SPI_AD_CS);
71
72#ifdef AD7719_COM_DELAY_NEEDED
73_delay_us(50);
74#endif
75 CLR_BIT(PORTD,SPI_AD_CS);
76 spi_transfer_byte(FILTER_INIT); // Set Filter register to 0x52 --> good for 50Hz noise rejection.
77 SET_BIT(PORTD,SPI_AD_CS);
78#ifdef AD7719_COM_DELAY_NEEDED
79_delay_us(50);
80#endif
81 CLR_BIT(PORTD,SPI_AD_CS);
82 spi_transfer_byte(AD1CON_WR); // Next Operation is write to AD1CON
83 SET_BIT(PORTD,SPI_AD_CS);
84
85#ifdef AD7719_COM_DELAY_NEEDED
86_delay_us(50);
87#endif
88
89 CLR_BIT(PORTD,SPI_AD_CS);
90 spi_transfer_byte(AD1CON_INIT); // auxilliary ADC is not used in this application --> AD7719 data sheet for more info about aux. ADC
91 SET_BIT(PORTD,SPI_AD_CS);
92
93#ifdef AD7719_COM_DELAY_NEEDED
94_delay_us(50);
95#endif
96
97 CLR_BIT(PORTD,SPI_AD_CS);
98 spi_transfer_byte(AD0CON_WR); // Next Operation is write to AD0CON Start SPI
99 SET_BIT(PORTD,SPI_AD_CS);
100
101#ifdef AD7719_COM_DELAY_NEEDED
102_delay_us(50);
103#endif
104
105 CLR_BIT(PORTD,SPI_AD_CS);
106 spi_transfer_byte(AD0CON_INIT); // Write to AD0CON
107 SET_BIT(PORTD,SPI_AD_CS);
108
109#ifdef AD7719_COM_DELAY_NEEDED
110_delay_us(50);
111#endif
112
113 CLR_BIT(PORTD,SPI_AD_CS);
114 spi_transfer_byte(MODE_WR); // Next Operation is write to MODE
115 SET_BIT(PORTD,SPI_AD_CS);
116
117#ifdef AD7719_COM_DELAY_NEEDED
118_delay_us(50);
119#endif
120
121 CLR_BIT(PORTD,SPI_AD_CS);
122 spi_transfer_byte(MODE_CONT); // Set mode to: continuous sampling.
123 SET_BIT(PORTD,SPI_AD_CS);
124
125#ifdef AD7719_COM_DELAY_NEEDED
126_delay_us(50);
127#endif
128
129// these
130 CLR_BIT(PORTD,SPI_AD_CS);
131 spi_transfer_byte(FILTER_RD); // Next Operation is read from Filter register...
132 SET_BIT(PORTD,SPI_AD_CS);
133
134#ifdef AD7719_COM_DELAY_NEEDED
135_delay_us(50);
136#endif
137
138 CLR_BIT(PORTD,SPI_AD_CS); // the reading takes place here
139 spi_transfer_byte(0);
140 SET_BIT(PORTD,SPI_AD_CS);
141
142#ifdef AD7719_COM_DELAY_NEEDED
143_delay_us(50);
144#endif
145}
146
147void startconv(U08 continuous)
148{
149 // setup the SPI interface according to AD7710 specs.
150 // This is needed, because W5100 has different SPI specs.
151 spi_setup_ad7719();
152
153 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
154 spi_transfer_byte(MODE_WR); // Next Operation is write to Mode Register
155 SET_BIT(PORTD,SPI_AD_CS);
156 CLR_BIT(PORTD,SPI_AD_CS);
157 if (continuous) spi_transfer_byte(MODE_SINGLE); // Start new A/D conversion
158 else spi_transfer_byte(MODE_CONT); // Start continous conversion mode
159 SET_BIT(PORTD,SPI_AD_CS);
160}
161
162void stopconv(void)
163{
164 // setup the SPI interface according to AD7710 specs.
165 // This is needed, because W5100 has different SPI specs.
166 spi_setup_ad7719();
167
168 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
169 spi_transfer_byte(MODE_WR); // Next Operation is write to Mode Register
170 SET_BIT(PORTD,SPI_AD_CS);
171 CLR_BIT(PORTD,SPI_AD_CS);
172 spi_transfer_byte(MODE_IDLE);
173 SET_BIT(PORTD,SPI_AD_CS);
174}
175
176
177U32 read_adc(void)
178{
179 // setup the SPI interface according to AD7710 specs.
180 // This is needed, because W5100 has different SPI specs.
181 spi_setup_ad7719();
182
183 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
184 spi_transfer_byte(AD0DAT_RD); // Next Operation is read from Main ADC Data Register
185 SET_BIT(PORTD,SPI_AD_CS);
186
187 #ifdef AD7719_COM_DELAY_NEEDED
188 _delay_us(50);
189 #endif
190
191 CLR_BIT(PORTD,SPI_AD_CS);
192 U32 value=0; // actually a 24bit value is returned
193 value |= spi_transfer_byte(0) ;
194 value =value<<8;
195 value |= spi_transfer_byte(0) ;
196 value =value<<8;
197 value |= spi_transfer_byte(0) ;
198 SET_BIT(PORTD,SPI_AD_CS); // Set CS high
199 return value;
200}
201
202
Note: See TracBrowser for help on using the repository browser.