微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 求大侠相!获取低电平的子程序中while语句什么时候才能跳出

求大侠相!获取低电平的子程序中while语句什么时候才能跳出

时间:10-02 整理:3721RD 点击:
/*
* 红外接收数据,查询方式,并通过串口发送
*
* 晶振:11.0592M
*/
#include <reg52.h>
typedef unsigned char uint8;
sbit Ir_Pin = P3^3;
uint8 Ir_Buf[4]; //用于保存解码结果

/*
* UART初始化
* 波特率:9600
*/
void uart_init(void)
{
    TMOD = 0x21;
    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;
        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]);
        }
}

学习下,51单片机基础

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top