- #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] //写数据
- #define rd_com XBYTE[0xC200] //读命令
- #define rd_data XBYTE[0xC300] //读数据
- 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) ; // 写数据
- 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: 写入数据
- *******************************************************************/
- 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个字节
- {
- for (j=0; j<8; j++) //每个字节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]) //验证键数据码和其反码是否相等,一般情况下不必验证用户码
- {
- 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为低边缘触发类型
- //总中断开启
- 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++;
-
-
- }
- }
复制代码 |