红外接收显示二进制

来源:互联网 发布:linux查看当前文件大小 编辑:程序博客网 时间:2024/05/16 02:14
#include<reg52.h>#include"./uart/uart.h"sbit IR_IN = P3^2;sbit LED0 = P2^0;sbit LED1 = P2^1;sbit LED2 = P2^2;sbit LED3 = P2^3;sbit LED4 = P2^4;sbit LED5 = P2^5;sbit LED6 = P2^6;sbit LED7 = P2^7;unsigned int irtime;bit irok = 0;bit startflag = 0;unsigned char ircode[4];bit irhandleok = 0;unsigned char putbuf[35];unsigned char irdata[33];unsigned char bitnum = 0;void int0_init(){    IT0 = 1; //设置外部中断的触发方式    EA = 1;    EX0 = 1;}void timer0_init(){    EA = 1;    TMOD |= 0x02;    TH0 = 0;    ET0 = 1;    TR0 = 1;}void timer0_isr() interrupt 1{   irtime++;//0.256ms  引导码 13.5/0.256 = 52      1.12/0.256 = 4   0.56+1.685/0.256 = 8}void int0_isr() interrupt 0{    if(startflag)    {        if(irtime>=40&&irtime<=60)        {            bitnum = 0;        }         irdata[bitnum] = irtime;        bitnum++;        irtime = 0;        if(bitnum == 33)        {            irok = 1;            bitnum = 0;                 startflag = 0;        }    }    else    {        irtime = 0;        startflag = 1;    }}void data_handle(){    unsigned char i,j,k = 1;    unsigned char temp = 0;    if(irok)    {        for(i = 0; i < 4; i++)        {            for(j = 0; j < 8; j++)            {                temp >>= 1;                if(irdata[k] > 6)                {                    temp += 0x80;                }                k++;            }            ircode[i] = temp;        }        irok = 0;                        }    if((ircode[0] + ircode[1] == 0xff) && (ircode[2] + ircode[3] == 0xff))    {        irhandleok = 1;    }}void main(){    timer0_init();    int0_init();    uart_init();    while(1)    {        if(irok)        {            data_handle();            if(irhandleok)            {                switch(ircode[2])                {                       case 0x45:LED0 = ~LED0;break;                    case 0x46:LED1 = ~LED1;break;                    case 0x47:LED2 = ~LED2;break;                    case 0x44:LED3 = ~LED3;break;                    case 0x40:LED4 = ~LED4;break;                    case 0x43:LED5 = ~LED5;break;                    case 0x7:LED6 = ~LED6;break;                    case 0x15:LED7 = ~LED7;break;                    default:break;                }            }             putbuf[0] = (ircode[0] / 16) > 9 ? (ircode[0] / 16 ) + 0x37 : (ircode[0] /16) + 0x30;            putbuf[1] = (ircode[0] % 16) > 9 ? (ircode[0] % 16 )+ 0x37 : (ircode[0] %16 )+ 0x30;            putbuf[2] = (ircode[1] / 16) > 9 ? (ircode[1] / 16 )+ 0x37 : (ircode[1] /16) + 0x30;            putbuf[3] = (ircode[1] % 16) > 9 ? (ircode[1] % 16 )+ 0x37 : (ircode[1] %16 )+ 0x30;            putbuf[4] = (ircode[2] / 16) > 9 ? (ircode[2] / 16 )+ 0x37 : (ircode[2] /16 )+ 0x30;            putbuf[5] = (ircode[2] % 16) > 9 ? (ircode[2] % 16 )+ 0x37 : (ircode[2] %16 )+ 0x30;            putbuf[6] = (ircode[3] / 16) > 9 ? (ircode[3] / 16 )+ 0x37 : (ircode[3] /16) + 0x30;            putbuf[7] = (ircode[3] % 16) > 9 ? (ircode[3] % 16 )+ 0x37 : (ircode[3] %16) + 0x30;            putbuf[8] = '\r';            putbuf[9] = '\n';            uart_send_string(putbuf);            irhandleok = 0;              }          irok = 0;    }                           }/*void main(){    timer0_init();    int0_init();    uart_init();    while(1)    {        if(irok == 1)        {            Ircordpro();            irok = 0;        }        if(irpro_ok == 1)        {              uart_send_string(buf);              uart_send_byte('\n');              irpro_ok = 0;        }    }} */
0 0