Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
944 | - | 1 | // ******************************************************** |
2 | // ** Spektrum DX8 Expander for 12 Channels at large ** |
||
3 | // ******************************************************** |
||
4 | // ** Target: Atmel ATtiny2313 with delivered standard ** |
||
5 | // ** Fuses (internal RC-Oscillator @ 1 MHz) ** |
||
6 | // ******************************************************** |
||
7 | // ** It sends data from 4 additionally switches over ** |
||
8 | // ** the tripole "FLAP" channel which the FlightControl ** |
||
9 | // ** can decode "on the fly" :) ** |
||
10 | // ******************************************************** |
||
11 | // ** (c) 2009-0xFFFF Stefan Pendsa ** |
||
12 | // ** License: don't know - use it and have fun ** |
||
13 | // ******************************************************** |
||
14 | |||
15 | |||
16 | #include <avr/io.h> |
||
17 | #include <util/delay.h> |
||
18 | |||
19 | #define TX_High PORTA |= 1 << PORTA0 // Set TX-Pin High |
||
20 | #define TX_Low PORTA &= ~(1 << PORTA0) // Set TX-Pin Low |
||
21 | #define SYNC_High PORTA |= 1 << PORTA1 // Set Sync-Pin High |
||
22 | #define SYNC_Low PORTA &= ~(1 << PORTA1) // Set Sync-Pin Low |
||
23 | |||
24 | |||
25 | |||
26 | void Send(char sync, char data) |
||
27 | { |
||
28 | unsigned char synced; |
||
29 | |||
30 | if (sync == 0) SYNC_High; else SYNC_Low; // Send Sync |
||
31 | if (data == 0) TX_High; else TX_Low; // Send Data |
||
32 | |||
33 | synced = 0; |
||
34 | while (synced == 0) |
||
35 | { |
||
36 | while ( PIND & (1<<PD5) ); // Wait for DX8-Sync goes low |
||
37 | while ( !(PIND & (1<<PD5)) ); // Wait for DX8-Sync goes high |
||
38 | _delay_us(4500); |
||
39 | if ((PIND & (1<<PD5))) synced = 1; // When its high (again) after 4,5ms, we are synced |
||
40 | } |
||
41 | |||
42 | |||
43 | } |
||
44 | |||
45 | |||
46 | |||
47 | int main(void) |
||
48 | { |
||
49 | char i,Parity; // Bit-Counter and Parity Bit |
||
50 | DDRA = 0b011; // PORT A INPUT: RESET; OUTPUT: Sync+Data |
||
51 | DDRB = 0b00000000; // PORT B INPUT: Jumper for Bypass |
||
52 | DDRD = 0b0000000; // PORT D INPUT: Switches + DX7-Sync |
||
53 | PORTA = 0b111; // Pull-Up for /RESET, High for Sync+Data |
||
54 | PORTB = 0b01000000; // Pull-Up for Jumper |
||
55 | PORTD = 0b1011111; // Pull-Up for Switches |
||
56 | |||
57 | while(1) // Main-Loop (Sending data all the time) |
||
58 | { |
||
59 | if (!(PINB & (1<<PINB6))) // By-Pass Mode |
||
60 | { |
||
61 | if (PIND & (1<<PIND0)) PORTA |= 1 << PORTA1; // A1=D0 |
||
62 | else PORTA &= ~(1 << PORTA1); |
||
63 | |||
64 | if (PIND & (1<<PIND1)) PORTA |= 1 << PORTA0; // A0=D1 |
||
65 | else PORTA &= ~(1 << PORTA0); |
||
66 | } |
||
67 | else // Normal Mode |
||
68 | { |
||
69 | Parity = 0; // Reset Parity Bit |
||
70 | Send(1,0); // Send SYNC Pulse |
||
71 | |||
72 | i=0; |
||
73 | while (i<7) // Encode PD0-PD4 + PD6 (F-MODE(2), ELEV(1), MIX, GEAR, ELEV(2)) |
||
74 | { |
||
75 | if (PIND & (1<<i)) // Check Pin #i |
||
76 | { |
||
77 | Send(0,0); // When High, Send Low (Switched to "0") |
||
78 | Parity = ~Parity; // and invert Parity Bit |
||
79 | } |
||
80 | else Send(0,1); // When Low, Send High (Switched to "1") |
||
81 | i++; |
||
82 | if (i == 5) i = 6; // Skip PD5 |
||
83 | } |
||
84 | |||
85 | for (i=0;i<2;i++) // Encode PB0+PB1 (AIL D/R (2)) |
||
86 | { |
||
87 | if ((PINB & (1<<i))) // Check Pin #i |
||
88 | { |
||
89 | Send(0,0); // When High, Send Low (Switched to "0") |
||
90 | Parity = ~Parity; // and invert Parity Bit |
||
91 | } |
||
92 | else Send(0,1); // When Low, Send High (Switched to "1") |
||
93 | } |
||
94 | |||
95 | Send(0,Parity); // Send Parity Bit |
||
96 | } |
||
97 | } |
||
98 | } |