ASURO Library  2.80
rc5.c
gehe zur Dokumentation dieser Datei
1 /*
2  * rc5.c Infrarot Fernbedienung Funktionen
3  *
4  * This program is free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General
6  * Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  * This program is distributed in the hope that it will be
10  * useful, but WITHOUT ANY WARRANTY; without even the implied
11  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  * PURPOSE. See the GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the Free
15  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
16  * MA 02111-1307, USA.
17  *
18  */
19 
35 // Infos ueber RC6: http://www.xs4all.nl/~sbp/knowledge/ir/rc6.htm
36 // http://www.xs4all.nl/~sbp/knowledge/ir/ir.htm
37 
38 // ========================================================================
39 // RC5 Infrarot-Empfaenger
40 // ========================================================================
41 #include <avr/io.h>
42 #include "asuro.h"
43 #include "rc5.h"
44 
45 
46 // -----------------------------------------------------------------------------
47 // Timing
48 // -----------------------------------------------------------------------------
49 #define IR_SAMPLES_PER_BIT 8
50 #define IR_SAMPLES_PER_BIT_EARLY 6
51 #define IR_SAMPLES_PER_BIT_LATE 10
52 #define IR_SAMPLES_PER_BIT_MIN 3
53 #define IR_PAUSE_SAMPLES 250
54 // Pegelaenderung gueltig -- eigentlich muesste
55 // man rund 500 Samples abwarten (50 x
56 // Bitzeit), doch weil der Samplezaehler ein
57 // Byte ist, beschraenken wir uns hier auf ein
58 // Minimum von 250 Samples
59 
60 #define IR_PORT PORTD
61 #define IR_DDR DDRD
62 #define IR_PINR PIND
63 #define IR_PIN PD0
66 static uint8_t RC5lastsample = 0;
67 static uint8_t RC5bittimer = 0;
69 static uint16_t RC5data_tmp = 0;
70 static uint8_t RC5bitcount = 0;
72 volatile uint16_t RC5data = 0;
73 volatile uint8_t enableRC5 = 0;
79 void IsrRC5 (void)
80 {
81  // sample lesen
82  uint8_t sample = 1;
83  if (enableRC5 && !(count36kHz % 8))
84  {
85  if ((IR_PINR & (1<<IR_PIN)) != 0)
86  {
87  sample = 0;
88  }
89 
90  // bittimer erhoehen (bleibt bei 255 stehen)
91  if (RC5bittimer<255)
92  {
93  RC5bittimer++;
94  }
95 
96  // flankenerkennung
97  if ( RC5lastsample != sample)
98  {
99  if (RC5bittimer<=IR_SAMPLES_PER_BIT_MIN)
100  {
101  // flanke kommt zu frueh: paket verwerfen
102  RC5bitcount=0;
103  }
104  else
105  {
106  // Startbit
107  if (RC5bitcount==0)
108  {
109  if ( (sample==1) && (RC5bittimer>IR_PAUSE_SAMPLES) )
110  {
111  // Startbit speichern
112  RC5data_tmp = 1;
113  RC5bitcount++;
114  }
115  else
116  {
117  // error
118  RC5data_tmp = 0;
119  }
120 
121  // bittimer-reset
122  RC5bittimer = 0;
123 
124  // Bits 2..14: nur Flanken innerhalb des Bits beruecksichtigen
125  }
126  else
127  {
128  if (RC5bittimer >= IR_SAMPLES_PER_BIT_EARLY)
129  {
130  if (RC5bittimer<=IR_SAMPLES_PER_BIT_LATE)
131  {
132  // Bit speichern
133  RC5data_tmp = (RC5data_tmp<<1) | sample;
134  RC5bitcount++;
135  }
136  else
137  {
138  // zu spaet: paket verwerfen
139  RC5bitcount = 0;
140  }
141 
142  // bittimer-reset
143  RC5bittimer = 0;
144  }
145  }
146  }
147 
148  }
149  else
150  {
151  // keine flanke innerhalb bitzeit?
152  if (RC5bittimer > IR_SAMPLES_PER_BIT_LATE)
153  {
154  // 14 bits gelesen?
155  if (RC5bitcount==14)
156  {
157  RC5data = RC5data_tmp;
158  }
159  // paket verwerfen
160  RC5bitcount = 0;
161  }
162  }
163 
164  // sample im samplepuffer ablegen
165  RC5lastsample = sample;
166  }
167 
168 }
169 
170 
175 uint16_t ReadRC5 (void)
176 {
177  uint16_t retvalue = RC5data;
178  RC5data = 0;
179  return retvalue;
180 }
181 
185 void InitRC5 (void)
186 {
187  IR_DDR &= ~IR_PIN; // Pin auf Input
188  IR_PORT |= IR_PIN; // Pullup an
189  enableRC5 = 1;
191 }
192 
193