Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/**************************************************************************************************************************************
* File: rc.c
*
* Purpose: Decoding of the radio control sum signal
*
* Functions: void rc_sum_init(void)
* ISR(TIMER1_CAPT_vect)
*
*************************************************************************************************************************************/
#include "rc.h"
#include "main.h"
volatile int PPM_in
[11]; // containing the stick signal of the according radio control chanel
volatile int PPM_diff
[11]; // the differential stick signal of the appropriate radio control chanel
volatile char Channels
; // containing the number of channels
volatile char tmpChannels
= 0; //
volatile unsigned char NewPpmData
= 1; //
//----------------------------------------------------------------------------------------------------------------------------------
// The input capture function of Timer1 is used to decode the PPM sum signal
//----------------------------------------------------------------------------------------------------------------------------------
void rc_sum_init
(void)
{
//------------------------------------------------------------------------------------------------
// TCCR1B = Timer/Counter1 Control Register B -> ICNC1 ICES1 WGM13 WGM12 CS12 CS11 CS10
//
// ICES1: Input Capture Edge Select = activated
// ICNC1: Input Capture Noise Canceler = activated
// CSn2:0 Clock Select = 1:64 from prescaler
//
TCCR1B
= (1<<ICES1
)|(1<<ICNC1
)|(1<<CS11
)|(1<<CS10
);
//------------------------------------------------------------------------------------------------
// TIMSK1 = Timer/Counter1 Interrupt Mask Register -> ICIE1 OCIE1B OCIE1A TOIE1
// ICIE1: Timer/Counter1, Input Capture Interrupt Enable
//
TIMSK1
|= (1<<ICIE1
);
AdNeutralGier
= 0;
AdNeutralRoll
= 0;
AdNeutralNick
= 0;
return;
}
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------
// This function works with Timer1 to decode the radio control
//----------------------------------------------------------------------------------------------------------------------------------
ISR
(TIMER1_CAPT_vect
)
{
static unsigned int AltICR
=0;
signed int signal
= 0;
signed int tmp
;
static int index
;
signal
= (unsigned int) ICR1
- AltICR
; // ICR1H and ICR1L Input Capture Register 1
AltICR
= ICR1
;
if((signal
> 1100) && (signal
< 8000)) // Syncronisation break? (3.52 ms < signal < 25.6 ms)
{
Channels
= index
;
if(index
>= 4) NewPpmData
= 0; // zero is equal to new Data
index
= 1;
}
else
{
if(index
< 10)
{
if((signal
> 250) && (signal
< 687))
{
signal
-= 466;
if(abs(signal
- PPM_in
[index
]) < 6) // good signal
{
if(SenderOkay
< 200) SenderOkay
+= 10;
else SenderOkay
= 200;
}
tmp
= (3 * (PPM_in
[index
]) + signal
) / 4;
if(tmp
> signal
+1) tmp
--;
else if(tmp
< signal
-1) tmp
++;
if(SenderOkay
>= 195) PPM_diff
[index
] = ((tmp
- PPM_in
[index
]) / 3) * 3;
else PPM_diff
[index
] = 0;
PPM_in
[index
] = tmp
;
}
index
++;
if(index
== 5) J3High
; else J3Low
; // Servosignal an J3 anlegen
if(index
== 6) J4High
; else J4Low
; // Servosignal an J4 anlegen
if(index
== 7) J5High
; else J5Low
; // Servosignal an J5 anlegen
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------