avr_adc.h

 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;
109 } ADC_ConfigData;
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;
177  ADC_DisableInterrupt();
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 
202 uint16_t ADC_ReadTemperature()
203 {
204  uint8_t Low, High;
205  ADMUX = ADC_VOLTAGE_REFERENCE_IREF | ADC8;
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_ */

إرسال تعليق

Post a Comment (0)

أحدث أقدم