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