weixin_50697999
2022-05-15 06:35
采纳率: 80%
浏览 17

stc15+LCD1602+4x4矩阵键盘的问题



```c

//当按键按下 0329的时候,正确显示。为什么一样的代码,输入1129却不行?永远无法和RecordDate_2匹配上

//单片机源程序如下:

#include <stc15.h>
#include <string.h>
#include "LCD1602.h"
#include "delay.h"
#include "MatrixKey.h"
//#include "timer0.h"
//#include "key.h"


void main(void)
{
         unsigned char Key_Number='x';
//        extern unsigned char Key_Flag;
//        char RecordDate[6]="xxxxx";
//        const char RecordDate_1[6]="03-29";
//        const char RecordDate_2[6]="11-19";
        char RecordDate[6]={'\0','\0','\0','\0','\0','\0'};
const char RecordDate_1[6]={'0','3','-','2','9','\0'};
        const char RecordDate_2[6]={'1','1','-','2','9','\0'};
        unsigned char i=0,j=0,k=0;
        P0M0=0X00;
        P0M1=0X00;
        P1M0=0X00;
        P1M1=0X00;
        P2M0=0X00;
        P2M1=0X00;
        P3M0=0X00;
        P3M1=0X00;
//        Timer0_Init();
        LCD_Init();
        LCD_ShowString(1,1,"   Enter a Date:");
        while(1)
        {        
                for(i=0;i<6;)
                {
                        if(i==2)
                        {
                                RecordDate[i]='-';
                                LCD_ShowChar(2,i+1,RecordDate[i]);
                                Key_Number='x';
                                i++;
                                continue;
                        }
                        if(i==5)
                        {
                                RecordDate[i]='\0';
                                Key_Number='x';
                                i++;
                                break;
                        }
                        
                        Key_Number=MatrixKey();
                        
                        if('0'<=Key_Number&&Key_Number<='9'&&Key_Number!='*'&&Key_Number!='#')        //如果按下的按键编码为0-9(ASCII码),输入日期
                        {
                                RecordDate[i]=Key_Number;
                                Key_Number='x';
                                LCD_ShowChar(2,i+1,RecordDate[i]);
                                i++;                        
                        }
                        if(Key_Number=='*')
                        {
                                LCD_ShowString(2,1,"                ");
                                Key_Number='x';
                                i=0;
                        }
                        if(Key_Number=='#')
                        {                        
                                if(!strcmp(RecordDate,RecordDate_1)||!(RecordDate,RecordDate_2))
                                {
                                        LCD_ShowString(2,1,"              OK");
                                        delay(5000);
                                       
                                        if(!strcmp(RecordDate,RecordDate_1))
                                        {
                                                LCD_ShowString(1,1,"Date:           ");
                                                LCD_ShowString(1,6,RecordDate);
                                                LCD_ShowString(2,1,"         Playing");
                                                delay(50000);
                                                LCD_ShowString(1,1,"Enter a Date:   ");
                                                LCD_ShowString(2,1,"                ");
                                                
                                                //动作1-P30=1;
                                                
                                                Key_Number='x';
                                                i=0;
                                                for(j=0;j<6;j++)
                                                {
                                                        RecordDate[j]='\0';
                                                }
                                        }
                                        if(!strcmp(RecordDate,RecordDate_2))
                                        {
                                                LCD_ShowString(1,1,"Date:           ");
                                                LCD_ShowString(1,6,RecordDate);
                                                LCD_ShowString(2,1,"         Playing");
                                                delay(50000);
                                                LCD_ShowString(1,1,"Enter a Date:   ");
                                                LCD_ShowString(2,1,"                ");
                                                
                                                //动作2-P31=1;
                                                
                                                Key_Number='x';
                                                i=0;
                                                for(j=0;j<6;j++)
                                                {
                                                        RecordDate[j]='\0';
                                                }
                                        }
                                                

                                }
                                else
                                {
                                        LCD_ShowString(2,1,"           Error");
                                        delay(5000);
                                        LCD_ShowString(2,1,"                ");
                                        Key_Number='x';
                                        i=0;
                                        for(k=0;k<6;k++)
                                        {
                                                RecordDate[k]='\0';
                                        }
                                }
                        }               
                }                        
        }        
}

```

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

相关推荐 更多相似问题