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