为什么这个程序用串口读出遥控器的按键值 同一个按键每按一次的数组都不一样?
时间:10-02
整理:3721RD
点击:
直接复制过来的。注释看不了。
#include<reg52.h>
typedef unsigned char uint8;
sbit IR=P3^3;
uint8 IR_buf[4]; //±£′??a???á1?
void uart_init()
{
TMOD=0X21;
SCON=0X50; //′?DD?ú??????′???
TH1=0XFD;
TL1=0XFD;
TR1=1;
}
void uart_send(uint8 dat)
{
SBUF=dat; //??datμ??μ?3??SBUF′??ú
while(TI==0); //
TI=0;
}
unsigned int IR_get()
{
TL0=0;
TH0=0;
TR0=1;
while(!IR &&(TH0&0X80)==0); //!IRoí(TH0&0X80)??DD°′??ó??????£??òaóDò???ì??t?a0?íì?1ywhile è?oó?áè?ê±??
TR0=0; //1?±??¨ê±?÷
return (TH0*256+TH0); //·μ??ê±???μ
}
unsigned int IR_geth()
{
TL0=0;
TH0=0;
TR0=1;
while(IR&&(TH0&0X80)==0);
TR0=0;
return(TH0*256+TH0);
}
void main ()
{
unsigned int temp;
char i,j;
uart_init();
while(1)
{
start:
while(IR);
temp=IR_get();
if((temp<7833)||(temp>8755)) //μíμ?????3?8500-9500US
goto start;
temp=IR_geth();
if((temp<3686) || (temp>4608)) //??μ?????3?4000-5000US
goto start;
for(i=0;i<4;i++) //4??×??ú
{
for(j=0;j<8;j++) //????×??ú8??
{
temp=IR_get();
if((temp<184)||(temp>737)) //200-800us
goto start;
temp=IR_geth();
if((temp<184)||(temp>1843)) //200-2000us
goto start;
IR_buf[i]>>=1;
if(temp>1302)
IR_buf[i]|=0x80;
}
}
uart_send(IR_buf[0]);
uart_send(IR_buf[1]);
uart_send(IR_buf[2]);
uart_send(IR_buf[3]);
}
}
#include<reg52.h>
typedef unsigned char uint8;
sbit IR=P3^3;
uint8 IR_buf[4]; //±£′??a???á1?
void uart_init()
{
TMOD=0X21;
SCON=0X50; //′?DD?ú??????′???
TH1=0XFD;
TL1=0XFD;
TR1=1;
}
void uart_send(uint8 dat)
{
SBUF=dat; //??datμ??μ?3??SBUF′??ú
while(TI==0); //
TI=0;
}
unsigned int IR_get()
{
TL0=0;
TH0=0;
TR0=1;
while(!IR &&(TH0&0X80)==0); //!IRoí(TH0&0X80)??DD°′??ó??????£??òaóDò???ì??t?a0?íì?1ywhile è?oó?áè?ê±??
TR0=0; //1?±??¨ê±?÷
return (TH0*256+TH0); //·μ??ê±???μ
}
unsigned int IR_geth()
{
TL0=0;
TH0=0;
TR0=1;
while(IR&&(TH0&0X80)==0);
TR0=0;
return(TH0*256+TH0);
}
void main ()
{
unsigned int temp;
char i,j;
uart_init();
while(1)
{
start:
while(IR);
temp=IR_get();
if((temp<7833)||(temp>8755)) //μíμ?????3?8500-9500US
goto start;
temp=IR_geth();
if((temp<3686) || (temp>4608)) //??μ?????3?4000-5000US
goto start;
for(i=0;i<4;i++) //4??×??ú
{
for(j=0;j<8;j++) //????×??ú8??
{
temp=IR_get();
if((temp<184)||(temp>737)) //200-800us
goto start;
temp=IR_geth();
if((temp<184)||(temp>1843)) //200-2000us
goto start;
IR_buf[i]>>=1;
if(temp>1302)
IR_buf[i]|=0x80;
}
}
uart_send(IR_buf[0]);
uart_send(IR_buf[1]);
uart_send(IR_buf[2]);
uart_send(IR_buf[3]);
}
}
通讯协议呢?
通讯协议?我不是很懂
这个程序中应该有对应遥控器过来信息的解析,之后你再通过串口读出来!