Rev 157 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 157 | Rev 201 | ||
|---|---|---|---|
| Line 11... | Line 11... | ||
| 11 | * Includes |
11 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | #include "irreceiver.h" |
13 | #include "irreceiver.h" |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | 15 | ||
| - | 16 | /*----------------------------------------------------------------------------- |
|
| - | 17 | * Globals |
|
| - | 18 | *---------------------------------------------------------------------------*/ |
|
| - | 19 | volatile uint16_t lastProtoTimeout = 0; |
|
| - | 20 | ||
| 16 | 21 | ||
| 17 | /*----------------------------------------------------------------------------- |
22 | /*----------------------------------------------------------------------------- |
| 18 | * Prerequisites |
23 | * Prerequisites |
| 19 | *---------------------------------------------------------------------------*/ |
24 | *---------------------------------------------------------------------------*/ |
| 20 | 25 | ||
| 21 | 26 | ||
| 22 | /*----------------------------------------------------------------------------- |
27 | /*----------------------------------------------------------------------------- |
| 23 | * Public Functions |
28 | * Public Functions |
| 24 | *---------------------------------------------------------------------------*/ |
29 | *---------------------------------------------------------------------------*/ |
| 25 | 30 | ||
| 26 | void initTimer(void) { |
31 | void initTimer(void) { |
| 27 | //sätt upp timer |
32 | //sätt upp timer |
| 28 | #if defined(__AVR_ATmega8__) |
33 | #if defined(__AVR_ATmega8__) |
| 29 | TCCR1B = (0<<CS11) | (1<<CS10); // prescaler: 1 |
34 | TCCR1B = (0<<CS11) | (1<<CS10); // prescaler: 1 |
| 30 | TIFR |= (1<<TOV1); // clear overflow flag. |
35 | TIFR |= (1<<TOV1); // clear overflow flag. |
| 31 | TCNT1 = 0; |
36 | TCNT1 = 0; |
| 32 | #endif |
37 | #endif |
| 33 | #if defined(__AVR_ATmega88__) |
38 | #if defined(__AVR_ATmega88__) |
| 34 | TCCR1B = (0<<CS11) | (1<<CS10); // prescaler: 1 |
39 | TCCR1B = (0<<CS11) | (1<<CS10); // prescaler: 1 |
| 35 | TIFR1 |= (1<<TOV1); // clear overflow flag. |
40 | TIFR1 |= (1<<TOV1); // clear overflow flag. |
| 36 | TCNT1 = 0; |
41 | TCNT1 = 0; |
| 37 | #endif |
42 | #endif |
| 38 | } |
43 | } |
| 39 | 44 | ||
| 40 | int isTimerOvfl(void) { |
45 | int isTimerOvfl(void) { |
| 41 | #if defined(__AVR_ATmega8__) |
46 | #if defined(__AVR_ATmega8__) |
| 42 | if (TIFR & (1<<TOV1)) return 1; |
47 | if (TIFR & (1<<TOV1)) return 1; |
| 43 | #endif |
48 | #endif |
| Line 76... | Line 81... | ||
| 76 | int receiveRC5(uint8_t *address, uint8_t *command) { |
81 | int receiveRC5(uint8_t *address, uint8_t *command) { |
| 77 | //printf("startbit of RC5!\n"); |
82 | //printf("startbit of RC5!\n"); |
| 78 | uint8_t i; |
83 | uint8_t i; |
| 79 | //read second startbit, togglebit and the 5 addressbits |
84 | //read second startbit, togglebit and the 5 addressbits |
| 80 | for (i=0; i<7; i++) { |
85 | for (i=0; i<7; i++) { |
| 81 | //set up a quarter of a bit period time |
86 | //set up a quarter of a bit period time |
| 82 | setTimerVal(0xFFFF-(IR_RC5_BIT/4)); |
87 | setTimerVal(0xFFFF-(IR_RC5_BIT/4)); |
| 83 | //and wait for time to elapse |
88 | //and wait for time to elapse |
| 84 | while(!isTimerOvfl()); |
89 | while(!isTimerOvfl()); |
| 85 | //reset timer ovfl flag |
90 | //reset timer ovfl flag |
| 86 | setTimerVal(0); |
91 | setTimerVal(0); |
| 87 | if (IRPIN & (1<<IRBIT)) { |
92 | if (IRPIN & (1<<IRBIT)) { |
| 88 | //save a one |
93 | //save a one |
| 89 | *address |= (1<<(6-i)); |
94 | *address |= (1<<(6-i)); |
| 90 | //wait for negative slope, while checking timer overflow |
95 | //wait for negative slope, while checking timer overflow |
| 91 | while ((IRPIN & (1<<IRBIT))) { |
96 | while ((IRPIN & (1<<IRBIT))) { |
| 92 | //om timeout (om timer-ovflow-flaggan sätt) |
97 | //om timeout (om timer-ovflow-flaggan sätt) |
| 93 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
98 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
| 94 | } |
99 | } |
| 95 | } else { |
100 | } else { |
| 96 | //save a zero |
101 | //save a zero |
| 97 | 102 | ||
| 98 | //wait for positiv slope |
103 | //wait for positiv slope |
| 99 | while (!(IRPIN & (1<<IRBIT))) { |
104 | while (!(IRPIN & (1<<IRBIT))) { |
| 100 | //om timeout (om timer-ovflow-flaggan sätt) |
105 | //om timeout (om timer-ovflow-flaggan sätt) |
| 101 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
106 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
| 102 | } |
107 | } |
| 103 | } |
108 | } |
| 104 | //set up a half a bit period time |
109 | //set up a half a bit period time |
| 105 | setTimerVal(0xFFFF-(IR_RC5_BIT/2)); |
110 | setTimerVal(0xFFFF-(IR_RC5_BIT/2)); |
| 106 | //and wait for time to elapse |
111 | //and wait for time to elapse |
| 107 | while(!isTimerOvfl()); |
112 | while(!isTimerOvfl()); |
| 108 | } |
113 | } |
| 109 | 114 | ||
| 110 | //read 5 command bits |
115 | //read 5 command bits |
| 111 | for (i=0; i<6; i++) { |
116 | for (i=0; i<6; i++) { |
| 112 | //set up a quarter of a bit period time |
117 | //set up a quarter of a bit period time |
| 113 | setTimerVal(0xFFFF-(IR_RC5_BIT/4)); |
118 | setTimerVal(0xFFFF-(IR_RC5_BIT/4)); |
| Line 123... | Line 128... | ||
| 123 | //om timeout (om timer-ovflow-flaggan sätt). |
128 | //om timeout (om timer-ovflow-flaggan sätt). |
| 124 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
129 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
| 125 | } |
130 | } |
| 126 | } else { |
131 | } else { |
| 127 | //save a zero |
132 | //save a zero |
| 128 | 133 | ||
| 129 | //wait for positiv slope |
134 | //wait for positiv slope |
| 130 | while (!(IRPIN & (1<<IRBIT))) { |
135 | while (!(IRPIN & (1<<IRBIT))) { |
| 131 | //om timeout (om timer-ovflow-flaggan sätt) |
136 | //om timeout (om timer-ovflow-flaggan sätt) |
| 132 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
137 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
| 133 | } |
138 | } |
| Line 150... | Line 155... | ||
| 150 | *address &= 0x1f; |
155 | *address &= 0x1f; |
| 151 | //printf("address %i\n", *address); |
156 | //printf("address %i\n", *address); |
| 152 | //printf("command %i\n", *command); |
157 | //printf("command %i\n", *command); |
| 153 | 158 | ||
| 154 | return IR_OK; |
159 | return IR_OK; |
| - | 160 | } |
|
| - | 161 | ||
| - | 162 | uint16_t getLastProtoTimeout(void) { |
|
| - | 163 | return lastProtoTimeout; |
|
| 155 | } |
164 | } |
| 156 | 165 | ||
| 157 | /** |
166 | /** |
| 158 | * Checks if ir-receiver is outputting data, if so then receive it |
167 | * Checks if ir-receiver is outputting data, if so then receive it |
| 159 | * |
168 | * |
| Line 168... | Line 177... | ||
| 168 | */ |
177 | */ |
| 169 | int checkIr(uint8_t *proto, uint8_t *address, uint8_t *command) { |
178 | int checkIr(uint8_t *proto, uint8_t *address, uint8_t *command) { |
| 170 | if (IRPIN & (1<<IRBIT)) return IR_NO_DATA; //om irmodulen ger en etta så återgå |
179 | if (IRPIN & (1<<IRBIT)) return IR_NO_DATA; //om irmodulen ger en etta så återgå |
| 171 | 180 | ||
| 172 | //nu lägger irmodulen ut en nolla, "startbiten" alltså |
181 | //nu lägger irmodulen ut en nolla, "startbiten" alltså |
| 173 | 182 | ||
| - | 183 | uint16_t timerVal; |
|
| - | 184 | ||
| - | 185 | *address = 0; |
|
| - | 186 | *command = 0; |
|
| - | 187 | ||
| - | 188 | //Läs in längden på startbiten |
|
| - | 189 | initTimer(); |
|
| - | 190 | while (!(IRPIN & (1<<IRBIT))) { //vänta på att irmodulen lägger ut en etta |
|
| - | 191 | //om timeout (om timer-ovflow-flaggan sätt) |
|
| - | 192 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
|
| - | 193 | } |
|
| - | 194 | timerVal = getTimerVal(); |
|
| - | 195 | ||
| - | 196 | /* |
|
| - | 197 | * Anropa olika funktioner beroende på längden |
|
| - | 198 | * för vissa protokoll behöver längden på första biten skickas med till funktionen? |
|
| - | 199 | * t.ex. sharp? |
|
| - | 200 | * */ |
|
| - | 201 | ||
| - | 202 | if ((timerVal < IR_RC5_ST_BIT+500) && (timerVal > IR_RC5_ST_BIT-500)) { |
|
| - | 203 | *proto = IR_PROTO_RC5; |
|
| - | 204 | return receiveRC5(&*address, &*command); |
|
| - | 205 | } //else if ... |
|
| - | 206 | ||
| - | 207 | ||
| - | 208 | return IR_NO_PROTOCOL; |
|
| - | 209 | } |
|
| - | 210 | ||
| - | 211 | ||
| - | 212 | int checkIrIdle(void) { |
|
| - | 213 | if (IRPIN & (1<<IRBIT)) return IR_NO_DATA; //om irmodulen ger en etta så återgå |
|
| - | 214 | ||
| - | 215 | //nu lägger irmodulen ut en nolla, "startbiten" alltså |
|
| - | 216 | ||
| 174 | uint16_t timerVal; |
217 | uint16_t timerVal; |
| 175 | 218 | ||
| 176 | *address = 0; |
- | |
| 177 | *command = 0; |
- | |
| 178 | - | ||
| 179 | //Läs in längden på startbiten |
219 | //Läs in längden på startbiten |
| 180 | initTimer(); |
220 | initTimer(); |
| 181 | while (!(IRPIN & (1<<IRBIT))) { //vänta på att irmodulen lägger ut en etta |
221 | while (!(IRPIN & (1<<IRBIT))) { //vänta på att irmodulen lägger ut en etta |
| 182 | //om timeout (om timer-ovflow-flaggan sätt) |
222 | //om timeout (om timer-ovflow-flaggan sätt) |
| 183 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
223 | if (isTimerOvfl() == 1) return IR_TIME_OVFL; |
| 184 | } |
224 | } |
| 185 | timerVal = getTimerVal(); |
225 | timerVal = getTimerVal(); |
| 186 | 226 | ||
| 187 | /* |
- | |
| 188 | * Anropa olika funktioner beroende på längden |
- | |
| 189 | * för vissa protokoll behöver längden på första biten skickas med till funktionen? |
- | |
| 190 | * t.ex. sharp? |
- | |
| 191 | * */ |
- | |
| 192 | - | ||
| 193 | if ((timerVal < |
227 | if ((timerVal < IR_MAX_REPEATE_PULSE_WIDTH) && (timerVal > IR_MIN_REPEATE_PULSE_WIDTH)) { |
| 194 |
|
228 | return IR_OK; |
| 195 | return receiveRC5(&*address, &*command); |
- | |
| 196 | } //else if ... |
229 | } //else if ... |
| 197 | 230 | ||
| 198 | 231 | ||
| 199 | return IR_NO_PROTOCOL; |
232 | return IR_NO_PROTOCOL; |
| 200 | } |
233 | } |