Subversion Repositories Projects

Rev

Blame | Last modification | View Log | RSS feed

// ********************************************************
// ** Spektrum DX8 Expander for 12 Channels at large     **
// ********************************************************
// ** Target: Atmel ATtiny2313 with delivered standard   **
// ** Fuses (internal RC-Oscillator @ 1 MHz)             **
// ********************************************************
// ** It sends data from 4 additionally switches over    **
// ** the tripole "FLAP" channel which the FlightControl **
// ** can decode "on the fly" :)                         **
// ********************************************************
// ** (c) 2009-0xFFFF Stefan Pendsa                      **
// ** License: don't know - use it and have fun          **
// ********************************************************


#include <avr/io.h>
#include <util/delay.h>

#define TX_High PORTA |= 1 << PORTA0                                            // Set TX-Pin High
#define TX_Low  PORTA &= ~(1 << PORTA0)                                         // Set TX-Pin Low
#define SYNC_High       PORTA |= 1 << PORTA1                                    // Set Sync-Pin High
#define SYNC_Low        PORTA &= ~(1 << PORTA1)                                 // Set Sync-Pin Low



void Send(char sync, char data)
{
        unsigned char synced;

        if (sync == 0) SYNC_High; else SYNC_Low;                                // Send Sync
        if (data == 0) TX_High; else TX_Low;                                    // Send Data

        synced = 0;
        while (synced == 0)
        {
                while ( PIND & (1<<PD5) );                                                      // Wait for DX8-Sync goes low
                while ( !(PIND & (1<<PD5)) );                                           // Wait for DX8-Sync goes high
                _delay_us(4500);
                if ((PIND & (1<<PD5))) synced = 1;                                      // When its high (again) after 4,5ms, we are synced
        }


}



int main(void)
{
        char i,Parity;                                                                                  // Bit-Counter and Parity Bit
        DDRA = 0b011;                                                                                   // PORT A INPUT: RESET; OUTPUT: Sync+Data
        DDRB = 0b00000000;                                                                              // PORT B INPUT: Jumper for Bypass
        DDRD = 0b0000000;                                                                               // PORT D INPUT: Switches + DX7-Sync
        PORTA = 0b111;                                                                                  // Pull-Up for /RESET, High for Sync+Data
        PORTB = 0b01000000;                                                                             // Pull-Up for Jumper
        PORTD = 0b1011111;                                                                              // Pull-Up for Switches

        while(1)                                                                                                // Main-Loop (Sending data all the time)
        {
                if (!(PINB & (1<<PINB6)))                                                       // By-Pass Mode
                {
                        if (PIND & (1<<PIND0)) PORTA |= 1 << PORTA1;    // A1=D0
                        else PORTA &= ~(1 << PORTA1);

                        if (PIND & (1<<PIND1)) PORTA |= 1 << PORTA0;    // A0=D1
                        else PORTA &= ~(1 << PORTA0);
                }
                else                                                                                            // Normal Mode
                {
                        Parity = 0;                                                                             // Reset Parity Bit
                        Send(1,0);                                                                              // Send SYNC Pulse

                        i=0;
                        while (i<7)                                                                             // Encode PD0-PD4 + PD6 (F-MODE(2), ELEV(1), MIX, GEAR, ELEV(2))
                        {
                                if (PIND & (1<<i))                                                      // Check Pin #i
                                {
                                        Send(0,0);                                                              // When High, Send Low (Switched to "0")
                                        Parity = ~Parity;                                               // and invert Parity Bit
                                }
                                else Send(0,1);                                                         // When Low, Send High (Switched to "1")
                                i++;
                                if (i == 5) i = 6;                                                      // Skip PD5
                        }

                        for (i=0;i<2;i++)                                                               // Encode PB0+PB1 (AIL D/R (2))
                        {
                                if ((PINB & (1<<i)))                                            // Check Pin #i
                                {
                                        Send(0,0);                                                              // When High, Send Low (Switched to "0")
                                        Parity = ~Parity;                                               // and invert Parity Bit
                                }
                                else Send(0,1);                                                         // When Low, Send High (Switched to "1")
                        }

                        Send(0,Parity);                                                                 // Send Parity Bit
                }
        }
}