PD2-PD5 和 PB5-PB7控制矩阵键盘
谁帮我看下,下面的程序有什么问题
#include
#include
#define uchar unsigned char
#define uint unsigned int
#pragma data:code
const uchar Duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
const uchar Numtk[]={10,11,12,3,6,9,2,5,8,1,4,7,16};
const uchar Act[]={0x06,0x05,0x03};
void delay_ms(uint n)
{
uint i,j;
for(i=0;i
for(j=0;j<562;j++);
}
uchar key_press() //判断是否有键按下
{
uchar in;
PORTD=PORTD&0XC3; //PD2-PD5输出0
PORTB=PORTB|0XE0; //PB5-PB7上拉有效
in=PINB;
in=in>>5;
in=in&0x07;
if(in!=0x07)
{
delay_ms(10); //延时再判断
if(in!=0x07)
return 1;
}
else
return 0;
}
uchar key_scan() //有键按下时,判断按下了那个键
{
uchar i,k,j;
for(i=0;i<4;i++)
{
PORTD=PORTD|0X3C; //先把PD2-PD5设置为1
PORTD=PORTD&(~BIT(i+2));//PD2-PD5分别置0
j=PINB;
j=j>>5;
j=j&0x07;
for(k=0;k<3;k++) //判断PB5-PB7哪个为0
{
if(j==Act[k])
return (3*i+k);
}
}
return 12;
}
uchar key,num;
void main()
{
DDRD=DDRD|BIT(1); //打开PD1引脚的指示灯
PORTD=PORTD&(~BIT(1));
DDRB=DDRB|BIT(4); //打开第一位数码管
PORTB=PORTB|BIT(4);
DDRA=0xff; //PA0-PA8为输出0,关闭段选
PORTA=0x00;
DDRD=DDRD|0X3C; //PD2-PD5设置为输出0
//PORTD=PORTD&0XC3;
DDRB=DDRB&0X1F; //PB5-PB7设置为输入,上拉有效
//PORTB=PORTB|0Xe0;
while(1)
{
if(key_press())
{
num=key_scan();
key=Numtk[num];
PORTA=Duan[key];
delay_ms(1);
}
}
}
楼主,问问题,怎么老是不说,程序出什么问题了,什么现象?
也好让大家有针对的看,要大家从头到尾给你分析?????