overdoes_218 2024-03-06 20:55 采纳率: 0%
浏览 3
已结题

关于#单片机#的问题:这个静态变量只能初始化一次记录位置在我的仿真里面是c6+i来改变1602显示的位置,但是清除不掉标志位应该要怎么写好

static unsigned char i = 0;
这个静态变量只能初始化一次记录位置在我的仿真里面是c6+i来改变1602显示的位置,但是清除不掉标志位应该要怎么写好?

#include<reg51.h>
#include<string.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,0xbb,0x7b};  //键值
char key2[]="OK";
char key3[]="error";
char table[]="password";
char table1[]="input:";
char passwordInput[4]; 

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;
        case 0x7b:        KeyNumber=12;break;
    }
    return KeyNumber;
}                    
void main() {
    unsigned int i, KeyNum, passwordLen = 0;
    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 = 0, y;
        write_com(0xc6 + i);
        KeyNum = juxingjianpan();
        if(KeyNum)
        {      
            if(KeyNum <= 10)
            {   
                if(x <= 3)   
                {
                    write_dat(key[KeyNum]);
                    delay(5);
                    passwordInput[passwordLen++] = key[KeyNum]; 
                    i++;
                }
                x++;                
            }
 
          else if(KeyNum == 11)     // 确认键
            {    
                
                if (strcmp(passwordInput, "1234") == 0) 
                { 
                    write_com(0x01);
                    delay(20);
                    write_com(0x8a);
                    for(y = 0; y < 2; y++)
                    {
                        write_dat(key2[y]);
                        delay(5);
                    }
                } 
                 else 
                {
                    write_com(0x01);
                    delay(20);
                    write_com(0x8a);
                    for(y = 0; y < 5; y++)
                    {
                        write_dat(key3[y]);
                        delay(5);
                    }
                }
                memset(passwordInput, 0, sizeof(passwordInput));
                passwordLen = 0;
                x = 0;
                i=0;
            }
        else if (KeyNum == 12) 
        {
            
            memset(passwordInput, 0, sizeof(passwordInput));
            passwordLen = 0;
            x = 0;
            i=0;
            
            write_com(0x01); 
            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]);  
            }
        }
    }
}

}

  • 写回答

3条回答 默认 最新

  • GISer Liu 2024-03-06 21:03
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    根据你提供的代码和描述,你的问题似乎是关于静态变量 i 在显示密码输入时无法正确清除的问题。你的程序中有两个地方定义了静态变量 i,一个是在 main() 函数的外部,另一个是在 while(1) 循环内部的 static unsigned char i = 0
    问题出现在 while(1) 循环内部的 static unsigned char i = 0
    的定义。因为这个变量定义在循环内部,每次循环都会重新初始化为0,而不是保持上一次循环结束时的值。这导致在输入密码时,每次按下确认键时都会重新从显示屏的第一个字符位置开始显示密码,而不是清除之前输入的密码。
    解决方案是将 static unsigned char i = 0 的定义从 while(1) 循环内部移出来,放在 main() 函数内部或者移动到 while(1) 循环外部。这样就可以保持 i
    变量的状态,确保在输入密码时能正确记录位置。
    以下是修改后的部分代码:
    cCopy code

    void main() {
    unsigned int i, KeyNum, passwordLen = 0;
    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]);
    }
    static unsigned char i = 0, x = 0, y; // 将静态变量定义移到while循环外部
    while(1) {
    write_com(0xc6 + i);
    KeyNum = juxingjianpan();
    if(KeyNum) {
    if(KeyNum <= 10) {
    if(x <= 3) {
    write_dat(key[KeyNum]);
    delay(5);
    passwordInput[passwordLen++] = key[KeyNum];
    i++;
    }
    x++;
    } else if(KeyNum == 11) { // 确认键
    if (strcmp(passwordInput, "1234") == 0) {
    write_com(0x01);
    delay(20);
    write_com(0x8a);
    for(y = 0; y < 2; y++) {
    write_dat(key2[y]);
    delay(5);
    }
    } else {
    write_com(0x01);
    delay(20);
    write_com(0x8a);
    for(y = 0; y < 5; y++) {
    write_dat(key3[y]);
    delay(5);
    }
    }
    memset(passwordInput, 0, sizeof(passwordInput));
    passwordLen = 0;
    x = 0;
    i = 0;
    } else if (KeyNum == 12) {
    memset(passwordInput, 0, sizeof(passwordInput));
    passwordLen = 0;
    x = 0;
    i = 0;
    write_com(0x01);
    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]);
    }
    }
    }
    }
    }
    

    在这个修改后的代码中,我将 static unsigned char i = 0, x = 0, y; 移动到 while(1) 循环外部,确保了 i 变量的状态在循环中得以保持。这样就可以正确地记录位置,清除密码输入的标志位。

    如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订

    用户答题指南

    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥30 解决github RealtimeTTS项目的打包,别用ai回答,没用
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?
  • ¥15 如何用QDomDocument读取内容为空格的xml数据
  • ¥15 请阅读下面代码,帮我修改下代码
  • ¥15 关于#microsoft#的问题:电脑启动后不显示桌面图标和窗口,除任务栏外无法操作任何东西
  • ¥15 如何输入百度,显示本地下载的html文件页面,地址栏还显示百度的地址
  • ¥15 通过kinect制作换装程序但是服装不贴合(标签-ar)