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