#include\x0d\x0a#include\x0d\x0a#define uchar unsigned char\x0d\x0a#define uint unsigned int\x0d\x0asbit lcden = P2^7\x0d\x0asbit lcdrs = P2^6\x0d\x0asbit lcdwr = P2^5\x0d\x0asbit IR = P3^2\x0d\x0auchar IRCOM[6]//数组,用于存储红外编码\x0d\x0auchar code table1[] = "remote control"\x0d\x0auchar code table2[] = "CODE:"\x0d\x0avoid delayms(uchar x)// 延时x*0.14ms\x0d\x0a{\x0d\x0auchar i\x0d\x0awhile(x--)\x0d\x0afor(i=0i//延时xms\x0d\x0a{\x0d\x0auchar i,j\x0d\x0afor(i=xi>0i--)\x0d\x0afor(j=110j>0j--)\x0d\x0a}\x0d\x0a/****************************LCD部分***********************************************/\x0d\x0avoid write_com(uchar com)\x0d\x0a{\x0d\x0alcden = 0\x0d\x0alcdrs = 0\x0d\x0alcdwr = 0\x0d\x0aP0 = com\x0d\x0adelay(5)\x0d\x0alcden = 1\x0d\x0adelay(5)\x0d\x0alcden = 0//别忘了lcden拉低\x0d\x0a}\x0d\x0avoid write_date(uchar date)\x0d\x0a{\x0d\x0alcden = 0\x0d\x0alcdrs = 1\x0d\x0alcdwr = 0\x0d\x0aP0 = date\x0d\x0adelay(5)\x0d\x0alcden = 1\x0d\x0adelay(5)\x0d\x0alcden = 0\x0d\x0a}\x0d\x0avoid lcd_init(void)\x0d\x0a{\x0d\x0alcden = 0\x0d\x0alcdrs = 0\x0d\x0alcdwr = 0\x0d\x0adelay(5)\x0d\x0awrite_com(0x38)\x0d\x0awrite_com(0x0c)\x0d\x0awrite_com(0x06)\x0d\x0awrite_com(0x01)\x0d\x0a}\x0d\x0a/*****************main()************************/\x0d\x0avoid main(void)\x0d\x0a{\x0d\x0auchar count=0\x0d\x0aIR = 1\x0d\x0alcd_init()\x0d\x0awrite_com(0x80)\x0d\x0awhile(table1[count]!='\0')\x0d\x0a{\x0d\x0awrite_date(table1[count])\x0d\x0acount++\x0d\x0adelay(5)\x0d\x0a}\x0d\x0acount = 0\x0d\x0awrite_com(0x80+0x40)\x0d\x0awhile(table2[count]!='\0')\x0d\x0a{\x0d\x0awrite_date(table2[count])\x0d\x0acount++\x0d\x0adelay(5)\x0d\x0a}\x0d\x0a\x0d\x0aIE = 0x81//开中断\x0d\x0aTCON = 0x01//脉冲负边沿触发\x0d\x0awhile(1)\x0d\x0a\x0d\x0a}\x0d\x0a/*********************红外中断**************************/\x0d\x0avoid IR_time() interrupt 0 \x0d\x0a{\x0d\x0auchar i,j,TimeNum=0//TimeNum用来计IR
高电平次数 从而判断是0还是1\x0d\x0aEX0 = 0//关闭中断\x0d\x0adelayms(5)\x0d\x0aif(1 == IR)\x0d\x0a{\x0d\x0aEX0 = 1\x0d\x0areturn\x0d\x0a}\x0d\x0awhile(!IR) //
跳过9ms前导低电平\x0d\x0adelayms(1)\x0d\x0afor(i=0i{\x0d\x0afor(j=0j{\x0d\x0awhile(IR) //跳过4.5ms的前导高电平\x0d\x0adelayms(1)\x0d\x0awhile(!IR) //跳过0.56ms的低电平\x0d\x0adelayms(1)\x0d\x0a\x0d\x0awhile(IR) \x0d\x0a{\x0d\x0aTimeNum++//计时高电平时间从而判断读取的是0还是1\x0d\x0adelayms(1)\x0d\x0a}\x0d\x0aif(TimeNum>=30)//按键按下时间过长 跳过\x0d\x0a{\x0d\x0aEX0 = 1\x0d\x0areturn\x0d\x0a}\x0d\x0aIRCOM[i] = IRCOM[i]>>1\x0d\x0aif(TimeNum >= 8) //8*0.14ms 这时读取的是1;\x0d\x0a{\x0d\x0aIRCOM[i] = IRCOM[i]|0x80\x0d\x0a}\x0d\x0aTimeNum = 0\x0d\x0a}\x0d\x0a}\x0d\x0aif(IRCOM[2]!=~IRCOM[3])//判断八位数据和八位数据反码是否相等\x0d\x0a{\x0d\x0aEX0 = 1\x0d\x0areturn\x0d\x0a}\x0d\x0aIRCOM[4] = IRCOM[2]&0x0f//取低四位\x0d\x0aIRCOM[5] = IRCOM[2]>>4//IRCOM[5]取IRCOM[2]高四位\x0d\x0aif(IRCOM[4] >9) //转换成字符\x0d\x0a{\x0d\x0aIRCOM[4] = IRCOM[4] + 0x37\x0d\x0a}\x0d\x0aelse\x0d\x0aIRCOM[4] = IRCOM[4] + 0x30\x0d\x0aif(IRCOM[5] >9)\x0d\x0a{\x0d\x0aIRCOM[5] = IRCOM[5] + 0x37\x0d\x0a}\x0d\x0aelse\x0d\x0aIRCOM[5] = IRCOM[5] + 0x30\x0d\x0adelay(5)\x0d\x0awrite_com(0x80 + 0x40 + 5)\x0d\x0awrite_date(IRCOM[5])\x0d\x0awrite_date(IRCOM[4])\x0d\x0aEX0 = 1//重新开启外部中断\x0d\x0a}以下文件是51单片机实现遥控解码,通过
数码管显示键码的程序,P0口驱动数码管段选,p2.6和p2.7为数码管位选,接收头连到P3.2口。此程序以通过验证,可以直接编译使用,另外还有一个继电器和蜂鸣器的控制,不用可以屏蔽掉。
********************************************************************************
* 描述: *
* 遥控键值读取器*
* 数码管显示, P0口为数码管的数据口 *
* *
********************************************************************************
遥控键值解码-数码管显示*
********************************************************************************/
#include <reg51.h>
#include <intrins.h>
void IR_SHOW()
void delay(unsigned char x)//x*0.14MS
void delay1(unsigned char ms)
void beep()
sbit IRIN = P3^2
sbit BEEP = P3^7
sbit RELAY= P1^3
sbit GEWEI= P2^7
sbit SHIWEI= P2^6
unsigned char IRCOM[8]
unsigned char code table[16] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}
main()
{
IE = 0x81 //允许总中断中断,使能 INT0 外部中断
TCON = 0x1 //触发方式为脉冲负边沿触发
delay(1)
IRIN=1
BEEP=1
RELAY=1
for()
{
IR_SHOW()
}
} //end main
void IR_IN() interrupt 0 using 0
{
unsigned char i,j,k,N=0
EA = 0
I1:
for (i=0i<4i++)
{
if (IRIN==0) break
if (i==3) {EA =1return}
}
delay(20)
if (IRIN==1) goto I1 //确认IR信号出现
while (!IRIN)//等 IR 变为高电平
{delay(1)}
for (j=0j<4j++)
{
for (k=0k<8k++)
{
while (IRIN)//等 IR 变为低电平
{delay(1)}
while (!IRIN) //等 IR 变为高电平
{delay(1)}
while (IRIN) //计算IR高电平时长
{
delay(1)
N++
if (N>=30) {EA=1return}
}
IRCOM[j]=IRCOM[j] >>1
if (N>=8) {IRCOM[j] = IRCOM[j] | 0x80}
N=0
}//end for k
}//end for j
if (IRCOM[2]!=~IRCOM[3]) {EA=1return}
IRCOM[5]=IRCOM[2] &0x0F
IRCOM[6]=IRCOM[2] &0xF0
IRCOM[6]=IRCOM[6] >>4
beep()
EA = 1
}
void IR_SHOW()
{
P0 = table[IRCOM[5]]
GEWEI = 0
SHIWEI = 1
delay1(4)
P0 = table[IRCOM[6]]
SHIWEI = 0
GEWEI = 1
delay1(4)
}
void beep()
{
unsigned char i
for (i=0i<100i++)
{
delay(5)
BEEP=!BEEP
}
BEEP=1
}
void delay(unsigned char x)//x*0.14MS
{
unsigned char i
while(x--)
{
for (i = 0i<13i++) {}
}
}
void delay1(unsigned char ms)
{
unsigned char i
while(ms--)
{
for(i = 0i<120i++)
{
_nop_()
_nop_()
_nop_()
_nop_()
}
}
}
sbit IR_OUT = P3^2
unsigned char SigInfo[4]//存储红外按键编码,SigInfo[2]为按键值
void Cmd_Require(void)
void INT_Ext0() interrupt 1 //外部中断0
{
u8 i, j
u16 time = 8000
EA = 0
for (i=0 i<10 i++) {
delay0_7ms ()
if (IR_OUT) {
EA = 1
return
}
}
while (!IR_OUT)
delay2_5ms ()
if (!IR_OUT) {
EA = 1
return
}
while (IR_OUT&&time) {
time--
}
time = 8000
for (i=0 i<4 i++) {
for (j=0 j<8 j++) {
SigInfo[i] >>= 1
while (!IR_OUT)
delay0_7ms ()
if (IR_OUT) {
SigInfo[i] |= 0x80
while (IR_OUT&&time) {
time--
}
time = 8000
}
}
}
Cmd_Require()
EA = 1
}
void Cmd_Require(void)
{
switch(SigInfo[2]) {
case 0x0c:
//此处代码随意
break
case 0x18:
//...
break
//...
default:
break
}
}
延时没写,因单片机而异的,要精确点才能解码。
评论列表(0条)