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

Last change on this file since 18342 was 17633, checked in by dneise, 11 years ago
a lot of whitespace changes and removed unneeded includes
File size: 5.8 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 CLR_BIT(PORTD,SPI_AD_CS);
130 spi_transfer_byte(FILTER_RD); // Next Operation is read from Filter register...
131 SET_BIT(PORTD,SPI_AD_CS);
132
133#ifdef AD7719_COM_DELAY_NEEDED
134 _delay_us(50);
135#endif
136
137 CLR_BIT(PORTD,SPI_AD_CS); // the reading takes place here
138 spi_transfer_byte(0);
139 SET_BIT(PORTD,SPI_AD_CS);
140
141#ifdef AD7719_COM_DELAY_NEEDED
142 _delay_us(50);
143#endif
144}
145
146void startconv(U08 continuous)
147{
148 // setup the SPI interface according to AD7710 specs.
149 // This is needed, because W5100 has different SPI specs.
150 spi_setup_ad7719();
151
152 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
153 spi_transfer_byte(MODE_WR); // Next Operation is write to Mode Register
154 SET_BIT(PORTD,SPI_AD_CS);
155 CLR_BIT(PORTD,SPI_AD_CS);
156 if (continuous) spi_transfer_byte(MODE_SINGLE); // Start new A/D conversion
157 else spi_transfer_byte(MODE_CONT); // Start continous conversion mode
158 SET_BIT(PORTD,SPI_AD_CS);
159}
160
161void stopconv(void)
162{
163 // setup the SPI interface according to AD7710 specs.
164 // This is needed, because W5100 has different SPI specs.
165 spi_setup_ad7719();
166
167 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
168 spi_transfer_byte(MODE_WR); // Next Operation is write to Mode Register
169 SET_BIT(PORTD,SPI_AD_CS);
170 CLR_BIT(PORTD,SPI_AD_CS);
171 spi_transfer_byte(MODE_IDLE);
172 SET_BIT(PORTD,SPI_AD_CS);
173}
174
175
176U32 read_adc(void)
177{
178 // setup the SPI interface according to AD7710 specs.
179 // This is needed, because W5100 has different SPI specs.
180 spi_setup_ad7719();
181
182 CLR_BIT(PORTD,SPI_AD_CS); // Set CS low
183 spi_transfer_byte(AD0DAT_RD); // Next Operation is read from Main ADC Data Register
184 SET_BIT(PORTD,SPI_AD_CS);
185
186 #ifdef AD7719_COM_DELAY_NEEDED
187 _delay_us(50);
188 #endif
189
190 CLR_BIT(PORTD,SPI_AD_CS);
191 U32 value=0; // actually a 24bit value is returned
192 value |= spi_transfer_byte(0) ;
193 value =value<<8;
194 value |= spi_transfer_byte(0) ;
195 value =value<<8;
196 value |= spi_transfer_byte(0) ;
197 SET_BIT(PORTD,SPI_AD_CS); // Set CS high
198 return value;
199}
200
201
Note: See TracBrowser for help on using the repository browser.