接收源程序+仿真
点击此处下载http://www.51hei.com/f/hong1.rar
遥控器源程序
点击此处下载http://www.51hei.com/f/hong2.rar
/***************************************************************作品:红外线遥控发射(NEC编码)单片机:STC89C52RC晶振:12M***************************************************************///// 发射引脚(接PNP三极管b极)// PNP三极管e极接2Ω电阻,c极接红外发射管#include #include "INCMY_SET.h"#include "INCLCD1602_6IO.h" sbit IR = P3^6; //发射引脚(接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编码发送程序void Z0(uint8 temp); //单帧(8位数据)发送程序void TT0(bit BT,uint16 x); //38KHz载波发射 + 延时程序/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:主程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void main(void){ TMOD = 0x01; //T0 16位工作方式IR=1; //发射端口常态为高电平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();}}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:4×4矩阵键盘 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端口电位稳定Key = P1&0xf0; //得到行标志P1 = 0x0f; //翻转键盘接口输出NOP;Key |= (P1&0x0f); //列标志 + 行标志return Key; //返回键值}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:散转程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/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;}/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:NEC编码发送程序┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void ZZ(uint8 Value){ L1602_xy(12,1);L1602_JZ(Value,16,1); //更新显示TT0(1,m9); //高电平9mSTT0(0,m4_5); //低电平4.5mS/*┈ 发送4帧数据┈*/Z0(USER_H); //用户码高8位Z0(USER_L); //用户码低8位Z0(Value); //操作码Z0(~Value); //操作码反码/*┈┈ 结束码 ┈┈*/TT0(1,m_56);TT0(0,m40);/*┈┈ 重复码 ┈┈*/while(KEY() != 0xFF){TT0(1,m9);TT0(0,m2_25);TT0(1,m_56);TT0(0,m40);TT0(0,m56); } }/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:单帧(8位数据)发送程序入口: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); //发送最低位else TT0(0,m_56); temp >>= 1; //右移一位 } }/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈函数:38KHz载波发射 + 延时程序入口:(是否发射载波,延时约 x (uS))┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/void TT0(bit BT,uint16 x){TH0 = x>>8; //输入定时值TL0 = x;TF0=0; //溢出标志位清0TR0=1; //启动定时器0if(BT == 0) while(!TF0); //BT=0时,不发射38KHz载波只延时;else while(1) //BT=1时,发射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; //关闭定时器0IR =1; //载波停止后,发射端口常态为高}