为什么这个程序用串口读出遥控器的按键值 同一个按键每按一次的数组都不一样?

2019-07-15 09:37发布

直接复制过来的。。注释看不了。。。。
#include<reg52.h>
typedef unsigned char uint8;
sbit IR=P3^3;
uint8 IR_buf[4];          //±£´æ½âÂë½á¹û
void uart_init()
{
        TMOD=0X21;
        SCON=0X50;          //´®ÐпڿØÖƼĴæλ
        TH1=0XFD;
        TL1=0XFD;
        TR1=1;
}

void uart_send(uint8 dat)
{
        SBUF=dat;          //½«datµÄÖµ¸³¸øSBUF´®¿Ú
        while(ti==0);      //
        TI=0;
}

unsigned int IR_get()
{
        TL0=0;
        TH0=0;
        TR0=1;
        while(!IR &&(TH0&0X80)==0); //!IRºÍ(TH0&0X80)½øÐа´Î»ÓëÔËËã¡£Ö»ÒªÓÐÒ»¸öÌõ¼þΪ0¾ÍÌø¹ýwhile È»ºó¶Áȡʱ¼ä
        TR0=0;        //¹Ø±Õ¶¨Ê±Æ÷
        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))           //µÍµçƽÂö³å8500-9500US
                                 goto start;
                         temp=IR_geth();
                         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();
                                         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]);
                 }
         }

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。