-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadc.c
More file actions
76 lines (54 loc) · 1.51 KB
/
Copy pathadc.c
File metadata and controls
76 lines (54 loc) · 1.51 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* adc.c
*
* Created: 18.02.2017 10:41:35
* Author: olesot
*/
//Written for UM
#include "adc.h"
#include <avr/io.h>
void adc_init(void){
/* Voltage ref AVcc with external capacitor on AREF pin */
ADMUX |= (1<<REFS0);
/* Select prescaler to 64 --> conversion f= 125kHz */
ADCSRA |= (1<<ADPS2)|(1<<ADPS2);
/* Enable the ADC */
ADCSRA |= (1<<ADEN);
}
uint16_t adc_read(adc_channel_t channel){
//Setting channel and type of reading, see enum in adc.h
ADMUX &= 0b11100000;
ADMUX |= (int8_t)channel;
/* Start the conversion */
ADCSRA |= (1<<ADSC);
/* Wait for the conversion to complete */
while(ADCSRA & (1<<ADSC));
return ADC;
}
void adc_Free_running_init(void){
/* Voltage ref AVcc with external capacitor on AREF pin */
ADMUX |= (1<<REFS0);
/* Select prescaler to 64 --> conversion f= 125kHz */
ADCSRA |= (1<<ADPS2);
/* Auto trigger of ADC */
ADCSRA |= (1<<ADATE);
/* Enable Interrupt of ADC */
ADCSRA |= (1<<ADIE);
/* Free running mode */
ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); // (initially at 0 so not useful)
/* Enable the ADC */
ADCSRA |= (1<<ADEN);
/* Start the conversion */
ADCSRA |= (1<<ADSC);
}
uint16_t adc_Free_running_read(adc_channel_t channel){
//Setting channel and type of reading, see enum in adc.h
ADMUX &= 0b11100000;
ADMUX |= (int8_t)channel;
return (ADCL+(ADCH<<8));
}
void Set_ADC_Channel(adc_channel_t channel)
{
channel &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|channel; // clears the bottom 3 bits before ORing
}