矩阵按键连接P2口 LED灯P1口,但是程序有问题,按键没反应,然后L2 L3 L4 灯不亮,其余都亮,在线等
#include
#define uchar unsigned char
#define uint unsigned int
#define CLR_Led1 P1OUT&=~BIT4;
#define SET_Led1 P1OUT|=BIT4;
#define CLR_Led2 P1OUT&=~BIT5;
#define SET_Led2 P1OUT|=BIT5;
#define CLR_Led3 P1OUT&=~BIT6;
#define SET_Led3 P1OUT|=BIT6;
#define CLR_Led4 P1OUT&=~BIT7;
#define SET_Led4 P1OUT|=BIT7;
uchar GetKey();
void Delay1ms(uint);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
uchar key,disp;
P1DIR=0xff;
// P1OUT=0x00;//全亮
P1OUT=0xf0;//全灭
while (1)
{
// key='2';
key=GetKey();
if(key>=0x30 && key<=0x39)
{ disp=key-0x30;
Delay1ms(3);
}
if (key=='F')
{P1OUT |=0xf0;// 灭灯
disp=0xf0;//清零
//break;
continue;
}
P1OUT&=0xf0;//
// key=key|0xf0;
P1OUT|=disp;
CLR_Led1;
Delay1ms(30);
SET_Led1;
}
}
uchar GetKey()
{
P2DIR=0x0f;
P2OUT=0x0e;//扫描第一行
_NOP();_NOP();
//P1DIR &=0x0f;//读列
if((P2IN & 0xf0)== 0xe0)return'0';
if((P2IN & 0xf0)== 0xd0)return'1';
if((P2IN & 0xf0)== 0xb0)return'2';
if((P2IN & 0xf0)== 0x70)return'3';
P2DIR=0x0f;
P2OUT=0x0d;//扫描第二行
_NOP();_NOP();
P2DIR &=0x0f;//读列
if((P2IN & 0xf0)== 0xe0)return'4';
if((P2IN & 0xf0)== 0xd0)return'5';
if((P2IN & 0xf0)== 0xb0)return'6';
if((P2IN & 0xf0)== 0x70)return'7';
P2DIR=0xff;
P2OUT=0xfb;//扫描第三行
_NOP();_NOP();
P2DIR &=0x0f;//读列
if((P2IN & 0xf0)== 0xe0)return'8';
if((P2IN & 0xf0)== 0xd0)return'9';
if((P2IN & 0xf0)== 0xb0)return'A';
if((P2IN & 0xf0)== 0x70)return'B';
P2DIR=0xff;
P2OUT=0xf7;//扫描第四行
_NOP();_NOP();
P2DIR &=0x0f;//读列
if((P2IN & 0xf0)== 0xe0)return'C';
if((P2IN & 0xf0)== 0xd0)return'D';
if((P2IN & 0xf0)== 0xb0)return'E';
if((P2IN & 0xf0)== 0x70)return'F';
return 0;//
}
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<120;j++);
}
|