实验所需期间:电脑一台,正点原子stm32f1精英板一套,STlink烧录器、usb连接线,keil5软件
实现功能:输入登陆密码, 与原先设置好的密码比较,正确的话开门(这里通过LED0红灯闪烁模拟);
失败的话==〉蜂鸣器响一下,出现失败界面,同时绿灯闪烁2下,并计算失败次数,假如失败次数等于3次,锁定输入并蜂鸣器响3下;
密码重置==〉对登陆密码进行更改,跟重置密码一致的话重新输入
主程序:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "lcd.h"
#include "usart.h"
#include "remote.h"
#include "codelock.h"
#include "beep.h"
#include "sys.h"
u8 *in_pwd1=0;
u8 *in_pwd2=0;
u8 *in_pwd3=0;
u8 *in_pwd4=0;
u8 *in_pwd5=0;
u8 *in_pwd6=0;
int fall_time=0;
int in_time;
int my_key=0;
u8 open_pwd[6]="123456";
u8 root_pwd[6]="666666";
u8 key;
u8 *str=0;
int main(void)
{
int t=0;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
LCD_Init();
KEY_Init();
BEEP_Init();
Remote_Init(); //红外接收初始化
POINT_COLOR=RED; //设置字体为红色
LCD_ShowString(30,50,200,16,16,"ELITE STM32");
LCD_ShowString(30,70,200,16,16,"REMOTE TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2015/1/15");
LCD_ShowString(30,130,200,16,16,"KEYVAL:");
LCD_ShowString(30,150,200,16,16,"SYMBOL:");
LCD_ShowString(30,180,200,24,24,"PASSWORD:");
while(1)
{
Code_Deal();
}
}
//**************************************************//
//**************************************************//
//**************************************************//
//**************************************************//
密码锁程序(codelock.c)
#include "codelock.h"
#include "led.h"
#include "delay.h"
#include "lcd.h"
#include "remote.h"
#include "beep.h"
extern u8 *in_pwd1;
extern u8 *in_pwd2;
extern u8 *in_pwd3;
extern u8 *in_pwd4;
extern u8 *in_pwd5;
extern u8 *in_pwd6;
extern int fall_time;
extern int in_time;
extern int my_key;
extern u8 open_pwd[6];
extern u8 root_pwd[6];
extern u8 key; //其实这些字符指针或者整数型指针都在main文件下已经定义
extern u8 *str; //在外部已定义,在codelock文件下声明后即可调用
void Code_Deal(void)
{
key=Remote_Scan(); //每次按下key(即红外遥控器按钮,会返回一个键值)
if(key)
{
LCD_ShowString(30+56,130,200,16,16,&key); //显示key的键值
switch(key) //对键值进行判断,假如满足那个键值,就返回相应的字符串str
{
case 0:str="ERROR";break;
case 104:str="1";break;
case 152:str="2";break;
case 176:str="3";break;
case 48:str="4";break;
case 24:str="5";break;
case 122:str="6";break;
case 16:str="7";break;
case 56:str="8";break;
case 90:str="9";break;
case 66:str="0";break;
case 82:str="DELETE";break;
case 162:str="POWER";break;
case 98:str="+";break;
case 2:str="PLAY";break;
case 226:str="ALIENTEK";break;
case 194:str="/";break;
case 34:str="*";break;
case 224:str="VOL-";break;
case 168:str="-";break;
case 144:str="VOL+";break;
}
LCD_Fill(30+56,150,286,166,WHITE); //清理字符所在位置
LCD_ShowString(30+56,150,200,16,16,str); //显示字符串str
if(Rmtcnt&&!my_key) //要在if(key)这个if语句内
{ //假如Rmtcnt为真,my_key为假时,即按钮按下、密码输入使能
in_time=in_time+1;
if(in_time==1) { LCD_Fill(86,270,86+240,270+16,WHITE);} //先清理密码字符所在位置
if(str=="0"||str=="1"||str=="2"||str=="3"||str=="4"||str=="5"||str=="6"||str=="7"||str=="8"||str=="9")
{
switch(in_time)//关键 ---这里又用到一个switch语句,对密码位数in_time判断,对应输出以及赋值给in_pwd1~in_pwd6这6个字符数组
{
case 1: in_pwd1=str;LCD_ShowString(142,180,200,24,24,in_pwd1);delay_ms(200);LCD_ShowString(142,180,200,24,24,"*");break;
case 2: in_pwd2=str;LCD_ShowString(142+12,180,200,24,24,in_pwd2);delay_ms(200);LCD_ShowString(142+12,180,200,24,24,"*");break;
case 3: in_pwd3=str;LCD_ShowString(142+24,180,200,24,24,in_pwd3);delay_ms(200);LCD_ShowString(142+24,180,200,24,24,"*");break;
case 4: in_pwd4=str;LCD_ShowString(142+36,180,200,24,24,in_pwd4);delay_ms(200);LCD_ShowString(142+36,180,200,24,24,"*");break;
case 5: in_pwd5=str;LCD_ShowString(142+48,180,200,24,24,in_pwd5);delay_ms(200);LCD_ShowString(142+48,180,200,24,24,"*");break;
case 6: in_pwd6=str;LCD_ShowString(142+60,180,200,24,24,in_pwd6);delay_ms(200);LCD_ShowString(142+60,180,200,24,24,"*");break;
}
}
}
if(in_time==6) //当输入的密码位数达到6位时,密码与登陆密码一致就进入
{
if(*in_pwd1==open_pwd[0]&&*in_pwd2==open_pwd[1]&&*in_pwd3==open_pwd[2]&&*in_pwd4==open_pwd[3]&&*in_pwd5==open_pwd[4]&&*in_pwd6==open_pwd[5])
{
LCD_Fill(30,180,420,250,WHITE);
LCD_ShowString(142,180,200,24,24,"Welcome");
LCD_ShowString(142,204,200,24,24,"Open The Door!");
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
delay_ms(1000);
delay_ms(1000);
LCD_Fill(30,180,420,250,WHITE);
LCD_ShowString(30,180,200,24,24,"PASSWORD:");
}else if(*in_pwd1==root_pwd[0]&&*in_pwd2==root_pwd[1]&&*in_pwd3==root_pwd[2]&&*in_pwd4==root_pwd[3]&&*in_pwd5==root_pwd[4]&&*in_pwd6==root_pwd[5])
{ //输入密码与重置密码一致时,进入重置界面
in_time=0;
root_h();
}
else //失败则重新输入并计算失败次数
{
LCD_Fill(142,180,420,302,WHITE);
LCD_ShowString(142,180,200,24,24,"Wrong Password!!!");
fall_time++;
LED0=!LED0;
BEEP=!BEEP;
delay_ms(500);
LED0=!LED0;
BEEP=!BEEP;
delay_ms(500);
LED0=!LED0;
delay_ms(500);
LED0=!LED0;
LCD_Fill(142,180,420,250,WHITE);
}
in_time=0;
}
if(fall_time==3) //失败的次数达到3次时,锁定1分钟,然后才可重新输入密码
{
fall_time=0;
LCD_Fill(30,180,420,228,WHITE);
LCD_ShowString(42,180,420,24,24,"Has been locked ");
LCD_ShowString(30,180,420,24,24,"please wait 1 minute...");
delay_ms(500);BEEP=!BEEP;delay_ms(500);BEEP=!BEEP;delay_ms(500);BEEP=!BEEP;
delay_ms(500);BEEP=!BEEP;delay_ms(500);BEEP=!BEEP;delay_ms(500);BEEP=!BEEP;
LCD_Fill(30,180,420,228,WHITE);
LCD_ShowString(30,180,200,24,24,"PASSWORD:");
}
}
}
void root_h(void) //这是重置时的操作函数---对密码进行修改
{ //原理与上面一样,就是一个while包着key语句,key语句包着swtich与及if语句
in_pwd1="0";
in_pwd2="0";
in_pwd3="0";
in_pwd4="0";
in_pwd5="0";
in_pwd6="0";
delay_ms(20);
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
delay_ms(500);
LED1=!LED1;
LCD_Fill(30,50,420,250,WHITE);
LCD_ShowString(42,180,420,24,24,"Welcome to root mode ");
LCD_ShowString(30,204,420,24,24,"You can reset the password here!");
LCD_ShowString(42,228,420,24,24,"NEWPWD:");
do
{
key=Remote_Scan();
if(key)
{
switch(key)
{
case 0:str="ERROR";break;
case 104:str="1";break;
case 152:str="2";break;
case 176:str="3";break;
case 48:str="4";break;
case 24:str="5";break;
case 122:str="6";break;
case 16:str="7";break;
case 56:str="8";break;
case 90:str="9";break;
case 66:str="0";break;
case 82:str="DELETE";break;
case 162:str="POWER";break;
case 98:str="+";break;
case 2:str="PLAY";break;
case 226:str="ALIENTEK";break;
case 194:str="/";break;
case 34:str="*";break;
case 224:str="VOL-";break;
case 168:str="-";break;
case 144:str="VOL+";break;
}
if(Rmtcnt&&!my_key)
{
in_time=in_time+1;
if(str=="0"||str=="1"||str=="2"||str=="3"||str=="4"||str=="5"||str=="6"||str=="7"||str=="8"||str=="9")
{
switch(in_time)
{
case 1: open_pwd[0]=*str;in_pwd1=str;LCD_ShowString(142,228,200,24,24,in_pwd1);delay_ms(200);LCD_ShowString(142,228,200,24,24,"*");break;
case 2: open_pwd[1]=*str;in_pwd2=str;LCD_ShowString(142+12,228,200,24,24,in_pwd2);delay_ms(200);LCD_ShowString(142+12,228,200,24,24,"*");break;
case 3: open_pwd[2]=*str;in_pwd3=str;LCD_ShowString(142+24,228,200,24,24,in_pwd3);delay_ms(200);LCD_ShowString(142+24,228,200,24,24,"*");break;
case 4: open_pwd[3]=*str;in_pwd4=str;LCD_ShowString(142+36,228,200,24,24,in_pwd4);delay_ms(200);LCD_ShowString(142+36,228,200,24,24,"*");break;
case 5: open_pwd[4]=*str;in_pwd5=str;LCD_ShowString(142+48,228,200,24,24,in_pwd5);delay_ms(200);LCD_ShowString(142+48,228,200,24,24,"*");break;
case 6: open_pwd[5]=*str;in_pwd6=str;LCD_ShowString(142+60,228,200,24,24,in_pwd6);delay_ms(200);LCD_ShowString(142+60,228,200,24,24,"*");break;
}
}
if(in_pwd1=="6"&&in_pwd2=="6"&&in_pwd3=="6"&&in_pwd4=="6"&&in_pwd5=="6"&&in_pwd6=="6")
{
in_time=0;
LCD_ShowString(42+84,228,420,24,24,"coudle not same as root");
LCD_ShowString(42+84,228+24,420,24,24,"please input new password");
delay_ms(1000);delay_ms(1000);delay_ms(1000);
LCD_Fill(126,228,480,228+48,WHITE);
}
}
if(in_time==6)
{
in_time=0;
LCD_Fill(30,228,420,228+48,WHITE);
LCD_ShowString(42+84,228,420,24,24,"Reset complete!");
LCD_ShowString(42+84,228+24,420,24,24,"will be back to ordinary mode ");
delay_ms(1000);delay_ms(1000);delay_ms(1000);
key=82;
}
}
}while(key!=82);
LCD_Fill(30,180,480,228+48,WHITE);
LCD_ShowString(30,50,200,16,16,"ELITE STM32");
LCD_ShowString(30,70,200,16,16,"REMOTE TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2015/1/15");
LCD_ShowString(30,130,200,16,16,"KEYVAL:");
LCD_ShowString(30,150,200,16,16,"SYMBOL:");
LCD_ShowString(30,180,200,24,24,"PASSWORD:");
}
|