1 | #include <stdint.h>
|
2 |
|
3 | void ADC_init(void){
|
4 | /* Voltage Reference for the ADC: */
|
5 | ADMUX |= (0<<REFS1) | (0<<REFS0); //AREF, Internal Vref turned off
|
6 | //ADMUX |= (0<<REFS1) | (1<<REFS0); //AVcc with external capacitor at AREF pin
|
7 | //ADMUX |= (1<<REFS1) | (1<<REFS0); //Internal 1.1V Voltage Reference with external capacitor at AREF pin
|
8 |
|
9 |
|
10 | /* ADC Left Adjust Result: */
|
11 | ADMUX |= (1<<ADLAR); //Write one to left adjust the result. Otherwise, the result is right adjusted.
|
12 |
|
13 |
|
14 | /* ADC Control and Status Register A: */
|
15 | /* ADC Auto Trigger Enable: */
|
16 | ADCSRA |= (1<<ADATE); //When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a conversion on a positive edge of the selected trigger signal. The trigger source is selected by setting the ADC Trigger Select bits, ADTS in ADCSRB.
|
17 |
|
18 | /* ADC Interrupt Flag */
|
19 |
|
20 | /* ADC Interrupt Enable */
|
21 | ADCSRA |= (1<<ADIE); //When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete Interrupt is activated.
|
22 |
|
23 | /* ADC Prescaler Select Bits */
|
24 | #ifndef ADC_PRESCALER
|
25 | #define ADC_PRESCALER 128
|
26 | #endif
|
27 | #if ADC_PRESCALER == 2
|
28 | ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); //Division Factor: 2
|
29 | #elif ADC_PRESCALER == 2
|
30 | ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (1<<ADPS0); //Division Factor: 2
|
31 | #elif ADC_PRESCALER == 4
|
32 | ADCSRA |= (0<<ADPS2) | (1<<ADPS1) | (0<<ADPS0); //Division Factor: 4
|
33 | #elif ADC_PRESCALER == 8
|
34 | ADCSRA |= (0<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //Division Factor: 8
|
35 | #elif ADC_PRESCALER == 16
|
36 | ADCSRA |= (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); //Division Factor: 16
|
37 | #elif ADC_PRESCALER == 32
|
38 | ADCSRA |= (1<<ADPS2) | (0<<ADPS1) | (1<<ADPS0); //Division Factor: 32
|
39 | #elif ADC_PRESCALER == 64
|
40 | ADCSRA |= (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0); //Division Factor: 64
|
41 | #else
|
42 | ADCSRA |= (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //Division Factor: 128
|
43 | #endif
|
44 |
|
45 | /* ADC Control and Status Register B: */
|
46 | /* ADC Auto Trigger Source */
|
47 | ADCSRB |= (0<<ADTS2) | (0<<ADTS2) | (0<<ADTS2); //Trigger Source: Free Running mode
|
48 | //ADCSRB |= (0<<ADTS2) | (0<<ADTS2) | (1<<ADTS2); //Trigger Source: Analog Comparator
|
49 | //ADCSRB |= (0<<ADTS2) | (1<<ADTS2) | (0<<ADTS2); //Trigger Source: External Interrupt Request 0
|
50 | //ADCSRB |= (0<<ADTS2) | (1<<ADTS2) | (1<<ADTS2); //Trigger Source: Timer/Counter0 Compare Match A
|
51 | //ADCSRB |= (1<<ADTS2) | (0<<ADTS2) | (0<<ADTS2); //Trigger Source: Timer/Counter0 Overflow
|
52 | //ADCSRB |= (1<<ADTS2) | (0<<ADTS2) | (1<<ADTS2); //Trigger Source: Timer/Counter1 Compare Match B
|
53 | //ADCSRB |= (1<<ADTS2) | (1<<ADTS2) | (0<<ADTS2); //Trigger Source: Timer/Counter1 Overflow
|
54 | //ADCSRB |= (1<<ADTS2) | (1<<ADTS2) | (1<<ADTS2); //Trigger Source: Timer/Counter1 Capture Event
|
55 |
|
56 | /* Digital Input Disable Register 0: */
|
57 |
|
58 | }
|
59 |
|
60 | void ADC_SetChannel(uint8_t channel){
|
61 | if(0<= channel && channel <= 7){
|
62 | /* ADC Multiplexer Selection Register */
|
63 | //ADMUX |= (0<<MUX3) | (1<<MUX2) | (1<<MUX1) | (1<<MUX0);
|
64 | ADMUX |= channel;
|
65 | }
|
66 | }
|
67 |
|
68 | void ADC_Enable(void){
|
69 | /* ADC Control and Status Register A */
|
70 | ADCSRA |= (1<<ADEN); //Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the ADC off while a conversion is in progress, will terminate this conversion.
|
71 | }
|
72 |
|
73 | void ADC_Disable(void){
|
74 | /* ADC Control and Status Register A */
|
75 | ADCSRA &= ~(1<<ADEN); //Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the ADC off while a conversion is in progress, will terminate this conversion.
|
76 | }
|
77 |
|
78 | void ADC_StartConversion(void){
|
79 | /* ADC Control and Status Register A */
|
80 | ADCSRA |= (1<<ADSC); //In Single Conversion mode, write this bit to one to start each conversion. In Free Running mode, write this bit to one to start the first conversion.
|
81 | }
|
82 |
|
83 | uint8_t ADC_GetHData(void){
|
84 | /* ADC Data Register - Highbyte*/
|
85 | return ADCH;
|
86 | }
|