rm documents to github
[svn42.git] / dart / dart.pde
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <inttypes.h>
4
5 //Player+Handycap BTN analog 5
6 #define PLAYER_SIG_PORTC B00100000
7 //INPUT PINS digital 2-7 PIND
8 #define PIND_MASK B11111100
9 //INPUT PINS digitat 8-12 PINB
10 #define PINB_MASK B00011111
11 //INPUT PINS analog 0-4 PINC
12 #define PINC_MASK B00011111
13
14 #define INPUT_SIG_PORTD B11000000
15 #define INPUT_SIG_PORTB B00011111
16 #define INPUT_SIG_PORTC B00010000
17
18 #define OUTPUT_SIG_PORTB ( PINB_MASK & ~INPUT_SIG_PORTB )
19 // B00011111 & ! B00011111 = 0
20 #define OUTPUT_SIG_PORTC ( PINC_MASK & ~INPUT_SIG_PORTC )
21 // B00011111 & ! B00010000 = B00001111
22 #define OUTPUT_SIG_PORTD ( PIND_MASK & ~INPUT_SIG_PORTD )
23 // B11111100 & ! B11000000 = 00111100
24 union union16 {
25   byte uint8[2];
26   uint16_t uint16;
27 }; 
28
29 uint8_t zahlen[] = {115,110,46,78,51,83,82,68,99,105,41,73,35,67,50,36,113,108,44,76,49,81,114,100,98,101,37,69,34,66,102,109,111,116,52,84,47,79,57,89,106,97,33,65,42,74,38,45,112,104,40,72,48,80,0,0,103,107,43,75,39,71,70,77} ;
30
31 union union32 {
32   byte uint8[4];
33   uint16_t uint16[2];
34   uint32_t uint32;
35 }; 
36
37 typedef unsigned char byte;
38
39 void start_timer()
40 {
41   // timer 1: 2 ms
42   TCCR1A = 0;                    // prescaler 1:8, WGM = 4 (CTC)
43   TCCR1B = 1<<WGM12 | 1<<CS10  | 1<<CS11;    // 
44   OCR1A = 65000;        // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
45 //  OCR1A = 207;        // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
46   TCNT1 = 0;          // reseting timer
47   TIMSK1 = 1<<OCIE1A; // enable Interrupt
48 }
49
50 void stop_timer() // stop the timer
51 {
52   // timer1
53   TCCR1B = 0; // no clock source
54   TIMSK1 = 0; // disable timer interrupt
55 }
56
57 ISR(TIMER1_COMPA_vect)
58 {
59   stop_timer();
60   PCICR|= B111;
61 }
62
63 static void send_dart(byte input,byte output)
64 {
65   byte value=0;
66   while(input>>=1)
67     value++;
68   value<<=3;
69   while(output>>=1)
70     value++;
71   
72         uint8_t zahl = zahlen[value];
73   uint8_t multi = zahl >> 5;
74   uint8_t base = zahl & B11111;
75   Serial.print(0+multi);
76   Serial.print("\t");
77   Serial.println(0+base);
78 }
79
80 static void send_btn(byte btn)
81 {
82   uint8_t value=0;
83   while(btn>>=1)
84     value++;
85   Serial.print("btn\t");
86   Serial.println(0+value);
87         
88 }
89
90 static void PCint() {
91   byte PINB_COPY = PINB;
92   byte PINC_COPY = PINC;
93   byte PIND_COPY = PIND;
94   byte output = ( OUTPUT_SIG_PORTC  & ~ PINC_COPY ) | (( OUTPUT_SIG_PORTD  & ~ PIND_COPY ) <<2 ); // no output on B 
95   byte input = ( INPUT_SIG_PORTB  & ~ PINB_COPY ) | ( ( INPUT_SIG_PORTC  & ~ PINC_COPY ) <<1 ) |( INPUT_SIG_PORTD  & ~ PIND_COPY );
96   byte btn = ( PLAYER_SIG_PORTC  & ~ PINC_COPY ); 
97
98   if ( input && output)
99    send_dart(input,output);
100   else if (btn)
101    send_btn(btn);
102         else
103    return;
104
105         //Timeout verhindert zu schnelle Wiederholungen
106   PCICR&=  ~ B111; // Disable Interrupt
107   start_timer();
108 }
109
110
111
112 SIGNAL(PCINT0_vect) {
113   PCint();
114 }
115 SIGNAL(PCINT1_vect) {
116   PCint();
117 }
118 SIGNAL(PCINT2_vect) {
119   PCint();
120 }
121 void setup()
122 {
123 //  pinMode(RF433_PIN, INPUT);      // set pin to input
124 //  digitalWrite(RF433_PIN, LOW);  // turn of pullup resistors 
125   //Set Port as input
126   DDRB=0;
127 // disable pull up
128   PORTB=0;
129   DDRD = DDRD & 3;
130   PORTD= PORTD & 3;
131
132   DDRC=0;
133   PORTC=0;
134   Serial.begin(57600);
135   PCMSK0=PINB_MASK & INPUT_SIG_PORTB;
136   PCMSK1=(PINC_MASK & INPUT_SIG_PORTC) |  PLAYER_SIG_PORTC;
137   PCMSK2=PIND_MASK & INPUT_SIG_PORTD;
138   PCICR|= B111;
139 }
140
141
142 void loop()
143 {
144 }