overdoes_218 2024-03-04 22:00 采纳率: 0%
浏览 24
已结题

51单片机怎么把每次输入键值保存到数组作为我的确认键的判断条件呢

51单片机怎么把每次输入键值保存到数组作为我的确认键的判断条件呢?求解

#include<reg51.h>
sbit lcd_RS=P2^0;
sbit lcd_RW=P2^1;
sbit lcd_E=P2^2;
char key[]={0,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30};  //键值
char key2[]="OK";
char table[]="password";
char table1[]="input:";

delay(unsigned int x)          
{       
    unsigned int i;
    for(x=0;x<100;x++)
        for(i=0;i<x;i++);
}

write_com(unsigned char com )     
{
    while(lcd_busy());
    lcd_RS=0;
    lcd_RW=0;
    delay(200);
    P0=com;
    delay(200);
    lcd_E=1;
    delay(200);
    lcd_E=0;
}
write_dat(unsigned char dat ) 
{
    while(lcd_busy());
    lcd_RS=1;
    lcd_RW=0;
    P0=dat;
    delay(200); 
    lcd_E=1;
    delay(200);
    lcd_E=0;
}
lcd_busy()
{ 
    unsigned char temp;
    lcd_RS=0;
    lcd_RW=1;
    delay(20);
    P0=0xff;
    delay(20);
    lcd_E=1;
    delay(20);
    temp=P0;
    delay(20);
    lcd_E=0;
    return(temp&0x80);
}
init()                       
{ 
    write_com(0x01);
    write_com(0x38);
    write_com(0x0c);
    write_com(0x06);
} 
juxingjianpan()                      
{ 
    unsigned char KeyNumber,x,y;
    P1=0x0f;              
    x=P1;
    P1=0xf0;
    y=P1;
    switch(x+y)
    { 
        case 0xee:      KeyNumber=1;break;
        case 0xde:      KeyNumber=2;break;
        case 0xbe:      KeyNumber=3;break;
        case 0x7e:      KeyNumber=4;break;
        case 0xed:      KeyNumber=5;break;
        case 0xdd:      KeyNumber=6;break;
        case 0xbd:      KeyNumber=7;break;
        case 0x7d:      KeyNumber=8;break;
        case 0xeb:      KeyNumber=9;break;         
        case 0xdb:      KeyNumber=10;break;
        case 0xbb:        KeyNumber=11;break;
    }
    return KeyNumber;
}                    
void main() {
unsigned int i,KeyNum,password;
    init();  
    write_com(0x80);
    for(i=0;i<8;i++)
    {     
        write_dat(table[i]);  
    }
    write_com(0xc0);
    for(i=0;i<6;i++)
    {     
        write_dat(table1[i]);  
    }
    while(1)
    { 
         static unsigned char i = 0,x,y;
        write_com(0xc6 + i);
        KeyNum=juxingjianpan();
        if(KeyNum)
        {      
            if(KeyNum<=10)
            {   if(x<=3)   
                {write_dat(key[KeyNum]);
                 delay(5);              
                 i++;
                 }
                 x++;
            }

                        
            if(KeyNum==11)     //确认键
            {    
                if(KeyNum==11)
                {  
                   write_com(0x01);
                   delay(20);
                   write_com(0x8a);
                   for(y=0;y<2;y++)
                   {write_dat(key2[y]);
                    delay(5);
                    }
                 }
            }      
        }
    }
}

  • 写回答

13条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-04 22:02
    关注

    引自免费微信小程序:皆我百晓生

    在您给出的51单片机C语言代码中,你已经定义了一个数组key[]来存储可能的键值。为了将每次用户输入的键值保存到数组中作为确认键的判断条件,你需要创建一个新的数组来存储用户的输入序列,并在检测到确认键时停止记录输入。

    以下是对您现有代码进行修改以实现这一功能的建议:

    #define PASSWORD_LENGTH 4 // 定义密码长度
    
    unsigned char input_buffer[PASSWORD_LENGTH]; // 创建一个缓冲区用于存储输入的按键值
    unsigned char input_index = 0; // 用于记录当前输入的索引位置
    
    void main() {
        unsigned int i, KeyNum, password;
        init();  
        write_com(0x80);
        for(i=0;i<8;i++)
        {     
            write_dat(table[i]);  
        }
        write_com(0xc0);
        for(i=0;i<6;i++)
        {     
            write_dat(table1[i]);  
        }
        
        while(1)
        { 
            KeyNum = juxingjianpan();
            
            if(KeyNum)
            {      
                if(KeyNum <= 10 && input_index < PASSWORD_LENGTH) // 只有当输入未满且是有效键值时才写入缓冲区
                {   
                    input_buffer[input_index++] = key[KeyNum];
                    write_com(0xc6 + input_index - 1); // 显示已输入的字符
                    write_dat(key[KeyNum]);
                    delay(5);              
                }
    
                if(KeyNum == 11) // 确认键
                {    
                    if(input_index == PASSWORD_LENGTH) // 检查是否已输入指定数量的字符
                    {
                       // 此处可以添加密码验证逻辑,比如与预设密码对比
                       // ...
                       
                       // 清空屏幕并显示"OK"
                       write_com(0x01);
                       delay(20);
                       write_com(0x8a);
                       for(y=0;y<2;y++)
                       {   
                           write_dat(key2[y]);
                           delay(5);
                       }
                       
                       // 重置输入索引以便下一次输入
                       input_index = 0;
                     }
                }      
            }
        }
    }
    

    这段代码中我做了一下几点修改和补充:

    1. 添加了input_buffer数组用于存储用户输入的按键序列。
    2. 添加了input_index变量记录已输入的字符个数。
    3. 在按下有效按键(非确认键)时,将其值存入input_buffer,并在LCD上显示出来。
    4. 当检测到确认键被按下时,检查input_index是否等于预设的密码长度(这里是4),如果满足条件,则可以执行密码验证逻辑;否则不作处理。
    5. 验证后清空屏幕显示"OK",并重置input_index以便接受下一轮输入。
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月5日
  • 创建了问题 3月4日

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆