Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2195 | - | 1 | /***************************************************************************************************************************** |
2 | * File: analog.c |
||
3 | * |
||
4 | * Purpose: collecting ADC analog inputs |
||
5 | * |
||
6 | * Functions: void ADC_Init(void) |
||
7 | * ISR(ADC_vect) |
||
8 | * |
||
9 | *****************************************************************************************************************************/ |
||
10 | #include "analog.h" |
||
11 | |||
12 | |||
13 | #include <stdlib.h> |
||
14 | #include <avr/interrupt.h> |
||
15 | |||
16 | volatile int AdWertNick=0, AdWertAccNick, Aktuell_ax; |
||
17 | int NeutralAccX = 511; |
||
18 | volatile unsigned char AdReady = 1; |
||
19 | |||
20 | |||
21 | |||
22 | // ************************************************************************************************************************** |
||
23 | // Purpose: set up ADC |
||
24 | // |
||
25 | // INPUT: None |
||
26 | // OUTPUT: None |
||
27 | // RETURN: None |
||
28 | // -------------------------------------------------------------------------------------------------------------------------- |
||
29 | void init_ADC(void) |
||
30 | { |
||
31 | static unsigned char sreg; // temporary variable to store SREG |
||
32 | |||
33 | sreg = SREG; // save backup status register , to be reseted later |
||
34 | cli(); // switch off Global Interrupt |
||
35 | |||
36 | // ADMUX = ADC Multiplexer Selection Register –> REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0 |
||
37 | // ----------------------------------------------------------------------------------------- |
||
38 | // REFS1:0 = Reference Selection Bits |
||
39 | // ADLAR = ADC Left Adjust Result |
||
40 | // MUX4:0 = Analog Channel and Gain Selection Bits |
||
41 | // |
||
42 | ADMUX &= ~((1 << REFS1)|(1<<REFS0)); // AREF is the reference and Internal Vref is turned off |
||
43 | ADMUX &= ~(1 << ADLAR); // the result is right side edge |
||
44 | ADMUX &= ~((1<<MUX4)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0)); // the analog input is connected to ADC0 = PORTA0 = port S21 |
||
45 | |||
46 | // SFIOR = Special Function IO Register –> ADTS2 ADTS1 ADTS0 – ACME PUD PSR2 PSR10 |
||
47 | //--------------------------------------------------------------------------------- |
||
48 | SFIOR &= ~((1<<ADTS2)|(1<<ADTS2)|(1<<ADTS0)); // ADTS2:0 = ADC Auto Trigger Source = 000 = Free Running mode |
||
49 | |||
50 | // ADCSRA = Analog Digital Conversion Status Register A -> ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 |
||
51 | // ---------------------------------------------------------------------------------------------------- |
||
52 | // ADEN = AD Wandler enable |
||
53 | // ADSC = start conversion |
||
54 | // ADATE = auto trigger ein |
||
55 | // ADIF = interrupt flag -> This bit is set when an ADC conversion completes and the Data Registers are updated |
||
56 | // ADIE = Analog to Dig Conversion Interrupt Enable |
||
57 | // ADPS = Prescaler = 111 = Division Factor 128 |
||
58 | // |
||
59 | ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIF)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
||
60 | ADCSRA &= ~((1<<ADATE)); |
||
61 | |||
62 | SREG = sreg; // write back to old state, |
||
63 | sei(); // switch on Global Interrupt |
||
64 | } |
||
65 | // *** EOF: init_ADC() ************************************************************************************************************** |
||
66 | |||
67 | |||
68 | |||
69 | |||
70 | // ********************************************************************************************************************************* |
||
71 | // read analog inputs from ADC with the following sequence: |
||
72 | // ------------------------------------------------------------------------------------------------ |
||
73 | // cases: |
||
74 | // 0 nick-gyro |
||
75 | // 1 x acc nick |
||
76 | // 2 nick-gyro |
||
77 | // 3 nick-gyro |
||
78 | // 4 x acc nick |
||
79 | // 5 nick-gyro |
||
80 | // ------------------------------------------------------------------------------------------------ |
||
81 | // Chanels: |
||
82 | // PA0 Gyro Gier |
||
83 | // PA1 Gyro Roll |
||
84 | // PA2 Gyro Nick |
||
85 | // PA3 Air Pressure |
||
86 | // PA4 Voltage |
||
87 | // PA5 ACC H |
||
88 | // PA6 ACC Roll |
||
89 | // PA7 ACC Nick |
||
90 | // ------------------------------------------------------------------------------------------------ |
||
91 | ISR(ADC_vect) |
||
92 | { |
||
93 | static unsigned char kanal=AD_NICK, state = 0; |
||
94 | static signed int nick1, accx; |
||
95 | |||
96 | switch(state++) |
||
97 | { |
||
98 | case 0: |
||
99 | nick1 = ADC; // 1st measure of Nick Gyro Sensor |
||
100 | kanal = AD_ACC_X; |
||
101 | break; |
||
102 | |||
103 | case 1: |
||
104 | Aktuell_ax = ADC - NeutralAccX; // 1st measure of ACC Nick Sensor |
||
105 | accx = Aktuell_ax; |
||
106 | kanal = AD_NICK; |
||
107 | break; |
||
108 | |||
109 | case 2: |
||
110 | nick1 += ADC; // 2nd measure of Nick Gyro Sensor |
||
111 | kanal = AD_NICK; |
||
112 | break; |
||
113 | |||
114 | case 3: |
||
115 | nick1 += ADC; // 3rd measure of Nick Gyro Sensor |
||
116 | kanal = AD_ACC_X; |
||
117 | break; |
||
118 | |||
119 | case 4: |
||
120 | Aktuell_ax = ADC - NeutralAccX; |
||
121 | AdWertAccNick = (Aktuell_ax + accx); // 2nd measure of ACC Nick Sensor 0...512 |
||
122 | kanal = AD_NICK; |
||
123 | break; |
||
124 | |||
125 | case 5: |
||
126 | nick1 += ADC; // 4th measure of Nick Gyro |
||
127 | AdWertNick = nick1 / 8; // AdWertNick = 0...511 |
||
128 | kanal = AD_NICK; |
||
129 | break; |
||
130 | |||
131 | case 6: |
||
132 | AdReady = 1; // all cases have been passed -> analog conversion ready |
||
133 | state = 0; |
||
134 | kanal = AD_NICK; |
||
135 | break; |
||
136 | |||
137 | default: |
||
138 | kanal = 0; state = 0; kanal = AD_NICK; |
||
139 | break; |
||
140 | } |
||
141 | ADMUX = kanal; // ADMUX = ADC Multiplexer Selection Register |
||
142 | |||
143 | // ADCSRA = Analog Digital Conversion Status Register A -> ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 |
||
144 | // ADSC = start conversion |
||
145 | if(state != 0) ADCSRA |= (1<<ADSC); |
||
146 | |||
147 | } |
||
148 | // *** EOF : ISR(ADC_vect) ********************************************************************************************************** |