keil c51红外遥控解码程序
2012-12-19
大炮
标签: C51 红外

本keil c51程序适用uPC1621/uPC1622及兼容的红外遥控器芯片,占用外部中断0和定时器1,以中断方式解码,节省系统资源,以查询方式检测遥控信号是否有效.

解码思路:

红外线经一体化接受头解码放到后送到单片机的外部中断0,单片机设置外部中断下降沿触发,T0和T1为16位定时器,T0在系统启动后定时5ms.T1在外部中断0启动后开始定时,初值为0,每次在INT0中断后先读T1计数值,并重设初值为0,而且判断T1的计数值,

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//Fosc=11.0592MHz
// states for and variables IR data processing ;
typedef enum {
    IR_idle,
    IR_waitstart,
    IR_getaddr,
    IR_getaddrinv,
    IR_getdata,
    IR_getdatainv
} _IRstate;
_IRstate IRstate = IR_idle;
unsigned char IRaddr = 0xff;
unsigned char _IRaddr = 0xff;
unsigned char IRdata = 0xff;
unsigned char _IRdata = 0xff;
unsigned char IR_repeat = 0;
unsigned char IR_ready = 0;
unsigned charIR_poweron = 0;
//bit ir_done=0;
// time constants
unsigned int IRtimer = 0; // IR timeout
//cpu初始化
void cpu_init(void)
{
    TMOD = 0X11; // T0 and T1 十六位定时
    TH0 = 0xee; //fosc=11.0592M,timer=5ms
    TL0 = 0x00;
    TR0 = 1; // run timer 0;
    TF0 = 0;
    ET0 = 1; // enable tmr 0 overflow interrupt
    IT0 = 1; // int0 edge sensitive
    EX0 = 1; //enable "int0"
    EA = 1; // global interupt enable
}
 
//T0中断
void tmrint() interrupt 1
{
    TH0 = 0xee;
    TL0 = 0x00;
    if (IRtimer) { //IR接收超时
        --IRtimer;    //
    }
    else {
        IRstate = IR_idle;
//IR_poweron=0;
    }
}
 
//Fosc=11.0592MHz
#define msec_12p50x2d00
#define msec_150x3600
#define msec_90x2066
//#define msec_90x1066
#define msec_2p50x900
#define msec_0p90x33d
#define msec_1p680x610
//void IRint() interrupt 0(void)
//When the IR receive pin goes low and interrupt is generated
// IR is collected by starting timer 2 in the first falling edge of the pin
// then on every other falling edge, the timer value is saved and the timer restarted .
// the captured time is then used to get the IR data
// a "start of data" is 13.5Msec,a "1" is 2.25Msec,a "0" is 1.12 msec and a "repeat" is 11.25msec.
// the counter increments at 1.085 Usec
// I allow a fairly large tolerance to time jitter but there are no false triggers seen.
void IRint() interrupt 0
{
    static unsigned char bits;
    unsigned short time;
    switch (IRstate) {
    case IR_idle:
        TL1 = 0;
        TH1 = 0;
        TR1 = 1;
        IRstate = IR_waitstart;
        IRtimer = 26;
        break;
    case IR_waitstart: //P2_4=!P2_4;
        TR1 = 0;
        time = TH1;
        time = (time << 8) + TL1;;
        TL1 = 0;
        TH1 = 0;
        TR1 = 1;
        if ((time > msec_12p5) && (time < msec_15)) { // greater than 12.5Msec & less than 15 msec = start code
            IRaddr = 0;
            _IRaddr = 0;
            IRdata = 0;
            _IRdata = 0;
            bits = 1;
            IRstate = IR_getaddr;
        }
        else if ((time > msec_9) && (time {
        IR_repeat = 2;
        IRstate = IR_idle;
    }
    else {
        // to short, bad data just go to idle
        IRstate = IR_idle;
    }
    break;
case IR_getaddr:// P2_4=!P2_4;
        TR1 = 0;
              time = TH1;
              time = (time << 8) + TL1;;
 
 
              TL1 = 0;
 
 
              TH1 = 0;
 
 
              TR1 = 1;
 
 
              if ((time > msec_2p5) || (time
 
 
        {
 
 
            IRstate = IR_idle;
 
 
            break;
 
 
        }
 
 
    if (time > msec_1p68) // greater than 1.68Msec is a 1
 
 
    {
 
 
        IRaddr |= bits;
 
 
    }
 
 
    bits = bits << 1;
 
 
           if (!bits)
 
 
    {
 
 
        IRstate = IR_getaddrinv;
 
 
        bits = 1;
 
 
    }
 
 
    break;
 
 
case IR_getaddrinv://P2_4=!P2_4;
 
 
        TR1 = 0;
 
 
              time = TH1;
 
 
              time = (time << 8) + TL1;;
 
 
              TL1 = 0;
 
 
              TH1 = 0;
 
 
              TR1 = 1;
 
 
              if ((time > msec_2p5) || (time
 
 
        {
 
 
            IRstate = IR_idle;
 
 
            break;
 
 
        }
 
 
    if (time > msec_1p68) // greater than 1.68Msec is a 1
 
 
    {
 
 
        _IRaddr |= bits;
 
 
    }
 
 
    bits = bits << 1;
 
 
           if (!bits)
 
 
    {
 
 
        IRstate = IR_getdata;;
 
 
        bits = 1;
 
 
    }
 
 
    break;
 
 
case IR_getdata:
 
 
        TR1 = 0;
 
 
              time = TH1;
 
 
              time = (time << 8) + TL1;;
 
 
              TL1 = 0;
 
 
              TH1 = 0;
 
 
              TR1 = 1;
 
 
              if ((time > msec_2p5) || (time
 
 
        {
 
 
            IRstate = IR_idle;
 
 
            break;
 
 
        }
 
 
    if (time > msec_1p68) // greater than 1.68Msec is a 1
 
 
    {
 
 
        IRdata |= bits;
 
 
    }
 
 
    bits = bits << 1;
 
 
           if (!bits)
 
 
    {
 
 
        IRstate = IR_getdatainv;
 
 
        bits = 1;
 
 
    }
 
 
    break;
 
 
case IR_getdatainv:
 
 
        TR1 = 0;
 
 
              time = TH1;
 
 
              time = (time << 8) + TL1;;
 
 
              TL1 = 0;
 
 
              TH1 = 0;
 
 
              TR1 = 1;
 
 
              if ((time > msec_2p5) || (time
 
 
        {
 
 
            IRstate = IR_idle;
 
 
            break;
 
 
        }
 
 
    if (time > msec_1p68) // greater than 1.68Msec is a 1
 
 
    {
 
 
        _IRdata |= bits;
 
 
    }
 
 
    bits = bits << 1;
 
 
           if (!bits)// we have it all , now we make sure it is a NEC code from the CHS IR transmitter
 
 
    {
        // make sure address,~address are correct , data ,~data are correct and address is 0.
 
 
        IR_ready = ((IRaddr ^ _IRaddr) == 0xff) && ((IRdata ^ _IRdata) == 0xff) && (IRaddr == 0);
 
 
            if (IR_ready)
 
 
            {
 
 
                IRstate = IR_idle;
 
 
            }
 
 
        }
 
 
        break;
 
 
    default:
 
 
            IRstate = IR_idle;
 
 
                      break;
 
 
        }
 
 
}
 
 
 
 
 
void main(void)
 
 
{
 
 
    cpu_init();
 
 
    while (1)
 
 
    {
 
 
        if (IR_ready)
 
 
        {
 
 
            IR_ready = 0;
 
 
            switch (IRdata)
 
 
            {
 
 
            case 0x45://1
 
 
//your code
 
 
                break;
 
 
            case 0x44://3
 
 
//your code
 
 
                break;
 
 
            case 0x43://4
 
 
//your code
 
 
                break;
 
 
            case 0x08://prev
 
 
//your code
 
 
                break;
 
 
            case 0x5a://next
 
 
//your code
 
 
                break;
 
 
            default:
 
 
                break;
 
 
            }
 
 
        }
 
 
    }
 
 
}




可能会用到的工具/仪表
本站简介 | 意见建议 | 免责声明 | 版权声明 | 联系我们
CopyRight@2024-2039 嵌入式资源网
蜀ICP备2021025729号