#include <msp430x14x.h>
/*********************************************
* Check_Key(),检查按键,确认键值
*********************************************/
//int iskeydown = 0;
int keyvalue = 1;
const char control_key[4]={0x01,0x02,0x04,0x08};
char controlvalue=0x08;
void check_Key(void){
P5DIR = 0x07;
P5OUT = 0x00;
P1OUT = 0x00;
for(int I = 0;I <= 0xFF; I ++ );
if (P5IN & 0x78) //有键被按下
{
P5OUT &= ~BIT2; //第一行
P5OUT |= BIT1;
P5OUT |= BIT0;
if(~P5IN & BIT6){keyvalue = 1;} //第一列,显示“1”
if(~P5IN & BIT5){keyvalue = 2;}//第二列,显示“2”
if(~P5IN & BIT4){keyvalue = 3;}//第三列,显示“3”
if(~P5IN & BIT3){keyvalue = 4;}//第四列,显示“4”
P5OUT &= ~BIT1; //第二行
P5OUT |= BIT2;
P5OUT |= BIT0;
if(~P5IN & BIT6){keyvalue = 5;} //第一列,显示“1”
if(~P5IN & BIT5){keyvalue = 6;}//第二列,显示“2”
if(~P5IN & BIT4){keyvalue = 7;}//第三列,显示“3”
if(~P5IN & BIT3){keyvalue = 8;}//第四列,显示“4”
P5OUT &= ~BIT0; //第三行
P5OUT |= BIT2;
P5OUT |= BIT1;
if(~P5IN & BIT6){controlvalue = control_key[0];} //第一列
if(~P5IN & BIT5){controlvalue = control_key[1];}//第二列
if(~P5IN & BIT4){controlvalue = control_key[2];}//第三列
if(~P5IN & BIT3){controlvalue = control_key[3];}//第四列
}
}
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
const char tab_seg[8] = { 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,0x80}; //从0到7循环显示
P4DIR = 0xFF;//p4输出
P6DIR |= 0x03;
for(;;){
//for(int j = 0 ; j <= 7; j++)
// {
P6OUT = 0x00;//使选择控制的74HC373的LE引脚有效
P4OUT=controlvalue;//控制哪个LED亮
P6OUT=0x01;//关闭选择控制的74HC373的LE引脚
P6OUT = 0x02;//使数据显示控制的74HC373的LE引脚有效
check_Key();
P4OUT=tab_seg[keyvalue-1];
P6OUT=0x00;//关闭数据显示控制的74HC373的LE引脚
for(int I = 0 ; I <= 0x0FFF; I ++ );
//}
}
}