xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
adc.c
Go to the documentation of this file.
1 /*
2  * adc.c
3  *
4  * Copyright (c) 2013, Thomas Buck <xythobuz@me.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <avr/io.h>
31 #include <stdint.h>
32 
33 #include <adc.h>
34 
44 void adcInit(ADCRef ref) {
45  // Enable ADC Module, start one conversion, wait for finish
46  PRR0 &= ~(1 << PRADC); // Disable ADC Power Reduction (Enable it...)
47  switch(ref) {
48  case AVCC:
49  ADMUX = (1 << REFS0);
50  break;
51 
52  case AINT1:
53  ADMUX = (1 << REFS1);
54  break;
55 
56  case AINT2:
57  ADMUX = (1 << REFS1) | (1 << REFS0);
58  break;
59 
60  case AREF:
61  ADMUX &= ~((1 << REFS0) | (1 << REFS1));
62  break;
63  }
64 
65  ADCSRA = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Prescaler 128
66  ADCSRB = 0;
67  ADCSRA |= (1 << ADEN) | (1 << ADSC); // Start ADC, single conversion
68 }
69 
70 void adcStart(uint8_t channel) {
71  // Start a measurement on channel
72  if (channel > 15) {
73  channel = 0;
74  }
75  if (channel > 7) {
76  channel -= 8;
77  ADCSRB |= (1 << MUX5);
78  } else {
79  ADCSRB &= ~(1 << MUX5);
80  }
81  ADMUX &= ~0x1F; // Delete MUX0:4
82  ADMUX |= channel;
83  ADCSRA |= (1 << ADSC);
84 }
85 
86 uint8_t adcReady() {
87  // Is the measurement finished
88  if (ADCSRA & (1 << ADSC)) {
89  // ADSC bit is set
90  return 0;
91  } else {
92  return 1;
93  }
94 }
95 
96 uint16_t adcGet(uint8_t next) {
97  // Return measurements result
98  // Start next conversion
99  uint16_t temp = 0;
100  while (!adcReady());
101  temp = ADC;
102  if (next)
103  ADCSRA |= (1 << ADSC); // Start next conversion
104  return temp;
105 }
106 
107 void adcClose() {
108  // deactivate adc
109  ADCSRA &= ~(1 << ADSC);
110  PRR0 |= (1 << PRADC);
111  ADCSRA &= ~(1 << ADEN);
112 }