#include
typedef unsigned int uint;
typedefunsigned char uchar;
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit IRINPUT=P3^2;
uchar code Display_LINE0[]={"VALUE FROM IR:"};
ucharDisplay_LINE1[]={"IR CODE:H"};
uchar IR_Input_Buffer[8];
uchar Display_Buffer[3]={0x00,0x00};//数码管显示缓冲
void _delay_us(uint x)
{
uchar i;
while(x--)
for(i=0;i<13;i++);
}void _delay_ms(unsigned int x)
{
unsigned char i;
while(x--)
{
for(i=0;i<120;i++);
}
}
uchar LCD_Busy(void)//测忙
{
uchar LCD_Status;//返回值变量
LCD_RS=0;//读取状态
LCD_RW=1;
LCD_EN=1;
_nop_();_nop_();_nop_();_nop_();
LCD_Status=P0;
LCD_EN=0;
return LCD_Status;
}
void LCD_Write_Command(uchar cmd)//写指令
{
while((LCD_Busy()&0x80)==0x80);//测忙
LCD_RS=0;//
LCD_RW=0;
LCD_EN=0;
P0=cmd;
LCD_EN=1;
_nop_();_nop_();
LCD_EN=0;
_nop_();_nop_();
}
void LCD_Write_Data(uchar dat)//写数据
{
while((LCD_Busy()&0x80)==0x80);
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
LCD_EN=1;
_nop_();_nop_();
LCD_EN=0;
_nop_();_nop_();
}
void Init_LCD(void)//液晶初始化
{
_delay_ms(15);
LCD_Write_Command(0x38);
_delay_ms(5);
LCD_Write_Command(0x06);
_delay_ms(5);
LCD_Write_Command(0x0c);
_delay_ms(5);
LCD_Write_Command(0x01);
_delay_ms(5);
}
void LCD_POS(uchar pos)//字符显示位置
{
LCD_Write_Command(0x80|pos);
}
void Show_String(uchar *str)//显示字符串
{
while(*str!=)
LCD_Write_Data(*str++);
}
void IR_INT0(void)
{
EX0=1;
IT0=1;
EA=1;
}
void main(void)
{
IR_INT0();
IRINPUT=1;
Init_LCD();while(1)
{
LCD_POS(0);
Show_String(Display_LINE0);
LCD_POS(0x40);
Show_String(Display_LINE1);
}
}
void IR_Input_INT() interrupt 0 using 0
{
uchar i,j,k,N=0;
EX0=0;//INT0 off
_delay_us(15);//delay 2ms
for(i=0;i<14;i++)
{
if(IRINPUT)
{
EX0=1;
return;
}
}
while(!IRINPUT)
_delay_us(1);
for(j=0;j<4;j++)
{
for(k=0;k<8;k++)
{
while(IRINPUT)
_delay_us(1);
while(!IRINPUT)
_delay_us(1);
while(IRINPUT)
{
_delay_us(1);//delay 0.14ms
N++;
if(N>=30)
{
EX0=1;
return;
}
}
IR_Input_Buffer[j]=IR_Input_Buffer[j]>>1;
if(N>=8)
IR_Input_Buffer[j]=IR_Input_Buffer[j]|0x80;
N=0;
}
}
if(IR_Input_Buffer[2]!=~IR_Input_Buffer[3])
{
EX0=1;
return;
}
Display_Buffer[0]=IR_Input_Buffer[2]&0x0f;
Display_Buffer[1]=IR_Input_Buffer[2]>>4;
if(Display_Buffer[0]>9)
Display_Buffer[0]=Display_Buffer[0]+0x37;
else
Display_Buffer[0]=Display_Buffer[0]+0x30;
if(Display_Buffer[1]>9)
Display_Buffer[1]=Display_Buffer[1]+0x37;
else
Display_Buffer[1]=Display_Buffer[1]+0x30;
Display_LINE1[11]=Display_Buffer[0];//低4位显示在LCD1602的第11列*/
Display_LINE1[10]=Display_Buffer[1];//高4位显示在LCD1602的第10列*/
EX0=1;
}