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