H1838红外接收头

/*

* 红外接收数据,查询方式,并通过串口发送

 
 微笑 
 *,利用遥控器的左键和右键控制led的亮灭,并且在LCD上显示LED亮灯的数量 
 

* 晶振:11.0592M

采用了 go to语句 

*/
#include <reg52.h>
typedef unsigned char uint8;
sbit RS = P1^0; 
sbit RW = P1^1;
sbit EN = P1^2;


sbit SP_1=P1^5; 
sbit BUSY = P0^7;
sbit Ir_Pin = P3^3;
sbit jd=P1^4;
uint8 Ir_Buf[4]; //用于保存解码结果
char str1[]="!close!";
char str2[]="!open!";
char str3[1];
 int G=0;
/*
 * UART初始化
 * 波特率:9600
*/
void wait() //等待
{   P0 = 0XFF;
do {EN=0;
RS=0;
RW=1;
EN=1; 
} 
  while(BUSY==1);
   EN=0;
}
void write_cmd(uint8 cmd)  //写入命令
{  wait(); //查询是不是忙碌
   EN=0;  
  RS=0;
  RW=0;        //写操作 
  P0=cmd;   //输入命令
  EN=1;
  EN=0;

}


void write_data(uint8 dat)   //写入数据
{ wait();   //查询是不是忙碌
  EN=0;
  RS=1;
  RW=0;
  P0=dat;  //写入
  EN=1;
  EN=0;
}
void lcd_init()   //初始化
{
  write_cmd(0x38);   //设置工作方式
  write_cmd(0x0c);  //设置光标
  write_cmd(0x06); //设置输入方式
  write_cmd(0x01); //清屏
}
void w_str(uint8 addr,char *p) //起始地址,和字符
{
   write_cmd(addr);//写入起始地址
 
   while(*p!='\0')//如果*p的值不等于\0那么就把数据写入po
  {
    write_data(*p++);
  }
}
void uart_init(void)
{
    TMOD = 0x21;//0010 0001 
    SCON = 0x50; 


    TH1 = 0xFD;
    TL1 = 0xFD;


    TR1 = 1;
}


/*
 * UART发送一字节
*/
void UART_Send_Byte(uint8 dat)
{
  SBUF = dat;
  while (TI == 0);
  TI = 0;
}




/*
 * 获取低电平时间
*/
unsigned int Ir_Get_Low()
{
  TL0 = 0;
  TH0 = 0;
  TR0 = 1;
  while (!Ir_Pin&&(TH0&0x80)==0 ); 
  TR0 = 0;           
  return (TH0 * 256 + TL0);
}


/*
 * 获取高电平时间
*/
unsigned int Ir_Get_High()
{
  TL0 = 0;
  TH0 = 0;
  TR0 = 1;
  while (Ir_Pin && (TH0&0x80)==0);


  TR0 = 0;
  return (TH0 * 256 + TL0);
}




main()
{
  unsigned int temp;
  char i,j;
  int flag=0;int G=0;
  lcd_init();
  uart_init();

while (1)
{
  start:
  while (Ir_Pin);


  temp = Ir_Get_Low();
  if ((temp < 7833) || (temp > 8755))  //引导脉冲低电平8500~9500us
  goto start;
  temp = Ir_Get_High();
  if ((temp < 3686) || (temp > 4608))  //引导脉冲高电平4000~5000us
  goto start;


  for (i=0; i<4; i++) //4个字节
  {
    for (j=0; j<8; j++) //每个字节8位
  {
  temp = Ir_Get_Low();
  if ((temp < 184) || (temp > 737)) //200~800us
  goto start;


  temp = Ir_Get_High();
  if ((temp < 184) || (temp > 1843)) //200~2000us
  goto start;


  Ir_Buf[i] >>= 1;
  if (temp > 1032) //1120us
  Ir_Buf[i] |= 0x80;
}
}


UART_Send_Byte(Ir_Buf[0]);
UART_Send_Byte(Ir_Buf[1]);
UART_Send_Byte(Ir_Buf[2]);
UART_Send_Byte(Ir_Buf[3]);
if(Ir_Buf[2]==0x09)
{ 
jd=flag;
flag=~flag;
if(flag==0)
{
write_cmd(0x01);
  w_str(0X80,str1);
}
else
{
write_cmd(0x01);
w_str(0X80,str2);
}
}
if(Ir_Buf[2]==0x08)
{
if(G>=8)
{
G=8;

}
P2=0x7f>>++G;
str3[0]=G+'0';
write_cmd(0x01);
w_str(0X80,str3);
}
if(Ir_Buf[2]==0x0A)
{
if(G<=0)
{
G=0;

}
P2=(0xff>>--G);
str3[0]=G+'0';
write_cmd(0x01);
w_str(0X80,str3);
}

}
}


猜你喜欢

转载自blog.csdn.net/qq1294272813/article/details/53667794
今日推荐