Rev 122 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 122 | Rev 124 | ||
|---|---|---|---|
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * IR reciver module |
3 | * IR reciver module |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canIR/Source/IRec.c $ |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canIR/Source/IRec.c $ |
| 7 | * Last changed: $LastChangedDate: 2006-11-18 |
7 | * Last changed: $LastChangedDate: 2006-11-18 22:14:33 +0100 (Sat, 18 Nov 2006) $ |
| 8 | * By: $LastChangedBy: johboh $ |
8 | * By: $LastChangedBy: johboh $ |
| 9 | * Revision: $Revision: |
9 | * Revision: $Revision: 124 $ |
| 10 | ********************************************************************/ |
10 | ********************************************************************/ |
| 11 | 11 | ||
| 12 | #include <IRec.h> |
12 | #include <IRec.h> |
| 13 | 13 | ||
| 14 | #include <CAN.h> |
14 | #include <CAN.h> |
| 15 | #include <funcdefs.h> |
15 | #include <funcdefs.h> |
| 16 | 16 | ||
| 17 | #ifdef USE_IREC |
17 | #ifdef USE_IREC |
| 18 | 18 | ||
| 19 | static BYTE toggle = 0; |
19 | static BYTE toggle = 0; |
| 20 | static BYTE addr = 0; |
20 | static BYTE addr = 0; |
| 21 | static BYTE data = 0; |
21 | static BYTE data = 0; |
| 22 | static BYTE bitIndex = 0; |
22 | static BYTE bitIndex = 0; |
| 23 | static IR_TYPE type = IR_NONE; |
23 | static IR_TYPE type = IR_NONE; |
| 24 | 24 | ||
| 25 | /* |
25 | /* |
| 26 | * Function: irecInit |
26 | * Function: irecInit |
| 27 | * |
27 | * |
| 28 | * Input: none |
28 | * Input: none |
| 29 | * Output: none |
29 | * Output: none |
| 30 | * Pre-conditions: none |
30 | * Pre-conditions: none |
| 31 | * Affects: none |
31 | * Affects: none |
| 32 | * Depends: TIMER 3 is free |
32 | * Depends: TIMER 3 is free |
| 33 | */ |
33 | */ |
| 34 | void irecInit() |
34 | void irecInit() |
| 35 | { |
35 | { |
| 36 | 36 | ||
| 37 | // Setup timer 3 to be source for CCP1 |
37 | // Setup timer 3 to be source for CCP1 |
| 38 | T3CON = 0b11111001; |
38 | T3CON = 0b11111001; |
| 39 | 39 | ||
| 40 | // configure timer 3 interrupt (for overflow) |
40 | // configure timer 3 interrupt (for overflow) |
| 41 | PIR2bits.TMR3IF = 0; |
41 | PIR2bits.TMR3IF = 0; |
| 42 | PIE2bits.TMR3IE = 1; |
42 | PIE2bits.TMR3IE = 1; |
| 43 | IPR2bits.TMR3IP = 1; |
43 | IPR2bits.TMR3IP = 1; |
| 44 | 44 | ||
| 45 | // Setup CCP1 to use capture on falling edge |
45 | // Setup CCP1 to use capture on falling edge |
| 46 | CCP1CON=0x04; |
46 | CCP1CON=0x04; |
| 47 | 47 | ||
| 48 | // Enable CCP1 interrupt and set priority to high. |
48 | // Enable CCP1 interrupt and set priority to high. |
| 49 | PIR1bits.CCP1IF=0; |
49 | PIR1bits.CCP1IF=0; |
| 50 | PIE1bits.CCP1IE=1; |
50 | PIE1bits.CCP1IE=1; |
| 51 | IPR1bits.CCP1IP=1; |
51 | IPR1bits.CCP1IP=1; |
| 52 | 52 | ||
| 53 | } |
53 | } |
| 54 | 54 | ||
| 55 | /* |
55 | /* |
| 56 | * Function: irecISR |
56 | * Function: irecISR |
| 57 | * |
57 | * |
| 58 | * Input: none |
58 | * Input: none |
| 59 | * Output: none |
59 | * Output: none |
| 60 | * Pre-conditions: a call to irecInit() |
60 | * Pre-conditions: a call to irecInit() |
| 61 | * Affects: ... |
61 | * Affects: ... |
| 62 | * Depends: ... |
62 | * Depends: ... |
| 63 | */ |
63 | */ |
| 64 | void irecISR() |
64 | void irecISR() |
| 65 | { |
65 | { |
| 66 | static WORD timediff = 0; |
66 | static WORD timediff = 0; |
| 67 | static BOOL started = FALSE; |
67 | static BOOL started = FALSE; |
| 68 | static BYTE lastbit = 1; |
68 | static BYTE lastbit = 1; |
| 69 | static BOOL second = FALSE; |
69 | static BOOL second = FALSE; |
| 70 | 70 | ||
| 71 | // If IR bit |
71 | // If IR bit |
| 72 | if(PIR1bits.CCP1IF && PIE1bits.CCP1IE) |
72 | if(PIR1bits.CCP1IF && PIE1bits.CCP1IE) |
| 73 | { |
73 | { |
| 74 | // Read timediff |
74 | // Read timediff |
| 75 | timediff = TMR3L; |
75 | timediff = TMR3L; |
| 76 | timediff += (((WORD)TMR3H)<<8); |
76 | timediff += (((WORD)TMR3H)<<8); |
| 77 | 77 | ||
| 78 | // Clear timer. |
78 | // Clear timer. |
| 79 | TMR3H = 0; |
79 | TMR3H = 0; |
| 80 | TMR3L = 0; |
80 | TMR3L = 0; |
| 81 | 81 | ||
| 82 | // Wait on inverse edge |
82 | // Wait on inverse edge |
| 83 | CCP1CONbits.CCP1M0=~CCP1CONbits.CCP1M0; |
83 | CCP1CONbits.CCP1M0=~CCP1CONbits.CCP1M0; |
| 84 | 84 | ||
| 85 | // Disable interrupt |
85 | // Disable interrupt |
| 86 | PIR1bits.CCP1IF=0; |
86 | PIR1bits.CCP1IF=0; |
| 87 | 87 | ||
| - | 88 | // If found RC5 och RC5X startbit... |
|
| 88 | if (started==TRUE && (type==IR_RC5 || type==IR_RC5X)) |
89 | if (started==TRUE && (type==IR_RC5 || type==IR_RC5X)) |
| 89 | { |
90 | { |
| - | 91 | // Parse message |
|
| 90 | 92 | ||
| 91 | if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE) |
93 | if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE) |
| 92 | { |
94 | { |
| 93 | if (bitIndex==0) toggle=lastbit; |
95 | if (bitIndex==0) toggle=lastbit; |
| 94 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
96 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
| 95 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
97 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
| 96 | bitIndex++; |
98 | bitIndex++; |
| 97 | 99 | ||
| 98 | second = TRUE; |
100 | second = TRUE; |
| 99 | return; |
101 | return; |
| 100 | } |
102 | } |
| 101 | 103 | ||
| 102 | if ((t4-250)<timediff && timediff<(t4+250) && second==FALSE) |
104 | if ((t4-250)<timediff && timediff<(t4+250) && second==FALSE) |
| 103 | { |
105 | { |
| 104 | lastbit=(lastbit?0:1); |
106 | lastbit=(lastbit?0:1); |
| 105 | 107 | ||
| 106 | if (bitIndex==0) toggle=lastbit; |
108 | if (bitIndex==0) toggle=lastbit; |
| 107 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
109 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
| 108 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
110 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
| 109 | bitIndex++; |
111 | bitIndex++; |
| 110 | 112 | ||
| 111 | second = TRUE; |
113 | second = TRUE; |
| 112 | return; |
114 | return; |
| 113 | } |
115 | } |
| 114 | 116 | ||
| 115 | if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE) |
117 | if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE) |
| 116 | { |
118 | { |
| 117 | second = FALSE; |
119 | second = FALSE; |
| 118 | return; |
120 | return; |
| 119 | } |
121 | } |
| 120 | 122 | ||
| 121 | if ((t4-250)<timediff && timediff<(t4+250) && second==TRUE) |
123 | if ((t4-250)<timediff && timediff<(t4+250) && second==TRUE) |
| 122 | { |
124 | { |
| 123 | lastbit=(lastbit?0:1); |
125 | lastbit=(lastbit?0:1); |
| 124 | 126 | ||
| 125 | if (bitIndex==0) toggle=lastbit; |
127 | if (bitIndex==0) toggle=lastbit; |
| 126 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
128 | if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex)); |
| 127 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
129 | if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex)); |
| 128 | bitIndex++; |
130 | bitIndex++; |
| 129 | 131 | ||
| 130 | return; |
132 | return; |
| 131 | } |
133 | } |
| 132 | 134 | ||
| 133 | } // if started and type == RC5 or RC5X |
135 | } // if started and type == RC5 or RC5X |
| 134 | 136 | ||
| 135 | 137 | ||
| - | 138 | // If RC6... |
|
| 136 | if (started==TRUE && type==IR_RC6) |
139 | if (started==TRUE && type==IR_RC6) |
| 137 | { |
140 | { |
| 138 | 141 | ||
| 139 | if ((t1-250)<timediff && timediff<(t1+250) && second==FALSE) |
142 | if ((t1-250)<timediff && timediff<(t1+250) && second==FALSE) |
| 140 | { |
143 | { |
| 141 | if (bitIndex==0) toggle=lastbit; |
144 | if (bitIndex==0) toggle=lastbit; |
| 142 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
145 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
| 143 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
146 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
| 144 | bitIndex++; |
147 | bitIndex++; |
| 145 | 148 | ||
| 146 | second = TRUE; |
149 | second = TRUE; |
| 147 | return; |
150 | return; |
| 148 | } |
151 | } |
| 149 | 152 | ||
| 150 | if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE) |
153 | if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE) |
| 151 | { |
154 | { |
| 152 | lastbit=(lastbit?0:1); |
155 | lastbit=(lastbit?0:1); |
| 153 | 156 | ||
| 154 | if (bitIndex==0) toggle=lastbit; |
157 | if (bitIndex==0) toggle=lastbit; |
| 155 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
158 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
| 156 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
159 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
| 157 | bitIndex++; |
160 | bitIndex++; |
| 158 | 161 | ||
| 159 | second = TRUE; |
162 | second = TRUE; |
| 160 | return; |
163 | return; |
| 161 | } |
164 | } |
| 162 | 165 | ||
| 163 | if ((t1-250)<timediff && timediff<(t1+250) && second==TRUE) |
166 | if ((t1-250)<timediff && timediff<(t1+250) && second==TRUE) |
| 164 | { |
167 | { |
| 165 | second = FALSE; |
168 | second = FALSE; |
| 166 | return; |
169 | return; |
| 167 | } |
170 | } |
| 168 | 171 | ||
| 169 | if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE) |
172 | if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE) |
| 170 | { |
173 | { |
| 171 | lastbit=(lastbit?0:1); |
174 | lastbit=(lastbit?0:1); |
| 172 | 175 | ||
| 173 | if (bitIndex==0) toggle=lastbit; |
176 | if (bitIndex==0) toggle=lastbit; |
| 174 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
177 | if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex)); |
| 175 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
178 | if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex)); |
| 176 | bitIndex++; |
179 | bitIndex++; |
| 177 | 180 | ||
| 178 | return; |
181 | return; |
| 179 | } |
182 | } |
| 180 | 183 | ||
| 181 | } // if started and type == RC6 |
184 | } // if started and type == RC6 |
| 182 | 185 | ||
| 183 | 186 | ||
| 184 | 187 | ||
| 185 | bitIndex++; |
188 | bitIndex++; |
| 186 | 189 | ||
| 187 | 190 | ||
| 188 | if (started==FALSE && type==IR_NONE && bitIndex==2 && (t2-250)<timediff && timediff<(t2+250)) |
191 | if (started==FALSE && type==IR_NONE && bitIndex==2 && (t2-250)<timediff && timediff<(t2+250)) |
| 189 | { |
192 | { |
| 190 | type=IR_RC5; |
193 | type=IR_RC5; |
| 191 | } |
194 | } |
| 192 | else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t4-250)<timediff && timediff<(t4+250)) |
195 | else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t4-250)<timediff && timediff<(t4+250)) |
| 193 | { |
196 | { |
| 194 | started = TRUE; |
197 | started = TRUE; |
| 195 | bitIndex = 0; |
198 | bitIndex = 0; |
| 196 | lastbit = 1; |
199 | lastbit = 1; |
| 197 | type=IR_RC5X; |
200 | type=IR_RC5X; |
| 198 | } |
201 | } |
| 199 | else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t6-250)<timediff && timediff<(t6+250)) |
202 | else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t6-250)<timediff && timediff<(t6+250)) |
| 200 | { |
203 | { |
| 201 | type=IR_RC6; |
204 | type=IR_RC6; |
| 202 | } |
205 | } |
| 203 | else if (started==FALSE && type==IR_RC6 && bitIndex==9) |
206 | else if (started==FALSE && type==IR_RC6 && bitIndex==9) |
| 204 | { |
207 | { |
| 205 | started = TRUE; |
208 | started = TRUE; |
| 206 | bitIndex = 0; |
209 | bitIndex = 0; |
| 207 | lastbit = 0; |
210 | lastbit = 0; |
| 208 | type=IR_RC6; |
211 | type=IR_RC6; |
| 209 | second = TRUE; |
212 | second = TRUE; |
| 210 | } |
213 | } |
| 211 | else if (started==FALSE && type==IR_RC5 && bitIndex==3 && (t2-250)<timediff && timediff<(t2+250)) |
214 | else if (started==FALSE && type==IR_RC5 && bitIndex==3 && (t2-250)<timediff && timediff<(t2+250)) |
| 212 | { |
215 | { |
| 213 | started = TRUE; |
216 | started = TRUE; |
| 214 | bitIndex = 0; |
217 | bitIndex = 0; |
| 215 | lastbit = 1; |
218 | lastbit = 1; |
| 216 | type=IR_RC5; |
219 | type=IR_RC5; |
| 217 | } |
220 | } |
| 218 | else if(bitIndex>2 && type!=IR_RC6) |
221 | else if(bitIndex>2 && type!=IR_RC6) |
| 219 | { |
222 | { |
| 220 | type=IR_NONE; |
223 | type=IR_NONE; |
| 221 | } |
224 | } |
| 222 | else if(bitIndex>9) |
225 | else if(bitIndex>9) |
| 223 | { |
226 | { |
| 224 | type=IR_NONE; |
227 | type=IR_NONE; |
| 225 | } |
228 | } |
| 226 | 229 | ||
| 227 | } |
230 | } |
| 228 | 231 | ||
| 229 | 232 | ||
| 230 | 233 | ||
| 231 | 234 | ||
| 232 | // If timer 3 overflow |
235 | // If timer 3 overflow |
| 233 | if(PIR2bits.TMR3IF && PIE2bits.TMR3IE) |
236 | if(PIR2bits.TMR3IF && PIE2bits.TMR3IE) |
| 234 | { |
237 | { |
| 235 | 238 | ||
| 236 | PIR2bits.TMR3IF=0; |
239 | PIR2bits.TMR3IF=0; |
| 237 | 240 | ||
| 238 | if (type!=IR_NONE) |
241 | if (type!=IR_NONE) |
| 239 | { |
242 | { |
| 240 | irecParse(type,toggle,addr,data); |
243 | irecParse(type,toggle,addr,data); |
| 241 | } |
244 | } |
| 242 | 245 | ||
| 243 | started = FALSE; |
246 | started = FALSE; |
| 244 | bitIndex = 0; |
247 | bitIndex = 0; |
| 245 | addr = 0; |
248 | addr = 0; |
| 246 | toggle = 0; |
249 | toggle = 0; |
| 247 | data = 0; |
250 | data = 0; |
| 248 | type = IR_NONE; |
251 | type = IR_NONE; |
| 249 | 252 | ||
| 250 | // wait in falling edge. |
253 | // wait in falling edge. |
| 251 | CCP1CONbits.CCP1M0=0; |
254 | CCP1CONbits.CCP1M0=0; |
| 252 | 255 | ||
| 253 | } |
256 | } |
| 254 | 257 | ||
| 255 | } |
258 | } |
| 256 | 259 | ||
| 257 | 260 | ||
| 258 | 261 | ||
| 259 | #endif |
262 | #endif |
| 260 | 263 | ||