📄 msp430键盘中断扫描程序07.4.3.txt
字号:
#include <msp430x14x.h>
void Init_Port(void)
{
//将P1口所有的管脚在初始化的时候设置为输入方式
P1DIR = 0;
//将P1口所有的管脚设置为一般I/O口
P1SEL = 0;
// 将P1.4 P1.5 P1.6 P1.7设置为输出方向
P1DIR |= BIT4;
P1DIR |= BIT5;
P1DIR |= BIT6;
P1DIR |= BIT7;
//先输出低电平
P1OUT = 0x00;
// 将中断寄存器清零
P1IE = 0;
P1IES = 0;
P1IFG = 0;
//打开管脚的中断功能
//对应的管脚由高到低电平跳变使相应的标志置位
P1IE |= BIT0;
P1IES|= BIT0;
P1IE |= BIT1;
P1IES|= BIT1;
P1IE |= BIT2;
P1IES|= BIT2;
P1IE |= BIT3;
P1IES |= BIT3;
_EINT();//打开中断
return;
}
void Delay(unsigned int k)
{
while(k--)
{}
}
int KeyProcess(void)
{
//P5OUT&=0x00;
P5OUT=0x00;
Delay(20);
unsigned char nP10,nP11,nP12,nP13;
nP10=0;
nP11=0;
nP12=0;
nP13=0;
int nRes = 0;
//P1.7输出低电平
P1OUT = ~(BIT7);
Delay(50);
nP10 = P1IN & BIT0;
if (nP10 == 0)
{
nRes = 13;
P5OUT|=BIT4;
}
nP11 = P1IN & BIT1;
if (nP11 == 0)
{
nRes = 14;
P5OUT|=BIT4;
}
nP12 = P1IN & BIT2;
if (nP12 == 0)
{
nRes = 15;
P5OUT|=BIT4;
}
nP13 = P1IN & BIT3;
if (nP13 == 0)
{
nRes = 16;
//P5OUT=0x00;
P5OUT|=BIT4;
}
//P1.6输出低电平
P1OUT = ~(BIT6);
Delay(50);
nP10 = P1IN & BIT0;
if (nP10 == 0)
{
nRes = 9;
P5OUT|=BIT5;
}
nP11 = P1IN & BIT1;
if (nP11 == 0)
{
nRes = 10;
P5OUT|=BIT5;
}
nP12 = P1IN & BIT2;
if (nP12 == 0)
{
nRes = 11;
P5OUT|=BIT5;
}
nP13 = P1IN & BIT3;
if (nP13 == 0)
{
nRes = 12;
//P5OUT=0x00;
P5OUT|=BIT5;
}
//P1.5输出低电平
P1OUT = ~(BIT5);
Delay(50);
nP10 = P1IN & BIT0;
if (nP10 == 0)
{
nRes = 5;
P5OUT|=BIT6;
}
nP11 = P1IN & BIT1;
if (nP11 == 0)
{
nRes = 6;
P5OUT|=BIT6;
}
nP12 = P1IN & BIT2;
if (nP12 == 0)
{
nRes = 7;
P5OUT|=BIT6;
}
nP13 = P1IN & BIT3;
if (nP13 == 0)
{
nRes = 8;
//P5OUT=0x00;
P5OUT|=BIT6;
}
//P1.4输出低电平
P1OUT = ~(BIT4);
nP10 = P1IN & BIT0;
if (nP10 == 0)
{
nRes = 1;
P5OUT|=BIT7;
}
nP11 = P1IN & BIT1;
if (nP11 == 0)
{
nRes = 2;
P5OUT|=BIT7;
}
nP12 = P1IN & BIT2;
if (nP12 == 0)
{
nRes = 3;
P5OUT|=BIT7;
}
nP13 = P1IN & BIT3;
if (nP13 == 0)
{
nRes = 4;
P5OUT|=BIT7;
}
P1OUT = 0x00;//恢复以前值。
//读取各个管脚的状态
for(;;)
{
if((P1IN&0x0F)==0x0F )
{
break;//等待松开按键
}
}
return nRes;
}
// 处理来自端口 1 的中断
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
Delay(30000);
KeyProcess();
if(P1IFG & BIT0)
{
P1IFG &= ~(BIT0);// 清除中断标志位
}
if(P1IFG & BIT1)
{
P1IFG &= ~(BIT1);// 清除中断标志位
}
if(P1IFG & BIT2)
{
P1IFG &= ~(BIT2);// 清除中断标志位
}
if(P1IFG & BIT3)
{
P1IFG &= ~(BIT3);// 清除中断标志位
}
}
void Init_CLK(void)
{
unsigned int i;
BCSCTL1 = 0X00; //将寄存器的内容清零
//XT2震荡器开启
//LFTX1工作在低频模式
//ACLK的分频因子为1
do
{
IFG1 &= ~OFIFG; // 清除OSCFault标志
for (i = 0x20; i > 0; i--);
}
while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1
BCSCTL2 = 0X00; //将寄存器的内容清零
BCSCTL2 += SELM1; //MCLK的时钟源为TX2CLK,分频因子为1
BCSCTL2 += SELS; //SMCLK的时钟源为TX2CLK,分频因子为1
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
Init_CLK();
P5DIR=0XFF;
P5SEL=0x00;
Delay(1000);
P5OUT&=0x00;
Init_Port( );
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -