Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_analog.h
Go to the documentation of this file.
1 
71 #ifndef NZ_ANALOG_H
72 #define NZ_ANALOG_H
73 
74 #if defined(HAS_NZ_ADC)
75 
79 #define CONVERT_MV_TO_10BIT_ADC_VALUE(mv) ((WORD) ( (((float)mv)/((float)ADC_REF_MV)) * 1023.0) )
80 
81 //Number of filter stages for each ADC channel. Must be 2, 4 or 8. For each ADC channel, this many
82 //words are needed. Total required memory is ADC_MAX_CHAN x ADC_FILTERS words.
83 #ifndef ADC_FILTER_STAGES
84 #define ADC_FILTER_STAGES 4
85 #endif
86 
87 //ADC Interrupt priority, a value from 0 to 7. 0 will disable interrupt, 7 is highest priority.
88 #ifndef nzINT_PRIORITY_ADC1
89 #define nzINT_PRIORITY_ADC1 4
90 #endif
91 
92 #ifndef ADC_MAX_CHAN
93 #define ADC_MAX_CHAN ADC_MAX_CHAN_BRD
94 #endif
95 
96 //The channel mask indicates what AN channels can be used for ADC inputs
97 #ifndef ADC_CHANNEL_MASK_LOW
98 #define ADC_CHANNEL_MASK_LOW ADC_CHANNEL_MASK_LOW_BRD
99 #endif
100 
101 //The channel mask indicates what AN channels can be used for ADC inputs
102 #ifndef ADC_CHANNEL_MASK_HIGH
103 #define ADC_CHANNEL_MASK_HIGH ADC_CHANNEL_MASK_HIGH_BRD
104 #endif
105 
106 
110 typedef struct __attribute__((aligned(2), packed))
111 {
112  BYTE count; //Number of ADC channels
113  BYTE currentFilter; //Value from 0 to (ADC_FILTER_STAGES - 1)
114 
115  union {
116  struct
117  {
118  unsigned int bDataValid : 1; //If set, getVarCmd() returned all requested data
119  unsigned int bFill : 15; //Fill to ensure flags take up a 16-bit WORD
120  } flags;
121  WORD flagsVal;
122  };
123 
124  //Bits indicating enabled ADC channels. Each bit represents an ADC channel, ex: bit 0=AN0, 1=AN1, 15=AN15....
125  //If ADC_CHANNEL_MASK_HIGH == 0, upper word is not used!
126  // - Is initialized with ADC_CHANNEL_MASK_LOW, giving all possible ADC channels
127  // - Then, in adcOpen() is masked with used channels. After adcOpen() contains only opened channels.
128  DWORD_VAL channels;
129 
130  //WORD adcValues[ADC_MAX_CHAN]; //Filtered ADC values
131 
132  //Array of ADC filters. For each channel, ADC_FILTER_STAGES WORDs are reserved. Only used channels require
133  //reserved adcFilter positions
134  WORD adcFilter[ADC_FILTER_STAGES][ADC_MAX_CHAN];
135 
136  WORD conversionCount; //Free running counter, get's incremented with each ISR
137 } ADC_INFO;
138 
139 
140 //This is used by others
141 #ifndef THIS_IS_NZ_ANALOG_C
142 extern ADC_INFO adcInfo;
143 #endif
144 
145 
185 void adcOpenExtra(DWORD adcChannels, BYTE conversionClock, BYTE sampleTime);
186 
187 
202 #define adcOpen(adcChannels) adcOpenExtra(adcChannels, 64, 31)
203 
204 
205 
211 #define adcStart() {_AD1IF = 0; _ASAM = 1;}
212 
213 
218 void adcStop(void);
219 
220 
235 WORD adcReadChanForIndex(BYTE index);
236 
237 
252 #define adcReadChan8BitForIndex(index) ((BYTE)(adcReadChanForIndex(index)>>2))
253 
254 
265 WORD adcReadChan(BYTE ch);
266 
267 
278 #define adcReadChan8Bit(ch) ((BYTE)(adcReadChan(ch)>>2))
279 
280 
291 WORD adcReadChanMvForIndex(BYTE ch);
292 
293 
304 WORD adcReadChanMv(BYTE ch);
305 
306 
321 WORD adcConvertChan(BYTE ch, BYTE times);
322 
323 
343 WORD adcConvertChanForIndex(BYTE index, BYTE times);
344 
345 
360 WORD adcConvertChanMv(BYTE ch, BYTE times);
361 
362 
382 WORD adcConvertChanMvForIndex(BYTE index, BYTE times);
383 
384 
388 #define adcIntEnable() (_AD1IE = 1)
389 
390 
394 #define adcIntDisable() (_AD1IE = 0)
395 
399 #define adcGetConversionCount() (adcInfo.conversionCount)
400 
405 #define adcGetFilterStages() (ADC_FILTER_STAGES)
406 
407 #endif //#if defined(HAS_NZ_ADC)
408 #endif //#ifndef NZ_ANALOG_H