-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathADCusingInterrupts.c
46 lines (32 loc) · 925 Bytes
/
ADCusingInterrupts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* ADCusingInterrupts.c
*
* Created: 26-06-2015 22:44:54
* Author: Sandeep
*/
#include <avr/io.h>
#include<avr/interrupt.h>
int main(void)
{
//Configure ADC
ADCSRA |= 1<<ADPS2; //enable prescaler-determined by clock 1000000/p= [50000,200000] p=[20,5]
ADMUX |= 1<<ADLAR; //left adjust the result 8bit 10bit
ADMUX |= 1<<REFS0; //set reference voltage
ADCSRA |= 1<<ADIE; //enable interrupts function
ADCSRA |= 1<<ADEN; //turn on adc
sei(); //Enable global interrupts
ADCSRA |= 1<<ADSC; //start first conversion
while(1)
{
//Code
}
}
/////////////Interrupt routine and display results
ISR(ADC_vect)
{
char adcResult[4];
uint8_t LowBit = ADCL;
uint16_t TenBitResult = ADCH<<2 | LowBit>>6; //ADLAR=1 set above
itoa(TenBitResult , adcResult ,10); //convert the adc conversion result
ADCSRA |= 1<<ADSC; //start the next conversion
}