Embedded C Firmware Library : Arduino / Atmega328p  1
Register Level Embedded C Hardware Abstraction Library for AVR ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI
avr_adc.h
Go to the documentation of this file.
1 
44 #ifndef AVR_ADC_H_
45 #define AVR_ADC_H_
46 
47 #include <avr/io.h>
48 
49 #pragma message ( "avr_adc.h included" )
50 
55 #define ADC0 0x00
56 #define ADC1 0x01
57 #define ADC2 0x02
58 #define ADC3 0x03
59 #define ADC4 0x04
60 #define ADC5 0x05
61 #define ADC6 0x06
62 #define ADC7 0x07
63 #define ADC8 0x08
64 #define VBG 0x0E
65 #define GND 0x0F
66 #define ADC_PIN_MASK 0x0F
73 #define ADC_VOLTAGE_REFERENCE_AREF (0<<REFS0)
74 #define ADC_VOLTAGE_REFERENCE_AVCC (1<<REFS0)
75 #define ADC_VOLTAGE_REFERENCE_IREF (1<<REFS1) | (1<<REFS0)
76 #define ADC_VOLTAGE_REFERENCE_MASK (1<<REFS1) | (1<<REFS0)
83 #define ADC_PRESCALER_2 (1<<ADPS0)
84 #define ADC_PRESCALER_4 (1<<ADPS1)
85 #define ADC_PRESCALER_8 (1<<ADPS1) | (1<<ADPS0)
86 #define ADC_PRESCALER_16 (1<<ADPS2)
87 #define ADC_PRESCALER_32 (1<<ADPS2) | (1<<ADPS0)
88 #define ADC_PRESCALER_64 (1<<ADPS2) | (1<<ADPS1)
89 #define ADC_PRESCALER_128 (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0)
90 #define ADC_PRESCALER_MASK (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0)
97 #define ADC_TEMP_TOS 0x01
98 #define ADC_TEMP_K 0x01
99 
105 typedef struct ADC_ConfigData
106 {
107  uint8_t VoltageReference;
108  uint8_t Prescaler;
116 inline void ADC_DisableInterrupt();
117 inline void ADC_EnableInterrupt();
124 inline void ADC_Init(ADC_ConfigData Data);
125 inline void ADC_DeInit();
132 uint16_t ADC_ReadPin(uint8_t Pin);
133 uint16_t ADC_ReadTemperature();
141 inline void ADC_DisableInterrupt()
142 {
143 
144 }
145 
151 inline void ADC_EnableInterrupt()
152 {
153 
154 }
155 
162 inline void ADC_Init(ADC_ConfigData Data)
163 {
164  ADMUX = Data.VoltageReference | VBG; // Set Voltage Reference and Connect the ADC H/W to GND
165  ADCSRA = (1<<ADEN) | Data.Prescaler;
166 }
167 
173 inline void ADC_DeInit()
174 {
175  ADMUX = 0x00;
176  ADCSRA = 0x00;
178 }
179 
186 uint16_t ADC_ReadPin(uint8_t Pin)
187 {
188  uint8_t Low, High;
189  ADMUX = (ADMUX & ADC_VOLTAGE_REFERENCE_MASK) | Pin;
190  ADCSRA |= (1<<ADSC);
191  while (ADCSRA & (1<<ADSC));
192  Low = ADCL;
193  High = ADCH;
194  return (High<<8) | Low;
195 }
196 
203 {
204  uint8_t Low, High;
206  ADCSRA |= (1<<ADSC);
207  while (ADCSRA & (1<<ADSC));
208  Low = ADCL;
209  High = ADCH;
210  return (((High<<8) | Low) - ADC_TEMP_TOS) / ADC_TEMP_K;
211 }
212 
213 #endif /* AVR_ADC_H_ */
ADC_VOLTAGE_REFERENCE_MASK
#define ADC_VOLTAGE_REFERENCE_MASK
Definition: avr_adc.h:76
ADC_DeInit
void ADC_DeInit()
Public Function to De-Initialize ADC.
Definition: avr_adc.h:173
ADC_Init
void ADC_Init(ADC_ConfigData Data)
Public Function to Configure and Initialize ADC.
Definition: avr_adc.h:162
ADC_ConfigData
Definition: avr_adc.h:106
ADC_EnableInterrupt
void ADC_EnableInterrupt()
Public Function to Enable ADC Interrupt.
Definition: avr_adc.h:151
ADC_ReadPin
uint16_t ADC_ReadPin(uint8_t Pin)
Public Function to Read Analog Voltage in Digital Value from a Pin / Analog Input Channel.
Definition: avr_adc.h:186
VBG
#define VBG
Definition: avr_adc.h:64
ADC8
#define ADC8
Definition: avr_adc.h:63
ADC_ReadTemperature
uint16_t ADC_ReadTemperature()
Public Function to Read the Chip's Temperature in Degree C.
Definition: avr_adc.h:202
ADC_DisableInterrupt
void ADC_DisableInterrupt()
Public Function to Disable ADC Interrupt.
Definition: avr_adc.h:141
ADC_VOLTAGE_REFERENCE_IREF
#define ADC_VOLTAGE_REFERENCE_IREF
Definition: avr_adc.h:75