Alarm Sensor Module  0.1
⇒ back to Project Page
adc.c
Go to the documentation of this file.
1 /***************************************************************************
31 ***************************************************************************/
32 
33 
34 
35 
36 #include <stdint.h>
37 #include <avr/io.h>
38 
39 #include "port_definitions.h"
40 #include "adc.h"
41 
42 
43 
44 /****************************************************************************/
72 /****************************************************************************/
73 
74 uint32_t single_adc_read(uint8_t vref_reg, uint16_t vref_val,
75  uint16_t number_cycles, uint16_t min_freq) {
76 
77  uint32_t ad_voltage;
78  uint16_t tmp_voltage;
79  uint16_t frequency_prescaler;
80  uint16_t i;
81 
82 
84  ad_voltage = ADC_UNKNOWN_ERROR;
85  ADCSRA = 0x00; //ADCSRA register = 0x00 --> ADC disabled completely
86 
87 
89 
90  //A/D pin needs the following configuration: input pin, internal pull-up resistor deactivated
91  if ((((ADC_DDR >> ADC_PIN) & 1) != 0) || (((ADC_PORT >> ADC_PIN) & 1) != 0)) {
92  return ADC_PIN_ERROR;
93  }
94 
95  //check number of requested conversion cycles
96  if ((number_cycles > MAX_NUMBER_ADC_CYCLES) || (number_cycles < 1)) {
98  }
99 
100  //check given ADC frequency
101  if ((min_freq > MAX_ADC_FREQUENCY) || (min_freq < MIN_ADC_FREQUENCY)) {
102  return ADC_FREQUENCY_ERROR;
103  }
104 
105 
107 
108  //set ADMUX register
109  ADMUX = ADC_PIN | (vref_reg << REFS0);
110 
111  //calculate the value for frequency setting
112  frequency_prescaler = F_CPU / (min_freq * 1000);
113 
114  //set AD Prescaler Bits depending on Prescaler value and minimum frequency and start the first conversion
115  if (frequency_prescaler > 64) {
116  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //prescaler=128
117  } else if (frequency_prescaler > 32) {
118  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); //prescaler=64
119  } else if (frequency_prescaler > 16) {
120  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); //prescaler=32
121  } else if (frequency_prescaler > 8) {
122  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (1 << ADPS2) | (0 << ADPS1) | (0 << ADPS0); //prescaler=16
123  } else if (frequency_prescaler > 4) {
124  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //prescaler=8
125  } else if (frequency_prescaler > 2) {
126  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (0 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); //prescaler=4
127  } else {
128  ADCSRA = 0x00 | (1 << ADEN) | (1 << ADSC) | (0 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); //prescaler=2
129  }
130 
131  //wait until the first conversion has been completed
132  while (((ADCSRA >> ADSC) & 1) != 0) {
133  ; //do nothing
134  }
135 
136  //now the first conversion has been completed --> read the result
137  tmp_voltage = ADCL;
138  tmp_voltage = ad_voltage | (ADCH << 8);
139 
140  ad_voltage = 0;
141 
142  //now start <number_cycles> conversions and calculate the mean
143  for (i = 0; i < number_cycles; i++) {
144 
145  ADCSRA = ADCSRA | (1 << ADSC); //start conversion
146 
147  //wait until the first conversion has been completed
148  while (((ADCSRA >> ADSC) & 1) != 0) {
149  ; //do nothing
150  }
151 
152  //now the first conversion has been completed --> read the result
153  tmp_voltage = ADCL;
154  tmp_voltage = tmp_voltage | (ADCH << 8);
155 
156  ad_voltage = ad_voltage + tmp_voltage;
157 
158  }
159 
160 
161  //disable the ADC
162  ADCSRA = 0x00;
163 
164 
165  //calculate the result (mean value)
166  ad_voltage = ad_voltage * vref_val;
167  ad_voltage = ad_voltage / number_cycles;
168  ad_voltage = ad_voltage >> 10; //ad_voltage/1024
169 
170  return ad_voltage;
171 
172 }
173 
174 
175 
176 /****************************************************************************/
191 /****************************************************************************/
192 
193 
194 void adc_pin_init(void) {
195 
196  //disable digital input buffer for ADC pin
197  DIDR0 = DIDR0 | (1 << ADC_PIN);
198 
199  //set ADC Port Pin to input pin (no pull-up resistor)
200  ADC_DDR = ADC_DDR & ~(1 << ADC_PIN);
201  ADC_PORT = ADC_PORT & ~(1 << ADC_PIN);
202 
203 
204 }
205 
206 /****************************************************************************/
220 /****************************************************************************/
221 
222 void adc_pin_deinit(void) {
223 
224 
225  //set ADC Port Pin to input pin and enable the internal pull-up resistor
226  ADC_DDR = ADC_DDR & ~(1 << ADC_PIN);
227  ADC_PORT = ADC_PORT | (1 << ADC_PIN);
228 
229 
230 }
231 
232 
233 
234 
235 
#define ADC_UNKNOWN_ERROR
Definition: adc.h:43
#define MAX_NUMBER_ADC_CYCLES
Definition: adc.h:48
#define ADC_NUMBERCYCLES_ERROR
Definition: adc.h:46
#define ADC_PIN_ERROR
Definition: adc.h:44
uint32_t single_adc_read(uint8_t vref_reg, uint16_t vref_val, uint16_t number_cycles, uint16_t min_freq)
Definition: adc.c:74
void adc_pin_init(void)
Definition: adc.c:194
#define MAX_ADC_FREQUENCY
Definition: adc.h:50
#define ADC_PIN
void adc_pin_deinit(void)
Definition: adc.c:222
#define ADC_DDR
#define ADC_FREQUENCY_ERROR
Definition: adc.h:45
#define ADC_PORT
#define F_CPU
Controller Clock Cycle [Hz].
Definition: main.c:69
#define MIN_ADC_FREQUENCY
Definition: adc.h:49