打印

红外控制电机正反停转问题

[复制链接]
1536|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
q1074926748|  楼主 | 2012-5-21 20:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg51.h>
typedef unsigned char uint8;
sbit Ir_Pin =P3^2;
uint8 Ir_Buf[4];
code uint8 a[]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6};//反转
code uint8 b[]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe};//正转

void uart_init(void)
{
    TMOD = 0x01;
   /* 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  ==0);  
              
        TR0 = 0;           
        return (TH0 * 256 + TL0);
}

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

        TR0 = 0;
        return (TH0 * 256 + TL0);
}
  void delay(unsigned int t)
{                           
   unsigned int k;
   while(t--)
   {
     for(k=0; k<80; k++)
     { }
   }
}

main()
{
        unsigned int temp;
        char i,j;

        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 < 276) || (temp > 737)) //200~800us
                                        goto start;

                                temp = Ir_Get_High();
                                if ((temp < 276) || (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]==0x16)//遥控器0键
                {
                   for(i=0;i<8;i++)
                   {
                     P1=a[i];
                         if(i==7)
                         {
                          i=0;
                         }
                         delay(5);
                         if(Ir_Buf[2]==0x08)遥控器4键
                         break;
                   }
                }
                if(Ir_Buf[2]==0x0c)遥控器1键
                {
                  for(i=0;i<8;i++)
                  {
                    P1=b[i];
                        if(i==7)
                        {
                          i=0;
                        }
                        delay(5);
                        if(Ir_Buf[2]==0x08)
                        break;
                  }
                }
                if(Ir_Buf[2]==0x08)
                {
                  P1=0xff;
                }
        }               
}

红外解码都没问题,我验证过了,但是比如拿下0键,正转,但是再按其他键没反应,仍然是正转,如果按下1键,反转,但是再按下其他键还是没反应,继续反转,这个和一起论坛的帖子用按键控制步进电机差不多,但是那个加入一个判断break就能实现,这个不知道为什么,求教!

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:郭强

0

主题

58

帖子

1

粉丝