03bc643d7bfecf6edd3461ad74dc5fc50d253cb4
[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 B11111111
13 #define INPUT_SIG_PORTC B11111111
14 #define INPUT_SIG_PORTB B11111111
15 union union16 {
16   byte uint8[2];
17   uint16_t uint16;
18 }; 
19
20 union union32 {
21   byte uint8[4];
22   uint16_t uint16[2];
23   uint32_t uint32;
24 }; 
25
26
27 //********************************************************************//
28
29 typedef unsigned char byte;
30
31 //********************************************************************//
32
33 void start_timer()
34 {
35   // timer 1: 2 ms
36   TCCR1A = 0;                    // prescaler 1:8, WGM = 4 (CTC)
37   TCCR1B = 1<<WGM12 | 1<<CS11;   // 
38   OCR1A = 159;        // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
39 //  OCR1A = 207;        // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
40   TCNT1 = 0;          // reseting timer
41   TIMSK1 = 1<<OCIE1A; // enable Interrupt
42 }
43
44 void stop_timer() // stop the timer
45 {
46   // timer1
47   TCCR1B = 0; // no clock source
48   TIMSK1 = 0; // disable timer interrupt
49 }
50
51 static void PCint(uint8_t port) {
52   union32 data;
53   data.uint32=0;
54   data.uint8[0]=PIND;
55   data.uint8[1]=PINC;
56   data.uint8[2]=PINB;
57   data.uint8[0]|= !PIND_MASK;
58   data.uint8[1]|= !PINC_MASK;
59   data.uint8[2]|= !PINB_MASK;
60   data.uint8[3]=0xff;
61   //PINS with signal:
62   //1
63   //3
64   //9
65   //14
66   //15
67   //21
68   //22
69   //23
70
71   for(int i=0;i<32; i++)
72   {
73     if (! (data.uint32 & 1 ))
74       Serial.println(i);
75     data.uint32>>=1;
76   }
77   //Serial.print(data.uint8[1]);
78 }
79
80
81
82 SIGNAL(PCINT0_vect) {
83   PCint(0);
84 }
85 SIGNAL(PCINT1_vect) {
86   PCint(1);
87 }
88 SIGNAL(PCINT2_vect) {
89   PCint(2);
90 }
91 void setup()
92 {
93 //  pinMode(RF433_PIN, INPUT);      // set pin to input
94 //  digitalWrite(RF433_PIN, LOW);  // turn of pullup resistors 
95   //Set Port as input
96   DDRB=0;
97 // disable pull up
98   PORTB=0;
99   DDRD = DDRD & 3;
100   PORTD= PORTD & 3;
101
102   DDRC=0;
103   PORTC=0;
104   Serial.begin(57600);
105   //Serial.println("starting timer");
106   PCMSK0=PINB_MASK & INPUT_SIG_PORTB;
107   PCMSK1=PINC_MASK & INPUT_SIG_PORTC;
108   PCMSK2=PIND_MASK & INPUT_SIG_PORTD;
109   PCICR|= B111;
110
111 //  start_timer();
112 }
113
114
115 //INPUT PINS digital 2-7 PIND
116 //INPUT PINS digitat 8-12 PINB
117 //INPUT PINS analog 0-4 PINC
118 void loop()
119 {
120 //  Serial.Serial.println("foo");
121 //  return;
122
123
124 }