本人在做一个电子秤设计,想通过键盘输入单价(单价只能输入三位整数), 显示在LCD1602上,但是,我按键盘上的按键,LCD没有任何的显示(LCD的编程应该是没问题的,因为可以显示一些初始化的内容)下面是本人写的键盘程序,请问有没有大神能够之处错误。
//键盘扫描
uchar keyscan(void)
{
uchar temp2;
//扫描第一行
P1 = 0xfe;
temp2 = P1; //读取P1口的当前状态
temp2 = temp2 & 0xf0; //判断是否有键按下
if (temp2 != 0xf0)
{
delay_ms (10); //延时10ms,去抖动
temp2 = P1;
temp2 = temp2 & 0xf0;
if (temp2 != 0xf0) //确实有键按下
{
temp2 = P1;
switch (temp2)
{
case 0xee: return keycode = 7; break;
case 0xde: return keycode = 8; break;
case 0xbe: return keycode = 9; break;
case 0x7e: return keycode = 15; break; //复位键按下标志位
}
while (temp2 != 0xf0) //确定已经松开按键
{
temp2 = P1;
temp2 = temp2 & 0xf0;
}
}
}
//扫描第二行
P1 = 0xfd;
temp2 = P1; //读取P1口的当前状态
temp2 = temp2 & 0xf0; //判断是否有键按下
if (temp2 != 0xf0)
{
delay_ms (10); //延时10ms,去抖动
temp2 = P1;
temp2 = temp2 & 0xf0;
if (temp2 != 0xf0) //确实有键按下
{
temp2 = P1;
switch (temp2)
{
case 0xed: return keycode = 4; break;
case 0xdd: return keycode = 5; break;
case 0xbd: return keycode = 6; break;
case 0x7d: return keycode = 14; break; //清除键按下标志位
}
while (temp2 != 0xf0) //确定已经松开按键
{
temp2 = P1;
temp2 = temp2 & 0xf0;
}
}
}
//扫描第三行
P1 = 0xfb;
temp2 = P1; //读取P1口的当前状态
temp2 = temp2 & 0xf0; //判断是否有键按下
if (temp2 != 0xf0)
{
delay_ms (10); //延时10ms,去抖动
temp2 = P1;
temp2 = temp2 & 0xf0;
if (temp2 != 0xf0) //确实有键按下
{
temp2 = P1;
switch (temp2)
{
case 0xeb: return keycode = 1; break;
case 0xdb: return keycode = 2; break;
case 0xbb: return keycode = 3; break;
case 0x7b: return keycode = 13; break; //去皮键按下标志位
}
while (temp2 != 0xf0) //确定已经松开按键
{
temp2 = P1;
temp2 = temp2 & 0xf0;
}
}
}
//扫描第四行
P1 = 0xf7;
temp2 = P1; //读取P1口的当前状态
temp2 = temp2 & 0xf0; //判断是否有键按下
if (temp2 != 0xf0)
{
delay_ms (10); //延时10ms,去抖动
temp2 = P1;
temp2 = temp2 & 0xf0;
if (temp2 != 0xf0) //确实有键按下
{
temp2 = P1;
switch (temp2)
{
case 0xe7: return keycode = 0; break;
case 0xd7: return keycode = 10; break; //小数点键按下标志位
case 0xb7: return keycode = 11; break; //#
case 0x77: return keycode = 12; break; //输入键按下标志位
}
while (temp2 != 0xf0) //确定已经松开按键
{
temp2 = P1;
temp2 = temp2 & 0xf0;
}
}
}
return keycode;
}
//键盘处理
void KeyPress (void)
{
uint8 i, temp;
uint8 count = 0;
do
{
P1 = 0xf0;
if (P1 != 0xf0)
{
keyscan ();
while (temp != 0xf0); //确定按键已经松开
{
temp = P1;
temp = temp & 0xf0;
}
if (keycode>=0 && keycode<=9)
{
keynum[count] = keycode; //把按键值保存在数组keynum[]中
lcd_write_com (0x80+0x40+5+count); //移位显示
lcd_write_data (keynum[count] + 0x30);
count++;
}
if (keycode == 15) //复位键
{
lcd_write_com (0x01); //清屏处理
Price = 0;
}
if (keycode == 14) //清除键
{
count = count--;
lcd_write_com (0x80+0x40+5+count);
lcd_write_data (0x20); //0x20为空格地址
}
if (keycode == 13) //去皮键
{
Get_Pizhong ();
}
if (keycode == 12) //输入键
{
goto cal_Price;
}
if (keycode == 11) //无定义键
{
BUZZER = 0;
delay_ms (100);
BUZZER = 1;
}
}
}
while (count <= 2);
if (count >= 2)
{
goto cal_Price;
}
cal_Price:
for (i=0; i<count; i++)
{
Price = Price*10 + keynum[i];
if (i == count)
count = 0;
}
}
|
感谢分享
uchar keyscan(void); 。。。。。。 keyscan (); while (temp != 0xf0); //确定按键已经松开 。。。。。。。。。。。。。。。。。。。。。。。 返回值没用?