一個紅外線發(fā)射+解碼程序+仿真文件
接收源程序+仿真
點擊此處下載http://www.51hei.com/f/hong1.rar
遙控器源程序
點擊此處下載http://www.51hei.com/f/hong2.rar
/***************************************************************作品:紅外線遙控發(fā)射(NEC編碼)單片機:STC89C52RC晶振:12M***************************************************************///// 發(fā)射引腳(接PNP三極管b極)// PNP三極管e極接2Ω電阻,c極接紅外發(fā)射管#include#include "INCMY_SET.h"#include "INCLCD1602_6IO.h" sbit IR = P3^6; //發(fā)射引腳(接PNP三極管基極)#define USER_H P2 //用戶碼高8位#define USER_L P0 //用戶碼低8位uint8c tab[16] = { //操作碼0x12,0x05,0x1e,0x55,0x01,0x1b,0x03,0x6b,0x07,0x08,0x09,0x68,0x22,0xE6,0x33,0xe2};#define m9 (65536-9000) //9mS#define m4_5 (65536-4500) //4.5mS#define m1_6 (65536-1650) //1.65mS#define m_56 (65536-560) //0.56mS#define m40 (65536-40000) //40mS#define m56 (65536-56000) //56mS#define m2_25 (65536-2250) //2.25mSvoid SanZhuan();uint8 KEY(void);void ZZ(uint8 x); //NEC編碼發(fā)送程序void Z0(uint8 temp); //單幀(8位數(shù)據(jù))發(fā)送程序void TT0(bit BT,uint16 x); //38KHz載波發(fā)射 + 延時程序/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):主程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void main(void){ TMOD = 0x01; //T0 16位工作方式IR=1; //發(fā)射端口常態(tài)為高電平L1602_Init(); L1602_clr();L1602_xy(0,0);L1602_ZIFUC("UserCode :0x"); L1602_xy(0,1);L1602_ZIFUC("Opcode :0x");while(1) { L1602_xy(12,0);L1602_JZ(USER_H,16,1);L1602_JZ(USER_L,16,1);SanZhuan();}}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):4×4矩陣鍵盤【 線翻轉(zhuǎn)法鍵值表 】 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 │ │ │ │ │ │ │ │ │ │ │ └──7e be de ee │ │ └─────7d bd dd ed │ └────────7b bb db eb └─────────── 77 b7 d7 e7 ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/uint8 KEY(void){uint8 Key = 0;P1 = 0xf0; //鍵盤初始:行值=0,列值=1NOP; //緩沖,待IO端口電位穩(wěn)定Key = P1&0xf0; //得到行標(biāo)志P1 = 0x0f; //翻轉(zhuǎn)鍵盤接口輸出NOP;Key |= (P1&0x0f); //列標(biāo)志 + 行標(biāo)志return Key; //返回鍵值}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):散轉(zhuǎn)程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void SanZhuan(){uint8 v;v = KEY(); //鍵盤檢測switch(v){case 0x7e:ZZ(tab[0]);break; case 0xbe:ZZ(tab[1]);break; case 0xde:ZZ(tab[2]);break; case 0xee:ZZ(tab[3]);break; case 0x7d:ZZ(tab[4]);break; case 0xbd:ZZ(tab[5]);break; case 0xdd:ZZ(tab[6]);break; case 0xed:ZZ(tab[7]);break;case 0x7b:ZZ(tab[8]);break; case 0xbb:ZZ(tab[9]);break; case 0xdb:ZZ(tab[10]);break; case 0xeb:ZZ(tab[11]);break;case 0x77:ZZ(tab[12]);break; case 0xb7:ZZ(tab[13]);break; case 0xd7:ZZ(tab[14]);break; case 0xe7:ZZ(tab[15]);break; default:break;}v=0;}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):NEC編碼發(fā)送程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void ZZ(uint8 Value){ L1602_xy(12,1);L1602_JZ(Value,16,1); //更新顯示TT0(1,m9); //高電平9mSTT0(0,m4_5); //低電平4.5mS/*┈ 發(fā)送4幀數(shù)據(jù)┈*/Z0(USER_H); //用戶碼高8位Z0(USER_L); //用戶碼低8位Z0(Value); //操作碼Z0(~Value); //操作碼反碼/*┈┈ 結(jié)束碼 ┈┈*/TT0(1,m_56);TT0(0,m40);/*┈┈ 重復(fù)碼 ┈┈*/while(KEY() != 0xFF){TT0(1,m9);TT0(0,m2_25);TT0(1,m_56);TT0(0,m40);TT0(0,m56); } }/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):單幀(8位數(shù)據(jù))發(fā)送程序入口:temp┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void Z0(uint8 temp){ uint8 v;for(v=0;v<8;v++) { TT0(1,m_56); //高電平0.65mS if(temp&0x01) TT0(0,m1_6); //發(fā)送最低位else TT0(0,m_56); temp >>= 1; //右移一位 } }/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函數(shù):38KHz載波發(fā)射 + 延時程序入口:(是否發(fā)射載波,延時約 x (uS))┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void TT0(bit BT,uint16 x){TH0 = x>>8; //輸入定時值TL0 = x;TF0=0; //溢出標(biāo)志位清0TR0=1; //啟動定時器0if(BT == 0) while(!TF0); //BT=0時,不發(fā)射38KHz載波只延時;else while(1) //BT=1時,發(fā)射38KHz脈沖+延時;38KHz載波(低電平)占空比5:26{IR = 0;if(TF0)break;if(TF0)break;IR = 1;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;if(TF0)break;}TR0=0; //關(guān)閉定時器0IR =1; //載波停止后,發(fā)射端口常態(tài)為高}
評論