待解决疑问:
1、矩阵键盘:
问题描述:
(代码1)在使用矩阵键盘和LCD1602进行密码锁的程序编写的时候发现如果矩阵键盘的判断语句内没有添加延时函数,LCD1602上面就会一直显示0000的数值,按下矩阵按键没有“数值返回”,即为LCD1602上面没有变化。(代码2)但是当在矩阵键盘的函数内部加入延时函数后,LCD1602初始化后显示00255的数值,并且按下按键出现反应,在LCD1602上面会返回响应的数值。
(代码1)
#include <REGX52.H>
#include "delay.H"
unsigned char Matrix(void)
{
unsigned char Matrix_Number;
P1=0XFF;
P1_7=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=0;}
if(P1_2==0){while(P1_2==0);Matrix_Number=1;}
if(P1_1==0){while(P1_1==0);Matrix_Number=2;}
if(P1_0==0){while(P1_0==0);Matrix_Number=3;}
P1=0XFF;
P1_6=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=4;}
if(P1_2==0){while(P1_2==0);Matrix_Number=5;}
if(P1_1==0){while(P1_1==0);Matrix_Number=6;}
if(P1_0==0){while(P1_0==0);Matrix_Number=7;}
P1=0XFF;
P1_5=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=8;}
if(P1_2==0){while(P1_2==0);Matrix_Number=9;}
if(P1_1==0){while(P1_1==0);Matrix_Number=10;}
if(P1_0==0){while(P1_0==0);Matrix_Number=11;}
P1=0XFF;
P1_4=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=12;}
if(P1_2==0){while(P1_2==0);Matrix_Number=13;}
if(P1_1==0){while(P1_1==0);Matrix_Number=14;}
if(P1_0==0){while(P1_0==0);Matrix_Number=15;}
return Matrix_Number;
}
(代码2)
#include <REGX52.H>
#include "delay.H"
unsigned char Matrix(void)
{
unsigned char Matrix_Number;
P1=0XFF;
P1_7=0;
if(P1_3==0){while(P1_3==0);Delay(20);Matrix_Number=0;}
if(P1_2==0){while(P1_2==0);Matrix_Number=1;}
if(P1_1==0){while(P1_1==0);Matrix_Number=2;}
if(P1_0==0){while(P1_0==0);Matrix_Number=3;}
P1=0XFF;
P1_6=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=4;}
if(P1_2==0){while(P1_2==0);Matrix_Number=5;}
if(P1_1==0){while(P1_1==0);Matrix_Number=6;}
if(P1_0==0){while(P1_0==0);Matrix_Number=7;}
P1=0XFF;
P1_5=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=8;}
if(P1_2==0){while(P1_2==0);Matrix_Number=9;}
if(P1_1==0){while(P1_1==0);Matrix_Number=10;}
if(P1_0==0){while(P1_0==0);Matrix_Number=11;}
P1=0XFF;
P1_4=0;
if(P1_3==0){while(P1_3==0);Matrix_Number=12;}
if(P1_2==0){while(P1_2==0);Matrix_Number=13;}
if(P1_1==0){while(P1_1==0);Matrix_Number=14;}
if(P1_0==0){while(P1_0==0);Matrix_Number=15;}
return Matrix_Number;
}
2.按键控制LED流水灯模式
问题描述:
本次采用了定时器中断的方式进行编写,但是如果在中断函数内部套用2个if判断函数并列的时候其内部的移位语句无法执行,其他语句(P2_0=!P2_0)正常执行,但是>>语句没有执行。值得注意的是,如果将if语句只保留一个的话,其内部的移位语句也可以正常进行。出现该问题的代码如下:
#include <REGX52.H>
#include "Tirmer0.h"
#include "Key.h"
#include "delay.h"
#include <intrins.H>
#include "lcd1602.h"
unsigned char Key_Number,LEDMode;
void main()
{
Timer0_Init();
P2=0XFE;
while(1)
{
Key_Number=Key();
}
}
void Timer0_Rountine(void) interrupt 1
{
static unsigned int count;
char i;
TH0=0XFC;
TL0=0X18;
count++;
if(count>1000)
{
count=0;
if(Key_Number==1)
{
i++;
P2_0=!P2_0;
P1=~(0X01<<i);
if(i>7){i=0;}
}
if(Key_Number==2)
{
i--;
P2_1=!P2_1;
P1=~(0X01<<i);
if(i<0){i=7;}
}
}
}
按键的代码如下:
#include <REGX52.H>
#include "DELAY.H"
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^3;
sbit K4=P3^4;
unsigned char Key(void)
{
unsigned char KeyNum;
if(K1==0){Delay(20);while(K1==0);Delay(20);KeyNum=1;}
if(K2==0){Delay(20);while(K2==0);Delay(20);KeyNum=2;}
if(K3==0){Delay(20);while(K3==0);Delay(20);KeyNum=3;}
if(K4==0){Delay(20);while(K4==0);Delay(20);KeyNum=4;}
return KeyNum;
}