#include <reg52.h>
#include "test1.h"
#include "./delay/delay.h"
#include "./1602/1602.h"
sbit led0 = P1^0;
sbit led1 = P1^1;
sbit led2 = P1^2;
sbit led3 = P1^3;
sbit led4 = P1^4;
sbit led5 = P1^5;
sbit led6 = P1^6;
sbit led7 = P1^7;
unsigned int irtime;
bit startflag = 0;
bit irok = 0;
bit irhandle = 0;
void time0_init()
{
EA = 1;
TMOD |= 0x02;
TH0 = 0;
ET0 = 1;
TR0 = 1;
}
void time0_isr() interrupt 1
{
irtime++; // 0.256ms 引导码 S:53 0码:4 1码:9
}
void int0_init()
{
IT0 = 1; // 设置外部中断的触发方式
EA= 1;
EX0 = 1;
}
unsigned char irdata[33];
unsigned char bitnum = 0;
void int0_isr() interrupt 0
{
if(startflag) //判断下降沿开始计时
{
if((irtime > 40) &&(irtime < 60)) //引导码判断
{
bitnum = 0;
}
irdata[bitnum] = irtime; //客户码1,2,操作码,反码放入缓冲区
bitnum++;
irtime = 0;
if(33 == bitnum) //32位数据
{
bitnum = 0;
irok = 1;
startflag = 0;
}
}
else //检测到信号开始,清零判断引导码
{
irtime = 0;
startflag = 1;
}
}
unsigned char ircode[4];
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;
//else temp + = 0x0x;
k++;
}
ircode[i] = temp;
}
irok = 0;
}
if((ircode[0] + ircode[1] == 0xff) && (ircode[2]+ircode[3])) //
irhandle = 1;
}
void main()
{
unsigned char putbuf[35];
time0_init();
int0_init();
uart_init();
LCD_Init();
while(1)
{
if(irok)
{
data_handle();
if(irhandle)
{
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 0x07 : led6 = ~led6;break;
case 0x15 : led7 = ~led7;break;
default :break;
}
switch(ircode[2])
{
case 0x45 : LCD_Write_String(0,0,'1');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 0x07 : 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);
irhandle =0; */
}
irok = 0;
}
/* if(irok == 1 )
{ //'0' 0x30; '1' 0x31;
//putbuf[0] = irdata[0] / 10; //+ 0x30;
//putbuf[1] = irdata[0] % 10;// + 0x30;
putbuf[0] = 's';
putbuf[1] = 't';
for(i=0 ;i< 32; i++)
{
putbuf[i+2] = irdata[i+1];// + 0x30;
}
putbuf[35] = '\0' ;
for(j = 2; j<34; j++)
{
if(putbuf[k]>6)
{
putbuf[k] = 1 + 0x30;
}
else
{
putbuf[k] = 0 +0x30;
}
}
uart_send_string(putbuf);
irok = 0;
} */
}
}