实验目的:
学习红外遥控器的使用.
硬件资源:
1,DS0(连接在PA8)
2,串口1(波特率:9600,PA9/PA10连接在板载USB转串口芯片CH340上面)
3,ALIENTEK 2.8/3.5/4.3/7寸TFTLCD模块(通过GPIO驱动,连接关系见lcd.h)
4,定时器5(TIM5),使用TIM5_CH2(PA1)的输入捕获,来解码红外信号.
5,ALIENTEK红外遥控器
实验现象:
本实验开机在LCD上显示一些信息之后,即进入等待红外触发,如过接收到正确的红外信号,则解码,并在LCD上显示
键值和所代表的意义,以及按键次数等信息。同样我们也是用DS0来指示程序正在运行。
注意事项:
1,4.3寸和7寸屏需要比较大电流,USB供电可能不足,请用外部电源供电(5V电源,接VOUT2即可).
2,本例程在LCD_Init函数里面(在ILI93xx.c),用到了printf,如果不初始化串口1,将导致液晶无法显示!!
3,LCD模块在开发板的LCD接口上面,必须靠右插
4,红外遥控器尾部默认带了一个小塑料片挡住电池供电,正常使用之前,请抽出这个塑料垫片.
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "remote.h"
- #include "string.h"
- //ALIENTEK Mini STM32开发板范例代码22
- //红外遥控实验
- u8 *in_pwd1=0;
- u8 *in_pwd2=0;
- u8 *in_pwd3=0;
- u8 *in_pwd4=0;
- u8 *in_pwd5=0;
- u8 *in_pwd6=0;
- u8 *str=0;
- int fall_time=0;
- u8 open_pwd[6]="123456";//初始密码
- u8 root_pwd[6]="666666";//管理员密码
- int in_time=0;
- int mykey=0;//控制in_time每按一个按键只加一次的坎儿
- void re_in(void);//重置/重新输入
- void root_h(void);//管理员函数
- int main(void)
- {
- u8 key;
- u8 t=0;
- delay_init(); //延时函数初始?
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- uart_init(9600); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init();
- Remote_Init(); //红外接收初始化
- POINT_COLOR=BLACK;//设置字体为红色
- LCD_ShowString(30,50,200,16,16,"---ordinary mode---");
- LCD_ShowString(30,70,200,16,16,"Please enter 6 password");
- //LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
- //LCD_ShowString(60,110,200,16,16,"2014/3/12");
- // LCD_ShowString(60,130,200,16,16,"KEYVAL:");
- // LCD_ShowString(60,150,200,16,16,"KEYCNT:");
- // LCD_ShowString(60,170,200,16,16,"SYMBOL:");
- LCD_ShowString(60,200,200,16,16,"PASSWD:");
- //LCD_ShowxNum(116,220,0,6,16,1);
-
- while(1)
- {
- key=Remote_Scan();
- if(key)
- {
-
- // LCD_ShowNum(116,130,key,3,16); //显示键值
- // LCD_ShowNum(116,150,RmtCnt,3,16); //显示按键次数
- switch(key)
- {
- case 0:str="ERROR";break;
- case 162:str="POWER";re_in();break;
- case 98:str="UP";break;
- case 2:str="PLAY";break;
- case 226:str="ALIENTEK";break;
- case 194:str="RIGHT";break;
- case 34:str="LEFT";break;
- case 224:str="VOL-";break;
- case 168:str="DOWN";break;
- case 144:str="VOL+";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;
- }
- // LCD_Fill(116,170,116+8*8,170+16,WHITE); //清楚之前的显示
- // LCD_ShowString(116,170,200,16,16,str); //显示SYMBOL
- if(RmtCnt==1&&mykey==0)
- {
- in_time++;
- //LCD_Fill(116,220,116+8*8,220+16,WHITE);//清除显示输入了多少位
- //LCD_ShowxNum(116,220,in_time,6,16,1);//显示输入了多少位
-
- if(in_time==1)
- {
- LCD_Fill(116,130,240,200+16,WHITE);//清除之前输入的密码以及open the door
-
- }
- 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:in_pwd1=str;LCD_ShowString(116,200,200,16,16,in_pwd1);delay_ms(200);LCD_ShowString(116,200,200,16,16,"*");break;
- case 2:in_pwd2=str;LCD_ShowString(116+8,200,200,16,16,in_pwd2);delay_ms(200);LCD_ShowString(116+8,200,200,16,16,"*");break;
- case 3:in_pwd3=str;LCD_ShowString(116+16,200,200,16,16,in_pwd3);delay_ms(200);LCD_ShowString(116+16,200,200,16,16,"*");break;
- case 4:in_pwd4=str;LCD_ShowString(116+24,200,200,16,16,in_pwd4);delay_ms(200);LCD_ShowString(116+24,200,200,16,16,"*");break;
- case 5:in_pwd5=str;LCD_ShowString(116+32,200,200,16,16,in_pwd5);delay_ms(200);LCD_ShowString(116+32,200,200,16,16,"*");break;
- case 6:in_pwd6=str;LCD_ShowString(116+40,200,200,16,16,in_pwd6);delay_ms(200);LCD_ShowString(116+40,200,200,16,16,"*");break;
- }
- }
-
- mykey=1;//设置为1防止再次进入
- }
- if(in_time==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])
- {
- LED1=0;
- fall_time=0;
- LCD_Fill(116,200,116+64,200+16,WHITE);//清除之前输入的密码
- LCD_ShowString(116,200,200,16,16,"Welcome");
- LCD_ShowString(116,130,200,16,16,"open the door!");
- }
- 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])
- {
- root_h();
- }
- else//配对失败
- {
- LED1=1;
- LCD_Fill(116,200,240,200+16,WHITE);//清除之前输入的密码
- LCD_ShowString(116,200,200,16,16,"wrong password");
- fall_time++;
- LED0=!LED0;//红灯闪烁提示
- delay_ms(300);
- LED0=!LED0;
- delay_ms(300);
- LED0=!LED0;
- delay_ms(300);
- LED0=!LED0;
- LCD_Fill(116,200,240,200+16,WHITE);//清除wrong password
- LED0=1;
- }
- in_time=0;
- }
- if(fall_time==5)
- {
- fall_time=0;
- LCD_Fill(116,200,240,200+16,WHITE);//清除准备显示锁定
- LCD_ShowString(60,200,200,16,16,"Has been locked please wait 1 min");
- fall_for_1min();
- LED0=1;
- LCD_Fill(116,200,240,200+16,WHITE);//清除Has been locked please wait 1 minute
- LCD_ShowString(60,200,200,16,16,"PASSWD:");
- }
-
- }else
- {
- delay_ms(10);
- mykey=0;//放手时,归零以便下次重新计数
- }
- t++;
- if(t==20)
- {
- t=0;
- //LED0=!LED0;
- }
- }
- }
- void re_in(void)//重置/重新输入
- {
- LCD_Fill(116,200,116+64,200+16,WHITE);//清除之前输入的密码
- //LCD_Fill(116,220,116+8*8,220+16,WHITE);
- LCD_ShowString(116,130,200,16,16,"close the door!");//提示关门
- delay_ms(800);
- LCD_Fill(116,130,240,200+16,WHITE);//清除之前输入的密码
- in_time=0;
- LED1=0;
- delay_ms(200);
- LED1=1;
- }
- void root_h(void)//管理员用户函数
- {
- u8 key;
- in_time=0;
- mykey=0;
- in_pwd1="0";
- in_pwd2="0";
- in_pwd3="0";
- in_pwd4="0";
- in_pwd5="0";
- in_pwd6="0";
-
- LED1=!LED1;//绿灯闪烁提示
- delay_ms(100);
- LED1=!LED1;
- delay_ms(100);
- LED1=!LED1;
- delay_ms(100);
- LED1=!LED1;
- LCD_Fill(30,50,240,110,WHITE);// 清除普通用户界面提示
- LCD_ShowString(60,50,200,16,16,"---root mode---");
- LCD_ShowString(60,70,200,16,16,"you can change");
- LCD_ShowString(60,90,200,16,16,"password here");
- LCD_Fill(0,200,240,200+16,WHITE);//清除输入密码提示以及密码
- LCD_ShowString(60,200,200,16,16,"NEWPWD:");//提示输入新密码
- do
- {
- //LCD_ShowString(60,70,200,16,16,"REMOTE TEST");
- //LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
- //LCD_ShowString(60,110,200,16,16,"2014/3/12");
- key=Remote_Scan();
- if(key)
- {
- //LCD_ShowNum(116,130,key,3,16); //显示键值
- //LCD_ShowNum(116,150,RmtCnt,3,16); //显示按键次数
- switch(key)
- {
- case 0:str="ERROR";break;
- case 162:str="POWER";re_in();break;
- case 98:str="UP";break;
- case 2:str="PLAY";break;
- case 226:str="ALIENTEK";break;
- case 194:str="RIGHT";break;
- case 34:str="LEFT";break;
- case 224:str="VOL-";break;
- case 168:str="DOWN";break;
- case 144:str="VOL+";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;
- }
- //LCD_Fill(116,170,116+8*8,170+16,WHITE); //清楚之前的显示
- //LCD_ShowString(116,170,200,16,16,str); //显示SYMBOL
- if(RmtCnt==1&&mykey==0)
- {
- in_time++;
- //LCD_Fill(116,220,116+8*8,220+16,WHITE);//清除显示输入了多少位
- //LCD_ShowxNum(116,220,in_time,6,16,1);//显示输入了多少位
-
- if(in_time==1)
- {
- LCD_Fill(116,200,116+64,200+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)
- {
- case 1:in_pwd1=str;open_pwd[0]=*str;LCD_ShowString(116,200,200,16,16,in_pwd1);delay_ms(200);LCD_ShowString(116,200,200,16,16,"*");break;//第一次输入
- case 2:in_pwd2=str;open_pwd[1]=*str;LCD_ShowString(116+8,200,200,16,16,in_pwd2);delay_ms(200);LCD_ShowString(116+8,200,200,16,16,"*");break;//第二次输入
- case 3:in_pwd3=str;open_pwd[2]=*str;LCD_ShowString(116+16,200,200,16,16,in_pwd3);delay_ms(200);LCD_ShowString(116+16,200,200,16,16,"*");break;//第三次输入
- case 4:in_pwd4=str;open_pwd[3]=*str;LCD_ShowString(116+24,200,200,16,16,in_pwd4);delay_ms(200);LCD_ShowString(116+24,200,200,16,16,"*");break;//第四次输入
- case 5:in_pwd5=str;open_pwd[4]=*str;LCD_ShowString(116+32,200,200,16,16,in_pwd5);delay_ms(200);LCD_ShowString(116+32,200,200,16,16,"*");break;//第五次输入
- case 6:in_pwd6=str;open_pwd[5]=*str;LCD_ShowString(116+40,200,200,16,16,in_pwd6);delay_ms(200);LCD_ShowString(116+40,200,200,16,16,"*");break;//第六次输入
- }
- }
-
- if(in_pwd1=="6"&&in_pwd2=="6"&&in_pwd3=="6"&&in_pwd4=="6"&&in_pwd5=="6"&&in_pwd6=="6")
- {
- LCD_Fill(0,200,240,200+16,WHITE);//清除输入新密码提示以及新密码
- LCD_ShowString(60,200,200,16,16,"coudle not same as root");
- LCD_ShowString(60,220,200,16,16,"password has been back to ");
- LCD_ShowString(60,240,200,16,16,"123456");
- open_pwd[0]=*"1";
- open_pwd[1]=*"2";
- open_pwd[2]=*"3";
- open_pwd[3]=*"4";
- open_pwd[4]=*"5";
- open_pwd[5]=*"6";
- delay_ms(1500);
- LCD_Fill(0,200,240,260,WHITE);
- }
-
- mykey=1;//设置为1防止再次进入
- }
-
- if(in_time==6)
- {
- in_time=0;
- LCD_Fill(0,200,240,200+16,WHITE);//清除输入新密码提示以及新密码
- LCD_ShowString(60,200,200,16,16,"reset complete");
- delay_ms(700);
- LCD_Fill(60,50,240,110,WHITE);// 清除管理员用户界面提示
- LCD_ShowString(60,50,200,16,16,"---ordinary mode---");
- LCD_Fill(0,200,240,200+16,WHITE);//清除Password reset complete
- LCD_ShowString(60,200,200,16,16,"PASSWD:");
- key=82;
- }
-
- }
- else
- {
- delay_ms(10);
- mykey=0;//放手时,归零以便下次重新计数
- }
- }while(key!=82);
- LCD_Fill(60,50,240,110,WHITE);// 清除管理员用户界面提示
- LCD_ShowString(60,50,200,16,16,"---ordinary mode---");
- LCD_Fill(0,200,240,200+16,WHITE);//清除Password reset complete
- LCD_ShowString(60,200,200,16,16,"PASSWD:");
- }
复制代码
所有资料51hei提供下载:
红外遥控器密码锁.rar
(378.81 KB, 下载次数: 65)
|