ac3ac3ea70669ac13476de2922329084c127b426
[svn42.git] / rf433rcv / rf433rcv.pde
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <inttypes.h>
4
5 #define RF433_PIN 10
6 //********************************************************************//
7
8 typedef unsigned char byte;
9
10 typedef struct {
11   byte offset;
12   byte state;
13 } rf_bit_t;
14
15 // offset is number of alphas (0.08ms)
16
17 const rf_bit_t zero_bit[] = { {  4, 1 },
18                               { 16, 0 },
19                               { 20, 1 },
20                               { 32, 0 },
21                               {  0, 0 } };
22
23 const rf_bit_t one_bit[] = { { 12, 1 },
24                              { 16, 0 },
25                              { 28, 1 },
26                              { 32, 0 },
27                              {  0, 0 } };
28
29 const rf_bit_t float_bit[] = { {  4, 1 },
30                                { 16, 0 },
31                                { 28, 1 },
32                                { 32, 0 },
33                                {  0, 0 } };
34
35 const rf_bit_t sync_bit[] = { {   4, 1 },
36                               { 128, 0 },
37                               {   0, 0 } };
38
39 typedef enum { ZERO = 0, ONE , FLOAT , SYNC } adbit_t;
40 typedef byte ad_bit_t;
41 #define WORD_LEN 13
42 typedef ad_bit_t word_t[WORD_LEN];
43
44 const rf_bit_t* bit_defs[] = { zero_bit, one_bit, float_bit, sync_bit };
45
46 byte alpha_cnt = 0;
47 byte bit_cnt = 0;
48 byte chunk_cnt = 0;
49 byte word_cnt = 0;
50 const ad_bit_t* current_word;
51 byte volatile frame_finished = 1;
52
53 #define FRAME_LEN 8
54
55 #define A1_ON  0
56 #define A1_OFF 1
57 #define A2_ON  2
58 #define A2_OFF 3
59
60 #define B1_ON  4
61 #define B1_OFF 5
62 #define B2_ON  6
63 #define B2_OFF 7
64
65 #define C1_ON  8
66 #define C1_OFF 9
67 #define C2_ON  10
68 #define C2_OFF 11
69
70 #define D1_ON  12
71 #define D1_OFF 13
72 #define D2_ON  14
73 #define D2_OFF 15
74
75 const word_t words[]  = { 
76 { ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A1_ON
77 { ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A1_OFF
78 { ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A2_ON
79 { ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A2_OFF
80
81 { FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B1_ON
82 { FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B1_OFF
83 { FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B2_ON
84 { FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B2_OFF
85
86 { ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C1_ON
87 { ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C1_OFF
88 { ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C2_ON
89 { ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C2_OFF
90
91 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D1_ON
92 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // D1_OFF
93 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D2_ON
94 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }  // D2_OFF
95 };
96
97
98 //********************************************************************//
99
100 void start_timer()
101 {
102   // timer 1: 2 ms
103   TCCR1A = 0;                    // prescaler 1:8, WGM = 4 (CTC)
104   TCCR1B = 1<<WGM12 | 1<<CS11;   // 
105   OCR1A = 159;        // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
106 //  OCR1A = 207;        // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
107   TCNT1 = 0;          // reseting timer
108   TIMSK1 = 1<<OCIE1A; // enable Interrupt
109 }
110
111 void stop_timer() // stop the timer
112 {
113   // timer1
114   TCCR1B = 0; // no clock source
115   TIMSK1 = 0; // disable timer interrupt
116 }
117
118 byte rf433_data=0;
119 byte rf433_cnt=0;
120 ISR(TIMER1_COMPA_vect)
121 {
122   rf433_data<<=1;
123   if (digitalRead(RF433_PIN) == HIGH)
124     rf433_data |=1;
125   rf433_cnt++;
126   if (rf433_cnt>7)
127   {
128     Serial.print(rf433_data);
129     rf433_cnt=0;
130     rf433_data=0;
131   }
132 }
133
134 //unsigned long wm_start_[3]={0,0,0};
135 //bool wait_millis(unsigned long *start_time, unsigned long ms)
136 //{
137 //  if (ms == 0)
138 //    return false;
139 //  else if (*start_time > 0)
140 //  {
141 //    if (millis() < *start_time || millis() > (*start_time) + ms)
142 //    {
143 //      *start_time = 0;
144 //      return false;
145 //    }
146 //    else
147 //      return true;
148 //  }
149 //  else
150 //  {
151 //    *start_time=millis();
152 //    return true;
153 //  }
154 //}
155
156 //********************************************************************//
157
158 void setup()
159 {
160   pinMode(RF433_PIN, INPUT);      // set pin to input
161   digitalWrite(RF433_PIN, LOW);  // turn of pullup resistors 
162
163   Serial.begin(57600);
164 //  Serial.println("starting timer");
165   start_timer();
166 }
167
168
169 void loop()
170 {
171 }