18,8 → 18,9 |
volatile int16_t PPM_in[15]; //PPM24 supports 12 channels per frame |
volatile int16_t PPM_diff[15]; |
volatile uint8_t NewPpmData = 1; |
volatile uint8_t SenderOkay = 0; |
volatile uint8_t RC_Quality = 0; |
|
|
/***************************************************************/ |
/* 16bit timer 1 is used to decode the PPM-Signal */ |
/***************************************************************/ |
70,6 → 71,8 |
TIMSK1 &= ~((1<<OCIE1B)|(1<<OCIE1A)|(1<<TOIE1)); |
TIMSK1 |= (1<<ICIE1); |
|
RC_Quality = 0; |
|
SREG = sreg; |
} |
|
100,11 → 103,14 |
static uint16_t oldICR1 = 0; |
int16_t signal = 0, tmp; |
static int16_t index; |
static int16_t Sum_RC_Quality = 0; |
|
// 16bit Input Capture Register ICR1 contains the timer value TCNT1 |
// at the time the edge was detected |
|
// calculate the time delay to the previous event time which is stored in oldICR1 |
// calculatiing the difference of the two uint16_t and converting the result to an int16_t |
// implicit handles a timer overflow 65535 -> 0 the right way. |
signal = (uint16_t) ICR1 - oldICR1; |
oldICR1 = ICR1; |
|
129,23 → 135,23 |
// shift signal to zero symmetric range -154 to 159 |
signal -= 466; // offset of 1.4912 ms ??? (469 * 3.2µs = 1.5008 ms) |
// check for stable signal |
// the deviation of the current signal level from the average must be less than 6 (aprox. 1%) |
if(abs(signal - PPM_in[index]) < 6) |
{ |
// a good signal condition increases SenderOkay by 10 |
// SignalOkay is decremented every 2 ms in main.c |
// this variable is a level for the average rate of a noiseless rc signal |
if(SenderOkay < 200) SenderOkay += 10; |
} |
Sum_RC_Quality -= Sum_RC_Quality/128; |
Sum_RC_Quality += 200 - abs(signal - PPM_in[index]); // max 200*128 = 25600 |
// calculate exponential history for signal |
tmp = (3 * (PPM_in[index]) + signal) / 4; |
if(tmp > signal+1) tmp--; else |
if(tmp < signal-1) tmp++; |
// calculate signal difference on good signal level |
if(SenderOkay >= 195) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction |
if(RC_Quality >= 170) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction |
else PPM_diff[index] = 0; |
PPM_in[index] = tmp; // update channel value |
} |
else |
{ // invalid PPM time |
Sum_RC_Quality -= Sum_RC_Quality/4; |
} |
if(Sum_RC_Quality < 0) Sum_RC_Quality = 0; |
RC_Quality = (uint8_t)(Sum_RC_Quality/128); |
index++; // next channel |
// demux sum signal for channels 5 to 7 to J3, J4, J5 |
if(index == 5) PORTD |= (1<<PORTD5); else PORTD &= ~(1<<PORTD5); |