<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR單片機(jī)紅外解碼程序

          AVR單片機(jī)紅外解碼程序

          作者: 時(shí)間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
          在網(wǎng)絡(luò)上基本上沒有avr的紅外遙控解碼函數(shù),有的也不是ICC的。為了大家的需要,我特地把自己項(xiàng)目需要的紅外解碼函數(shù)發(fā)上博客,希望對(duì)需要的人有所幫助。本解碼方法是偉納電子51單片機(jī)的解碼方法,算是移植吧,應(yīng)用的是掃描方式,需要的可以加上外中斷,用中斷方式反應(yīng)會(huì)更快,更實(shí)時(shí)。本解碼程序的關(guān)鍵在于延時(shí)函數(shù),如果要移植到別的單片機(jī)或別的晶振時(shí),一定要把延時(shí)調(diào)好。

          頭文件:

          本文引用地址:http://www.ex-cimer.com/article/201611/317366.htm

          /******************************************
          * 程序名稱: 遙控接收程序
          * 程序功能: 遙控操作
          * 目標(biāo)硬件: AVR MCU "ATMEGA128" 16.0000MHz
          * 文件名稱:telecontrol.c
          * 創(chuàng)建日期: 2009-02
          * 原創(chuàng)作者: zhaojun
          ******************************************/
          #ifndef TELECONTROL_H
          #define TELECONTROL_H

          // ************** 管腳定義

          #define IR 5
          #define IN_IR (PINE&(1<#define SET_IR_IN (DDRE &= ~(1<#define SET_IR_PORT (PORTE |= (1<

          // ************* 函數(shù)申明

          extern void IRInit(void); // 初始化紅外端口
          extern void IR_decode(void); // 紅外解碼函數(shù)

          // ************* 全局變量申明

          extern uint8 IRCOM[4]; // 紅外編碼暫存

          #endif

          C文件:

          /******************************************
          * 程序名稱: 遙控接收程序
          * 程序功能: 遙控操作
          * 目標(biāo)硬件: AVR MCU "ATMEGA128" 16.0000MHz
          * 文件名稱:telecontrol.c
          * 創(chuàng)建日期: 2009-02
          * 原創(chuàng)作者: zhaojun
          ******************************************/

          #include "..SOURCEconfig.h"

          uint8 IRCOM[4]; // IRCOM[0]和IRCOM[1]存放用戶編碼;IRCOM[2]鍵值碼暫存
          // IRCOM[3]鍵值反碼存放.

          /*****************************************************
          函 數(shù) 名:void IRInit(void)
          功 能:紅外引腳初始化
          說 明:無
          入口參數(shù):無
          返 回 值:無
          *****************************************************/
          void IRInit(void)
          {
          SET_IR_IN; // 設(shè)置端口為輸入
          SET_IR_PORT; // 開上拉電阻
          }

          /*****************************************************
          函 數(shù) 名:void IR_decode(void)
          功 能:紅外解碼函數(shù)
          說 明:利用普通端口解碼
          入口參數(shù):無
          返 回 值:無
          *****************************************************/
          void IR_decode(void)
          {
          uint8 i,j,k,num = 0;

          do // 讀引導(dǎo)碼
          {
          for (i=0; i<4; i++)
          {
          if (!IN_IR) break;
          if (i == 3)
          {
          return;
          }
          }
          DelayIR(20);
          }while (IN_IR);

          while(!IN_IR){DelayIR(1);} // 等IR變?yōu)楦唠娖?br />
          // 讀4字節(jié)按鍵編碼 = 16位的用戶碼+8位鍵值碼+8位鍵值反碼
          for (j=0; j<4; j++)
          { // 解碼原理:0=0.8ms的低電平+0.4ms的高電平;1=0.8ms的低電平+1.6ms的高電平
          for (k=0; k<8; k++) // 8位一接收
          {
          while (IN_IR){DelayIR(1);} // 等 IR 變?yōu)榈碗娖?br /> while (!IN_IR){DelayIR(1);} // 等 IR 變?yōu)楦唠娖?
          while (IN_IR) // 計(jì)算IR高電平時(shí)長(zhǎng)
          {
          DelayIR(1);
          num++;
          if (num >= 30)
          {
          return;
          }
          }

          IRCOM[j] >>= 1; // 接收數(shù)據(jù)右移一位
          if (num >= 8)
          {
          IRCOM[j] |= 0x80; // 電平長(zhǎng)度大于等于8,則寫入高電平
          }
          num = 0;

          } // 讀完一字節(jié)
          } // 按鍵4字節(jié)讀完
          // 接收數(shù)據(jù)檢測(cè)
          if (IRCOM[2] !=~ IRCOM[3]) // 8位鍵碼!=8位鍵反碼:按鍵錯(cuò)誤
          {
          return;
          }
          Ringing(1,100,50); // 蜂鳴器響1聲
          }

          /********************************************************************
          End Of File
          ********************************************************************/

          延時(shí)函數(shù):

          /*****************************************************
          函 數(shù) 名:void DelayIR()
          功 能:紅外解碼延時(shí)函數(shù)
          說 明:0.14MS延時(shí)函數(shù)
          入口參數(shù):tt
          返 回 值:無
          *****************************************************/
          void DelayIR(uint8 tt)
          {
          uint16 i;

          while(tt--)
          {
          for (i=315; i>0; i--)
          {
          asm(" nop");
          }
          }
          }



          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();