完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include "reg52.h"
***it alarm=P2^0;//蜂鸣器 ***it red=P3^7; ***it green=P3^6; volatile unsigned char Intrcnt; unsigned char code null[]={" "}; unsigned char cuowu3[]={"NO KEY ! 2 "}; unsigned char cuowu2[]={"NO KEY ! 1 "}; unsigned char cuowu1[]={"NO KEY ! 0 "}; unsigned char over[]={"game over ! "}; unsigned char kaishi[]={"input 6 key: "}; unsigned char ok[]={"key ok! "}; unsigned char wei_flag=0,cuowu_flag=0,led_flag=0; unsigned char volatile keyzhi=0; unsigned char mima[6]={0,0,0,0,0,0}; unsigned char mima_jiancha[6]={1,1,1,1,1,1}; #define KEY P1 #define LCD_DATA P0 ***it EN=P2^6; ***it RS=P2^7; void Delay(unsigned int i) { while(-- i); } /********4x4键盘函数******************* ***************************/ unsigned char key_44(void) { unsigned char a,b=0,c; KEY = 0x0f; //设定键盘初始电平状态 if (KEY != 0x0f){ //读取键盘状态是否改变 // delay20ms(); //误差 0us // if (KEY != 0x0f){ //重新读取 a = KEY; //寄存状态值到a // } KEY = 0xf0; //设定键盘反向电平状态 c = KEY; //读取反向电平状态值到c a = a|c; //a与c相或 switch(a){ //键盘状态查表 case 0xee: b = 1; break; case 0xed: b = 2; break; case 0xeb: b = 3; break; case 0xe7: b = 4; break; case 0xde: b = 5; break; case 0xdd: b = 6; break; case 0xdb: b = 7; break; case 0xd7: b = 8; break; case 0xbe: b = 9; break; case 0xbd: b = 10; break; case 0xbb: b = 11; break; case 0xb7: b = 12; break; case 0x7e: b = 13; break; case 0x7d: b = 14; break; case 0x7b: b = 15; break; case 0x77: b = 16; break; default: b = 0 ; break; } } return (b); //将b中的键值代号送入函数返回值 } /*******延时函数********************** *******************************/ void delay_ms(unsigned char ms) { unsigned int i; do{ i = 12000000 / 13000; while(--i) ; //14T per loop }while(--ms); } /*********LCD函数************* *****************************/ void LCD1602(void) { EN = 0; RS = 1; //RW = 1; LCD_DATA = 0xFF; } /*void Read_Busy(void) { LCD_DATA = 0xFF; RS = 0; //RW = 1; EN = 0; Delay(1250); EN = 1; // while(LCD_DATA & 0x80); Delay(1250); Delay(1250); Delay(1250); EN = 0; }*/ void Write_Lcd(unsigned char Value, bit i) //i = 0写指令.i = 1写数据; { EN = 0; Delay(1250); // Read_Busy(); LCD_DATA = Value; RS = i; // RW = 0; EN = 1; Delay(1250); EN = 0; } void Init_Lcd(void) { Delay(15000); Write_Lcd(0x38,0); Delay(5000); Write_Lcd(0x38,0); Delay(5000); Write_Lcd(0x38,0); Write_Lcd(0x08,0); Write_Lcd(0x01,0); Write_Lcd(0x06,0); Write_Lcd(0x0c,0); } void Display_LCD_Byte(unsigned char y,unsigned char x,unsigned char Value) { y &= 0x01; //y不能大于1 x &= 0x0F; //x不能大于15 if(y) //是否显示在第二行; { x += 0x40; //显示在第二行,加0X40; } x += 0x80; Write_Lcd(x,0); //写入显示的位置 Write_Lcd(Value,1); //写入显示数据 } void Display_LCD_TEXT(unsigned char y,unsigned char x,unsigned char *Value) { unsigned ListLength = 0; y &= 0x01; x &= 0x0F; while(x <= 15) { Display_LCD_Byte(y,x,Value[ListLength]); x ++; ListLength ++; } } /******************************* *********************************/ void InitTimer0(void) { TMOD = 0x01; TH0 = 0x0FC; TL0 = 0x18; EA = 1; ET0 = 1; TR0 = 1; //定时器的设置 } char jiancha() { char i; for(i=0;i<6;i++) { if(mima[i]!=mima_jiancha[i]) { // EA=0; return 0; } } return 1; } void main(void) { LCD1602(); Init_Lcd(); Display_LCD_TEXT(0,0,kaishi); InitTimer0(); while(1) // 每20ms 执行一次大循环 { keyzhi = key_44(); //keyzhi = key_44(); if(keyzhi) { switch(keyzhi) { case 1: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 2: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 3: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 4: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 5: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 6: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 7: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 8: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 9: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = keyzhi; keyzhi=0; wei_flag++; green=~green; } break; case 10: { Display_LCD_Byte(1,wei_flag,'*'); mima_jiancha[wei_flag] = 0; keyzhi=0; wei_flag++; green=~green; } break; case 16: { if(jiancha()) { Display_LCD_TEXT(0,0,ok); } else { cuowu_flag++; switch(cuowu_flag) { case 1: { Display_LCD_TEXT(0,0,cuowu3); wei_flag=0; Display_LCD_TEXT(1,0,null); }break; case 2: { Display_LCD_TEXT(0,0,cuowu2); wei_flag=0; Display_LCD_TEXT(1,0,null); }break; case 3: { Display_LCD_TEXT(0,0,cuowu1); // wei_flag=0; // led_flag=1; Display_LCD_TEXT(0,0,over); Display_LCD_TEXT(1,0,over); while(1) { green=0; delay_ms(25); green=1; delay_ms(25); } }break; } } } default: break; } } while(1) { if (Intrcnt>200) // 一直在等,直到20ms时间到 { Intrcnt=0; if(led_flag) green=~green; break; // 返回主循环 } } } } void Timer0Interrupt(void) interrupt 1 { TH0 = 0x0FC; TL0 = 0x18; //add your code here! Intrcnt++; // 1ms 中断1次,可变 }
|
|
相关推荐
2个回答
|
|
你这边到底是什么错误啊,这么一大堆,我们又没有实际的环境。你把错误的地方说出来看看
|
|
|
|
这个是一个密码锁仿真电路的程序,但是用来仿真却运行不了,附加图片了 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
770 浏览 0 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
544 浏览 0 评论
【敏矽微ME32G070开发板免费体验】使用coremark测试敏矽微ME32G070 跑分
677 浏览 0 评论
【敏矽微ME32G070开发板免费体验】开箱+点灯+点亮OLED
904 浏览 2 评论
649 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11990 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 17:30 , Processed in 0.562732 second(s), Total 77, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号