您现在的位置是:首页 > 单片机

遥控器程序

2020-01-15 02:15:10

遥控解码的原理:我们知道遥控是采用38KHZ的脉冲通过红外发光二极管调制发送出去的 。我们的主
目的是怎样将从空气中传送来的遥控信息,进行解码。对于遥控里面的最底层原理,我就不多介绍啦 , 网
友可以参考相关书籍。在这里简单的说一下遥控解码的注意事项:
     引导码的时间为:13.5ms,高电平时间为9ms,低电平时间为4.5ms。
     低电平时间周期为:用L表示低 L=1.125ms
     高电平时间周期为:用H表示高 H=2.25ms
     发送一串数据大约58.5---76.5ms
     遥控码是由:引导码、用户码、用户反码、键盘码、键盘反码 几部分组成。
    
相关源程序:(C语言版本)
            unsigned char th1_val = 0, th1_old = 0;

            unsigned char c_last=0;

            unsigned char c_bits = 8;

            unsigned char c_index = 0;

            unsigned char c_code ,c_last1,c_last2,c_last3=0;

            bit c_error = 0;

            #define T_100US (F_OSC/12/10000)

            #define T_ZERO_MIN (T_100US*10)

            #define T_ZERO_MAX (T_100US*18)

            #define T_ONE_MIN (T_100US*20)

            #define T_ONE_MAX (T_100US*28)

            #define T_REP_MIN (T_100US*110)

            #define T_REP_MAX (T_100US*120)

            #define T_START_MIN (T_100US*130)

            #define T_START_MAX (T_100US*150)

            #define TH_ZERO_MIN (T_ZERO_MIN>>8)

            #define TH_ZERO_MAX (T_ZERO_MAX>>8)

            #define TH_ONE_MIN (T_ONE_MIN>>8)

            #define TH_ONE_MAX (T_ONE_MAX>>8)

            #define TH_REP_MIN (T_REP_MIN>>8)

            #define TH_REP_MAX (T_REP_MAX>>8)

            #define TH_START_MIN (T_START_MIN>>8)

            #define TH_START_MAX (T_START_MAX>>8)

            #define CODE1 0x0a
            #define CODE2 0xf5

            #define INIT_T0 (65536L - (F_OSC/12)/HZ)
            #define INIT_TH0 (INIT_T0 >> 8)
            unsigned int remocnt=0;
            extern bit menu1flag;

            void int0_bh(void)
            {
            unsigned char temp = th1_val;
            th1_val -= th1_old;
            if (temp < th1_old)
                th1_val -= INIT_TH0;
                th1_old = temp;

            // if (th1_val > TH_START_MAX)
            // goto err_out;
            if (th1_val >= TH_START_MIN)
                goto reset_out;
            if (th1_val > TH_REP_MAX)
                goto err_out;
            if (th1_val >= TH_REP_MIN) {
            if (key_last == KEY_INC || key_last == KEY_DEC||key_last == KEY_MENU){
                key_on = 1;
                if(key_last==KEY_MENU) key_on=0;
                      if(++remocnt>40) { keymenu=1;
                           key_on=1;
                           remocnt=0;
                }
                else keymenu=0;
                  if(menu1flag) key_on=0;
              }
              goto reset_out;
            }
            if (c_error)
                return;
            if (th1_val > TH_ONE_MAX)
                goto err_out;
            if (th1_val >= TH_ONE_MIN) {
                c_last >>= 1;
                c_last |= 0x80;
                goto bit_rcv;
            }
            if (th1_val > TH_ZERO_MAX)
                goto err_out;
            if (th1_val >= TH_ZERO_MIN) {
                c_last >>= 1;
                goto bit_rcv;
            }
                goto err_out;
            bit_rcv:
            if (--c_bits)
                return;
              //c_index++;
            switch (c_index++) {
            case 0:
                 { if (c_last == CODE1)
                       goto out;
                   else { goto out;}
                 }
            case 1:
                 { if (c_last == CODE2)
                       goto out;
                       // c_last2=c_last;
                   else {goto out;}
                 }
            case 2: {
                     c_code = c_last;
                     goto out;
                    }
            case 3: {
                      if (c_last != ~c_code)
                         goto err_out;
                         // c_last3=c_last;
                         break;
                      }
                    }

            if (key_on) {
                     goto reset_out;
                       }
            switch (c_code) {
            case 0x12:
                     key_last = KEY_POWER;
                     break;
            case 0x03:
                     {
                     key_last = KEY_MENU;
                     keymenu=0;
                     remocnt=0;
                     }
                     break;
            case 0x06:
                     key_last = KEY_VIDEO;
                     break;
            case 0x13:
                     key_last = KEY_INC;
                     break;
            case 0x48:
                     key_last = KEY_DEC;
                     break;
            case 0x09:
                     key_last = KEY_ZOOM;
                     break;
            default:
                     key_last = KEY_INVALID;
                     goto reset_out;
            }       
                     key_on = 1;
                     goto reset_out;
            err_out:
                     c_error = 1;
                     goto out;
            reset_out:
                     c_error = 0;
                     c_index = 0;
                     goto out;
           out:
                     c_last = 0;
                     c_bits = 8;
          }

上一篇:单片机简谱程序

下一篇:串口调试程序