修正这个C51代码,使数码管左面第一位显示矩阵按键的键值: #include "reg52.h" #include "intrins.h" #define uchar unsigned char uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数码管共阴 uchar code colcode[ 4 ]={ 0xfe, 0xfd, 0xfb, 0xf7}; //列依次为0 void delayms(uchar ms) { uchar i; while(ms--) for(i=0;i<123;i++); } uchar key_scan(void)//P1口连矩阵键盘 { uchar temp, row, column, i; P1=0XF0; //行为1,列为0 temp=P1&0XF0; if(temp!=0xf0) { delayms(10); temp=P1&0XF0; if(temp!=0xf0)//发生变化 { switch(temp) { case 0x70: row=3; break; case 0xb0: row=2; break; case 0xd0: row=1; break; case 0xe0: row=0; break; default: break; } for(i=0; i<4; i++) { P1=colcode[i]; temp=P1&0XF0; temp=~temp; if(temp!=0x0f)column=i; } return row*4+column ; } } else P1=0XFF; return 16; } void main(void) { uchar key_val; while(1) { key_val=keyscan(); P0=seg[key_val];//数码管显示键值 delayms(50); } }
时间: 2024-03-18 18:41:08 浏览: 78
在主函数中调用的函数名和定义的函数名不一致,需要将key_scan函数改为keyscan。同时,在主函数中需要将数码管左侧第一位对应的段码赋值给P0的高四位。修改后的代码如下:
```
#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
uchar code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //数码管共阴
uchar code colcode[4] = { 0xfe, 0xfd, 0xfb, 0xf7 }; //列依次为0
void delayms(uchar ms)
{
uchar i;
while(ms--)
for(i=0; i<123; i++);
}
uchar keyscan(void)//P1口连矩阵键盘
{
uchar temp, row, column, i;
P1 = 0XF0; //行为1,列为0
temp = P1 & 0XF0;
if(temp != 0xf0)
{
delayms(10);
temp = P1 & 0XF0;
if(temp != 0xf0)//发生变化
{
switch(temp)
{
case 0x70: row = 3; break;
case 0xb0: row = 2; break;
case 0xd0: row = 1; break;
case 0xe0: row = 0; break;
default: break;
}
for(i = 0; i < 4; i++)
{
P1 = colcode[i];
temp = P1 & 0XF0;
temp = ~temp;
if(temp != 0x0f) column = i;
}
return row * 4 + column;
}
}
else P1 = 0XFF;
return 16;
}
void main(void)
{
uchar key_val;
while(1)
{
key_val = keyscan();
if(key_val == 0) P0 = (P0 & 0x0f) | (seg[0] << 4);//数码管显示矩阵按键的键值
else P0 = (P0 & 0x0f) | (seg[key_val] << 4);//数码管显示键值
delayms(50);
}
}
```
阅读全文