added rf433rcv (non working)
[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 ISR(TIMER1_COMPA_vect)
119 {
120   digitalRead(RF433_PIN);
121 }
122
123 //unsigned long wm_start_[3]={0,0,0};
124 //bool wait_millis(unsigned long *start_time, unsigned long ms)
125 //{
126 //  if (ms == 0)
127 //    return false;
128 //  else if (*start_time > 0)
129 //  {
130 //    if (millis() < *start_time || millis() > (*start_time) + ms)
131 //    {
132 //      *start_time = 0;
133 //      return false;
134 //    }
135 //    else
136 //      return true;
137 //  }
138 //  else
139 //  {
140 //    *start_time=millis();
141 //    return true;
142 //  }
143 //}
144
145 //********************************************************************//
146
147 void setup()
148 {
149   pinMode(RF433_PIN, INPUT);      // set pin to input
150   digitalWrite(RF433_PIN, LOW);  // turn of pullup resistors 
151
152   Serial.begin(57600);
153   start_timer();
154 }
155
156
157 void loop()
158 {
159 }