📄 main.c
字号:
#include "msp430x14x.h"
void InitSystemClock(void)
{
unsigned char i;
WDTCTL=WDTPW+WDTHOLD; //关闭看门狗
BCSCTL1=0x00; //Set 430 clk 开启XT2,DOC的标称频率为最低 且不分频;XT1为低速晶体(32.768K)
BCSCTL2=0x88; //选择MCLK SCLK的时钟源为高速时钟 不分频,均为8M
do
{
IFG1&=~OFIFG;
for(i=0xff;i>0;i--);
}
while ((IFG1&OFIFG)!=0);
}
void delay(void)
{
int i;
for(i=100;i>0;i++);
}
void KEY_init(void)
{
P1DIR=0;
P1SEL=0;
P1DIR|=BIT4;
P1DIR|=BIT5;
P1DIR|=BIT6;
P1DIR|=BIT7;
P1OUT=0X00;
return;
}
int keyprocess(void)
{
int np10,np11,np12,np13;
int nres=0;
//P1.4输出低电平
P1OUT&=~BIT4;
np10=P1IN&BIT0;
if(np10==0) nres=13;
np11=(P1IN&BIT1)>>1;
if(np11==0) nres=14;
np12=(P1IN&BIT2)>>2;
if(np12==0) nres=15;
np13=(P1IN&BIT3)>>3;
if(np13==0) nres=16;
//P1.5输出低电平
P1OUT&=~BIT4;
np10=P1IN&BIT0;
if(np10==0) nres=9;
np11=(P1IN&BIT1)>>1;
if(np11==0) nres=10;
np12=(P1IN&BIT2)>>2;
if(np12==0) nres=11;
np13=(P1IN&BIT3)>>3;
if(np13==0) nres=12;
//P1.6输出低电平
P1OUT&=~BIT4;
np10=P1IN&BIT0;
if(np10==0) nres=5;
np11=(P1IN&BIT1)>>1;
if(np11==0) nres=6;
np12=(P1IN&BIT2)>>2;
if(np12==0) nres=7;
np13=(P1IN&BIT3)>>3;
if(np13==0) nres=8;
//P1.7输出低电平
P1OUT&=~BIT4;
np10=P1IN&BIT0;
if(np10==0) nres=1;
np11=(P1IN&BIT1)>>1;
if(np11==0) nres=2;
np12=(P1IN&BIT2)>>2;
if(np12==0) nres=3;
np13=(P1IN&BIT3)>>3;
if(np13==0) nres=4;
P1OUT=0x00; //恢复以前值
for(;;)
{
//读各个管脚的状态
np10=P1IN&BIT0;
np11=(P1IN&BIT1)>>1;
np12=(P1IN&BIT2)>>2;
np13=(P1IN&BIT3)>>3;
if(np10==1&&np11==1&&np12==1&&np13==1)
{ //等待松开按键
break;
}
}
return nres;
}
int keyscan(void)
{
int np10,np11,np12,np13;
int nres=0;
for(;;)
{
//读取各个管脚的状态
np10=P1IN&BIT0;
np11=(P1IN&BIT1)>>1;
np12=(P1IN&BIT2)>>2;
np13=(P1IN&BIT3)>>3;
//是否有键被按下
if(np10==0||np11==0||np12==0||np13==0)
{ //有键被按下
_NOP();
break;
}
}
delay();
//读各个管脚的状态
np10=P1IN&BIT0;
np11=(P1IN&BIT1)>>1;
np12=(P1IN&BIT2)>>2;
np13=(P1IN&BIT3)>>3;
//是否有键被按下
if(np10==0||np11==0||np12==0||np13==0)
{ //有键被按下,进行按键分析
nres=keyprocess();
}
else return -1;
return nres;
}
void main()
{ int i;
InitSystemClock();
KEY_init();
while(1)
{
i=keyscan();
P4SEL=0X00;
P4DIR=0xff;
P4OUT= i+2 ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -