標題:
紅外電子鎖
[打印本頁]
作者:
白小七
時間:
2015-7-21 18:14
標題:
紅外電子鎖
自己做的,實測可用,用的是carMP3這種的21鍵的遙控器
紅外電子鎖(不復位,改密碼).zip
2015-7-21 18:13 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
42.45 KB, 下載次數(shù): 37, 下載積分: 黑幣 -5
作者:
admin
時間:
2015-7-22 00:14
#include "reg51.h"
#include "intrins.h"
#include"absacc.h"
#include"math.h"
#include"string.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE hdata=0x10;
BYTE ldata=0x10;
BYTE Ir_Buf[4]; //用于保存解碼結果
BYTE temp;
BYTE key=0;
BYTE num;
BYTE dis_buf;
BYTE Userpassword[10]="123";
BYTE inputpassword[10]="";
BYTE RXDDATA[]={0x00,0x00,0x00,0x00};
BYTE code RecvData[]={0x16,0x0C,0x18,0x5E,0x08,0x1C,0x5A,0x42,0x52,0x4A};
BYTE IRCOM[7];
int a;a=0;
int j;j=4;
int w=0;
int o=4;
int c=0;
sbit Ir_Pin=P3^3;
sbit led2=P2^2;
sbit led1=P2^1;
sbit beep=P2^0;
/* 液晶1602口地址*/
#define wr_com XBYTE[0xC000] //寫命令
#define wr_data XBYTE[0xC100] //寫數(shù)據(jù)
#define rd_com XBYTE[0xC200] //讀命令
#define rd_data XBYTE[0xC300] //讀數(shù)據(jù)
void Delay(WORD n);
void Delay2(WORD n);
void ShowResult(BYTE ch);
void lcd_init(void); // lcd初始化
void write_cmd(BYTE cmd); // lcd寫命令
//void write_string(unsigned char *s); // 寫字符串
void write_data(BYTE dat) ; // 寫數(shù)據(jù)
void set_display_place(BYTE line,column);
void write_string_lcd(BYTE line,column,unsigned char *string);
/********************************************************************
Function name: write_cmd
Descriptions: 向lcd輸入指令
********************************************************************/
void write_cmd(BYTE cmd)
{
BYTE dl;
do {
dl=rd_com;
} while((dl&0x80)!=0); //判忙
wr_com= cmd;
Delay(1);
}
/*******************************************************************
Function name: write_data
Descriptions: 寫入數(shù)據(jù)
*******************************************************************/
void write_data(BYTE dat)
{
BYTE dl;
do {
dl=rd_com;
} while((dl&0x80)!=0); //判忙
wr_data= dat;
Delay(1);
}
/********************************************************************
Function name: write_string
Descriptions: 寫入字符串
********************************************************************/
void write_string(BYTE *s)
{
while(*s != '\0') //'\0'為字符串結束標志
{
write_data(*s);
s++;
}
}
/***************************************************************
Function name: set_display_place
Descriptions: 設置字符的顯示位置
**************************************************************/
void set_display_place(BYTE line,column)
{
BYTE address;
if(line == 1)
{
address = 0x80 + column;
}
else if(line == 2)
{
address = 0xc0 + column;
}
write_cmd(address);
}
/*****************************************************************
Function name: 將字符串寫到指定的位置
Descriptions: 將字符串顯示在lcd的特定位置
*****************************************************************/
void write_string_lcd(BYTE line,column,unsigned char *string)
{
set_display_place(line,column);
write_string(string);
Delay(1);
}
/***************************************************
************液晶模塊初始化******************************
****************************************************/
void lcd_init(void)
{
write_cmd(0x38);//
write_cmd(0x38);//
write_cmd(0x06);//
write_cmd(0x0c);//
write_cmd(0x01);//
}
void crti(unsigned long dat)
{
unsigned char dhi;
unsigned char dli;
unsigned char ddi;
unsigned char dei;
unsigned long datah;
unsigned long datal;
datah=dat;
dhi=0;
if (datah>=10)
{
do
{
datah/=10;
dhi++;
}while(datah>=10);
datah=dat;
ddi=dhi;
for (dli=0;dli<dhi;dli++)
{
datal=1;
for (dei=0;dei<ddi;dei++)
{
datal*=10;
}
datah=dat/datal;
dat=dat%datal;
datah+=0x30;
write_data(datah);
ddi--;
}
dat+=0x30;
write_data(dat);
}
else
{
dat=dat+0x30;
write_data(dat);
}
}
void Delay(WORD n)
{
WORD x;
while(n--)
{
x=500; while(x--);
}
}
void Delay2(WORD n)
{
WORD x;
while(n--)
{
x=5000; while(x--);
}
}
void clear()
{write_string_lcd(2,4, " ");
}
////////////////////////////////////////
void int1_init(void)
{
IT1 = 1; //下降沿有效
EX1 = 1;
EA = 1;
TMOD = 0x01;
}
/*獲取低電平時間*/
unsigned int Ir_Get_Low()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (!Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
/* 獲取高電平時間*/
unsigned int Ir_Get_High()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
int fuwei()
{Delay2(15);
//
//
if(key==16)
return 0;
else return 1;
}
//*******************************************
void PC838()
{ int1_init();
}
void int1_isr() interrupt 2
{
unsigned int temp;
char i,j;
temp = Ir_Get_Low();
if ((temp < 7833) || (temp > 8755)) //引導脈沖低電平8500~9500us
return;
temp = Ir_Get_High();
if ((temp < 3686) || (temp > 4608)) //引導脈沖高電平4000~5000us
return;
for (i=0; i<4; i++) //4個字節(jié)
{
for (j=0; j<8; j++) //每個字節(jié)8位
{
temp = Ir_Get_Low();
if ((temp < 184) || (temp > 737)) //200~800us
return;
temp = Ir_Get_High();
if ((temp < 184) || (temp > 1843)) //200~2000us
return;
Ir_Buf[ i] >>= 1;
if (temp > 1032) //1120us
Ir_Buf[ i] |= 0x80;
}
}
if( Ir_Buf[2]=~ Ir_Buf[3]) //驗證鍵數(shù)據(jù)碼和其反碼是否相等,一般情況下不必驗證用戶碼
{
if(Ir_Buf[2]==0x16) key=0; //a[0]=00h;a[1]=ffh
if(Ir_Buf[2]==0x0c) key=1; //a[0]=00h;a[1]=ffh
if(Ir_Buf[2]==0x18) key=2; //a[0]=00h;a[1]=ffh
if(Ir_Buf[2]==0x5E) key=3;
if(Ir_Buf[2]==0x08) key=4;
if(Ir_Buf[2]==0x1C) key=5;
if(Ir_Buf[2]==0x5A) key=6;
if(Ir_Buf[2]==0x42) key=7;
if(Ir_Buf[2]==0x52) key=8;
if(Ir_Buf[2]==0x4A) key=9;
if(Ir_Buf[2]==0x45) key=10; //ON/OFF
if(Ir_Buf[2]==0x46) key=11; //MODE
if(Ir_Buf[2]==0x47) key=12; //靜音
if(Ir_Buf[2]==0x44) key=13; //暫停
if(Ir_Buf[2]==0x40) key=14; //下一曲
if(Ir_Buf[2]==0x43) key=15; //上一曲
if(Ir_Buf[2]==0x07) key=16; //EQ
dis_buf = key; //鍵值入顯示緩存
//dis_buf = dis_buf & 0x0f;
if(dis_buf>9) //轉換為ASCII碼
dis_buf = dis_buf+0x37;
else
dis_buf = dis_buf+0x30;
// beep(); //蜂鳴器響一聲 提示解碼成功
}
switch(key)
{case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
{inputpassword[a]=key+'0';
inputpassword[a+1]='\0';
a++;
set_display_place(2,o);
write_data(dis_buf);
o++;
}break;
case 10:
o++;
if(strcmp(inputpassword,Userpassword)==0)
{
write_string_lcd(2,0, "Unlock OK! ");
write_string_lcd(1,0, "Welcome ");
c=1;
inputpassword[0]="a";
a=0;
led1=0;
o=4;
w=0;
Delay2(200);
clear();
led1=1;
//write_string_lcd(1,0, "hello! ");
// write_string_lcd(2,0, "key: ");
}
else {w++;
if(w<3)
{ write_string_lcd(2,4, " error ");
inputpassword[10]="";
a=0;
led2=0;
Delay2(60);clear();o=4;c=0;led2=1;}
else{//
while(fuwei()!=0)
//while(1)
{
write_string_lcd(2,4, " shutdown!!! ");beep=0;
//
}
inputpassword[10]="";
clear();
beep=1;
a=0;
o=4;
w=0;
c=0;}
//Delay2(60);
}
//clear();
break;
case 12:
if(c==0) {write_string_lcd(2,4, "can't change ");Delay2(60);clear();o=4;a=0;}
else
{
Userpassword[0]=inputpassword[0];
Userpassword[1]=inputpassword[1];
Userpassword[2]=inputpassword[2];
Userpassword[3]=inputpassword[3];
Userpassword[4]=inputpassword[4];
Userpassword[5]=inputpassword[5];
Userpassword[6]=inputpassword[6];
Userpassword[7]=inputpassword[7];
Userpassword[8]=inputpassword[8];
Userpassword[9]=inputpassword[9];
Userpassword[10]=inputpassword[10];
beep=1;
o=4;
w=0;
c=0;
a=0;write_string_lcd(2,4, "change ok! "); Delay2(60);clear();o=4;}
break;
case 11:
{clear();
inputpassword[10]="";
c=0;
o=4;
a=0;}break;
}
//EX1=0;
}
////////////////////////////////////////
void main()
{j=4;
lcd_init();
//SP=0x60;
beep= 1;
PC838(); //設定外部中斷1為低邊緣觸發(fā)類型
//總中斷開啟
while(1)
{
if(c==1) write_string_lcd(1,7, "C");
else write_string_lcd(1,7, " ");
write_string_lcd(1,10, "warn:");
set_display_place(1,15);
write_data(0x30+w);
write_string_lcd(1,0, "hello! ");
write_string_lcd(2,0, "key:");
write_string_lcd(2,o, "_");//光標閃爍
Delay2(15);
write_string_lcd(2,o," ");
Delay2(15);
//
//
//
//if
//
//write_data(ldata+0x30);
// j++;
}
}
復制代碼
作者:
鳥蛤
時間:
2017-10-29 11:09
有沒有仿真圖?
作者:
hqhq
時間:
2017-10-31 19:31
謝謝分享
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1