xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Enumerations | Functions
ADC Driver

Analog-to-Digital Converter Library. More...

Files

file  adc.h
 Analog-to-Digital Converter API Header.
 
file  adc.c
 Analog-to-Digital Converter API Implementation.
 

Enumerations

enum  ADCRef { AREF, AVCC, AINT1, AINT2 }
 ADC Reference Voltage options. More...
 

Functions

void adcInit (ADCRef ref)
 Initialize the ADC Hardware. More...
 
void adcStart (uint8_t channel)
 Start a conversion on a given channel. More...
 
uint8_t adcReady (void)
 Check if a result is ready. More...
 
uint16_t adcGet (uint8_t next)
 Get the conversion results. More...
 
void adcClose (void)
 Disable the ADC to save energy. More...
 

Detailed Description

Analog-to-Digital Converter Library.

With 10bit Output and selectable Reference Voltage.

Enumeration Type Documentation

enum ADCRef

ADC Reference Voltage options.

Enumerator
AREF 

External Reference Voltage.

AVCC 

Supply Voltage.

AINT1 

Internal Reference 1 (1.1V)

AINT2 

Internal Reference 2 (2.56V)

Definition at line 45 of file adc.h.

45  {
46  AREF,
47  AVCC,
48  AINT1,
49  AINT2
50 } ADCRef;

Function Documentation

void adcClose ( void  )

Disable the ADC to save energy.

Definition at line 107 of file adc.c.

107  {
108  // deactivate adc
109  ADCSRA &= ~(1 << ADSC);
110  PRR0 |= (1 << PRADC);
111  ADCSRA &= ~(1 << ADEN);
112 }
uint16_t adcGet ( uint8_t  next)

Get the conversion results.

Parameters
nextStart next conversion if != 0
Returns
10bit ADC value

Definition at line 96 of file adc.c.

References adcReady().

Referenced by getVoltage().

96  {
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 }
void adcInit ( ADCRef  ref)

Initialize the ADC Hardware.

Parameters
refReference Voltage.

Definition at line 44 of file adc.c.

References AINT1, AINT2, AREF, and AVCC.

Referenced by xyInit().

44  {
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 }
uint8_t adcReady ( void  )

Check if a result is ready.

Returns
1 if conversion is done.

Definition at line 86 of file adc.c.

Referenced by adcGet(), and getVoltage().

86  {
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 }
void adcStart ( uint8_t  channel)

Start a conversion on a given channel.

Parameters
channelChannel (0 - 15)

Definition at line 70 of file adc.c.

Referenced by getVoltage().

70  {
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 }