Rev 804 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 804 | Rev 806 | ||
---|---|---|---|
Line 79... | Line 79... | ||
79 | 79 | ||
80 | // happens every 0.209712 s. |
80 | // happens every 0.209712 s. |
81 | // check for at least one input capture event per timer overflow (timeout) |
81 | // check for at least one input capture event per timer overflow (timeout) |
82 | ISR(TIMER1_OVF_vect) |
82 | ISR(TIMER1_OVF_vect) |
83 | { |
- | |
84 | int16_t signal = 0; |
83 | { |
85 | static uint16_t lastICR1 = 0; |
84 | static uint16_t lastICR1 = 0; |
86 | // if ICR1 has not changed |
85 | // if ICR1 has not changed |
87 | // then no new input capture event has occured since last timer overflow |
86 | // then no new input capture event has occured since last timer overflow |
88 | if (lastICR1 == ICR1) RC_Quality = 0; |
87 | if (lastICR1 == ICR1) RC_Quality = 0; |
Line 113... | Line 112... | ||
113 | */ |
112 | */ |
114 | ISR(TIMER1_CAPT_vect) // typical rate of 1 ms to 2 ms |
113 | ISR(TIMER1_CAPT_vect) // typical rate of 1 ms to 2 ms |
115 | { |
114 | { |
116 | int16_t signal = 0, tmp; |
115 | int16_t signal = 0, tmp; |
117 | static int16_t index; |
116 | static int16_t index; |
118 | static int16_t Sum_RC_Quality = 0; |
- | |
119 | static uint16_t oldICR1 = 0; |
117 | static uint16_t oldICR1 = 0; |
120 | int16_t Noise; |
- | |
Line 121... | Line 118... | ||
121 | 118 | ||
122 | // 16bit Input Capture Register ICR1 contains the timer value TCNT1 |
119 | // 16bit Input Capture Register ICR1 contains the timer value TCNT1 |
Line 123... | Line 120... | ||
123 | // at the time the edge was detected |
120 | // at the time the edge was detected |
Line 129... | Line 126... | ||
129 | oldICR1 = ICR1; |
126 | oldICR1 = ICR1; |
Line 130... | Line 127... | ||
130 | 127 | ||
131 | // lost frame? |
128 | // lost frame? |
132 | if(signal > 8000) |
129 | if(signal > 8000) |
133 | { |
130 | { |
134 | Sum_RC_Quality -= Sum_RC_Quality/2; |
131 | RC_Quality /= 2; |
135 | } |
132 | } |
136 | else |
133 | else |
137 | //sync gap? (3.52 ms < signal < 25.6 ms) |
134 | //sync gap? (3.52 ms < signal < 25.6 ms) |
138 | if((signal > 1100) && (signal < 8000)) |
135 | if((signal > 1100) && (signal < 8000)) |
Line 152... | Line 149... | ||
152 | if((signal > 250) && (signal < 687)) |
149 | if((signal > 250) && (signal < 687)) |
153 | { |
150 | { |
154 | // shift signal to zero symmetric range -154 to 159 |
151 | // shift signal to zero symmetric range -154 to 159 |
155 | signal -= 466; // offset of 1.4912 ms ??? (469 * 3.2µs = 1.5008 ms) |
152 | signal -= 466; // offset of 1.4912 ms ??? (469 * 3.2µs = 1.5008 ms) |
156 | // check for stable signal |
153 | // check for stable signal |
157 | Noise = abs(signal - PPM_in[index]); |
154 | if(abs(signal-PPM_in[index]) < 6) |
158 | if((Noise/16) > (200-RC_Quality)) // spike detector |
155 | { |
159 | { |
- | |
160 | Sum_RC_Quality -= 3*RC_Quality; |
- | |
161 | Sum_RC_Quality += 3*(200 - Noise); |
- | |
162 | } |
- | |
163 | else |
- | |
164 | { |
- | |
165 | Sum_RC_Quality -= RC_Quality; |
156 | if(RC_Quality < 200) RC_Quality +=10; |
166 | Sum_RC_Quality += 200 - Noise; |
- | |
167 | } |
157 | } |
168 | // calculate exponential history for signal |
158 | // calculate exponential history for signal |
169 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
159 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
170 | if(tmp > signal+1) tmp--; else |
160 | if(tmp > signal+1) tmp--; else |
171 | if(tmp < signal-1) tmp++; |
161 | if(tmp < signal-1) tmp++; |
172 | // calculate signal difference on good signal level |
162 | // calculate signal difference on good signal level |
173 | if(RC_Quality >= 170) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction |
163 | if(RC_Quality >= 195) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction |
174 | else PPM_diff[index] = 0; |
164 | else PPM_diff[index] = 0; |
175 | PPM_in[index] = tmp; // update channel value |
165 | PPM_in[index] = tmp; // update channel value |
176 | } |
166 | } |
177 | else |
167 | else |
178 | { // invalid PPM time |
168 | { // invalid PPM time |
179 | Sum_RC_Quality /= 8; |
169 | RC_Quality /= 2; |
180 | } |
170 | } |
181 | index++; // next channel |
171 | index++; // next channel |
182 | // demux sum signal for channels 5 to 7 to J3, J4, J5 |
172 | // demux sum signal for channels 5 to 7 to J3, J4, J5 |
183 | if(index == 5) PORTD |= (1<<PORTD5); else PORTD &= ~(1<<PORTD5); |
173 | if(index == 5) PORTD |= (1<<PORTD5); else PORTD &= ~(1<<PORTD5); |
184 | if(index == 6) PORTD |= (1<<PORTD4); else PORTD &= ~(1<<PORTD4); |
174 | if(index == 6) PORTD |= (1<<PORTD4); else PORTD &= ~(1<<PORTD4); |
Line 186... | Line 176... | ||
186 | { |
176 | { |
187 | if(index == 7) PORTD |= (1<<PORTD3); else PORTD &= ~(1<<PORTD3); |
177 | if(index == 7) PORTD |= (1<<PORTD3); else PORTD &= ~(1<<PORTD3); |
188 | } |
178 | } |
189 | } |
179 | } |
190 | } |
180 | } |
191 | if(Sum_RC_Quality < 0) Sum_RC_Quality = 0; |
- | |
192 | RC_Quality = Sum_RC_Quality/128; |
181 | if(RC_Quality) RC_Quality--; |
193 | } |
182 | } |