• <table id="4yyaw"><kbd id="4yyaw"></kbd></table>
  • <td id="4yyaw"></td>
  • 發布時間:2020-10-05 15:39 原文鏈接: 一文讀懂紅外遙控解碼(二)

    實現代碼:

    #include <reg52.h>

    // --- 紅外接收一體化輸出口 ----------------------------------

    sbit IR_Out = P3^2;

    bit START_Flag = 0;

    bit BOOT_REPEATING_CODE_Flag = 0;

    unsigned char DATA[4] = {0};

    bdata unsigned char TEMP_BIT;

    sbit B0 = TEMP_BIT^0;

    sbit B1 = TEMP_BIT^1;

    sbit B2 = TEMP_BIT^2;

    sbit B3 = TEMP_BIT^3;

    sbit B4 = TEMP_BIT^4;

    sbit B5 = TEMP_BIT^5;

    sbit B6 = TEMP_BIT^6;

    sbit B7 = TEMP_BIT^7;

    // --- 有無遙控信號判斷函數 ----------------------------------

    bit START_Judge();

    // --- 連發碼判斷函數 ----------------------------------------

    bit BOOT_REPEATING_CODE_Judge();

    // --- "0"和"1"識別 ------------------------------------------

    bit H_L_LEVEL_Judge();

    bit START_Judge()

    {

    bit TEMP_Flag = 1;

    unsigned char i = 0;

    //在正常無遙控信號時,一體化紅外接收頭輸出是高電平,程序一直在循環。

    while ( IR_Out == 1);

    //重復10次,目的是檢測在6876~8352微秒內如果出現高電平就退出解碼程序

    for(i =0;i <9; i++)

    {

    DELAY_Us(800);      // 測試實際延時約為764~928us

    if ( IR_Out == 1 )

    {

    TEMP_Flag = 0;

    break;

    }

    }

    return TEMP_Flag;

    }

    bit BOOT_REPEATING_CODE_Judge()

    {

    bit TEMP_Flag = 1;

    while( IR_Out == 0 ) ;  // 等待高電平避開9毫秒低電平引導脈沖

    DELAY_Ms(1);            // 測試實際延時約為1.007ms

    DELAY_Ms(1);            // 測試實際延時約為1.007ms

    DELAY_Us(200);          // 0.086ms

    DELAY_Us(200);          // 0.086ms

    DELAY_Us(200);          // 0.086ms

    // 共計2.272ms

    if( IR_Out == 0 )

    {

    TEMP_Flag = 1;      // 是連發碼

    }

    else

    {

    TEMP_Flag = 0;      // 不是連發碼,而是引導碼

    }

    return TEMP_Flag;

    }

    bit H_L_LEVEL_Judge()

    {

    while( IR_Out == 0 );   // 等待地址碼第一位的高電平信號

    DELAY_Us(800);          // 測試實際延時約為764~928us

    if ( IR_Out == 1)

    {

    DELAY_Ms(1);        // 測試實際延時約為1.007ms

    return 1;

    }

    else

    {

    return 0;

    }

    // --- 串口初始化 --------------------------------------------

    void UART_Initial();

    void DELAY_Us(unsigned int Us)

    {

    unsigned int x;

    for(x = 0; x <= (Us/200-1); x++);

    }

    void DELAY_Ms(unsigned int Ms)

    {

    unsigned int x,y;

    for(x = 0; x <= (Ms-1); x++)

    {

    for(y = 0; y <= 120; y++);

    }

    }

    void main()

    {

    unsigned char i;

    UART_Initial();

    IR_Out = 1;

    while(1)

    {

    START_Flag = START_Judge();

    BOOT_REPEATING_CODE_Flag = BOOT_REPEATING_CODE_Judge();

    if ( START_Flag && !BOOT_REPEATING_CODE_Flag )

    {

    for(i =0;i <4; i++)

    {

    B0 = H_L_LEVEL_Judge();

    B1 = H_L_LEVEL_Judge();

    B2 = H_L_LEVEL_Judge();

    B3 = H_L_LEVEL_Judge();

    B4 = H_L_LEVEL_Judge();

    B5 = H_L_LEVEL_Judge();

    B6 = H_L_LEVEL_Judge();

    B7 = H_L_LEVEL_Judge();

    DATA[i] = TEMP_BIT;

    }

    for(i =0;i <4; i++)

    {

    SBUF = DATA[i];

    while( TI == 0 );

    TI = 0;

    }

    }

    }

    }

    void UART_Initial()

    {

    SCON = 0x50;            // SCON: 模式 1, 8-bit UART, 使能接收

    TMOD |= 0x20;           // TMOD: timer 1, mode 2, 8-bit reload

    TH1 = 0xFD;             // TH1: reload value for 9600 baud @

    // 11.0592MHz

    TR1 = 1;                // TR1: timer 1 run

    EA = 0;                 // 關閉總中斷

    ES = 0;                 // 關閉串口中斷

    }

    }

    注

    1、紅外遙控的編碼不只NEC,還有很多也有廣泛應用如RC-5,RC-6等。

    2、上面的代碼所用指令是STC 89C52單片機,如需用其它芯片請另行更改。


  • <table id="4yyaw"><kbd id="4yyaw"></kbd></table>
  • <td id="4yyaw"></td>
  • 调性视频