#include <msp430f6638.h>
char keyscan();
void delay(unsigned char z);
void main(void)
{
char K=0;
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
P4DIR&=~(BIT4+BIT3+BIT2+BIT1+BIT0);
P4OUT|=BIT4+BIT3+BIT2+BIT1+BIT0;
P4REN =BIT4+BIT3+BIT2+BIT1+BIT0;
P4DIR|=BIT5+BIT6+BIT7;
P5DIR|=BIT7;
P8DIR|=BIT0;
while(1)
{
K=keyscan();
if(K==1)
{
delay(50);
if(K==1)
{
while(!(P4IN&0X10));
P4OUT^=BIT5;
}
}
if(K==2)
{
delay(50);
if(K==2)
{
while(!(P4IN&0X08));
P4OUT^=BIT6;
}
}
if(K==3)
{
delay(50);
if(K==3)
{
while(!(P4IN&0X04));
P4OUT^=BIT7;
}
}
if(K==4)
{
delay(50);
if(K==4)
{
while(!(P4IN&0X02));
P5OUT^=BIT7;
}
}
if(K==5)
{
delay(50);
if(K==5)
{
while(!(P4IN&0X01));
P8OUT^=BIT0;
}
}
}
}
char keyscan()
{
if(!(P4IN&0X10))
return 1;
if(!(P4IN&0X08))
return 2;
if(!(P4IN&0X04))
return 3;
if(!(P4IN&0X02))
return 4;
if(!(P4IN&0X01))
return 5;
return 0;
}
void delay(unsigned char z)
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=20;y>0;y--);
}
我用MSP430F6638做独立按键扫描,我把里面的if(!(P4IN&0X10))换成if(P4IN&0X08==0)无法实现按键的功能,但是两个语句的功能是一样的,很是不解,求各位大神指教!! |