1、MSP430 4x4 阵列按键程序作者:cxjr 摘自:匠人的百宝箱采用 P1 口 4x4 阵列参考电路例程:/*#include /*/IO 初始化程序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 =
2、0;P1IFG = 0;/打开管脚的中断功能/对应的管脚由高到低电平跳变使相应的标志置位P1IE |= BIT0; P1IES |= BIT0; P1IE |= BIT1; P1IES |= BIT1;P1IE |= BIT2; P1IES |= BIT2; P1IE |= BIT3; P1IES |= BIT3;_EINT();/打开中断return;void Delay(void)int i;for(i = 100;i-;i 0) ;/延时一点时间/*/键处理程序int KeyProcess(void)int nP10,nP11,nP12,nP13;int nRes = 0;/P1.4 输
3、出低电平P1OUT nP10 = P1IN if (nP10 = 0) nRes = 13;nP11 = P1IN if (nP11 = 0) nRes = 14;nP12 = P1IN if (nP12 = 0) nRes = 15;nP13 = P1IN if (nP13 = 0) nRes = 16;/P1.5 输出低电平P1OUT nP10 = P1IN if (nP10 = 0) nRes = 9;nP11 = P1IN if (nP11 = 0) nRes = 10;nP12 = P1IN if (nP12 = 0) nRes = 11;nP13 = P1IN if (nP13 =
4、 0) nRes = 12;/P1.6 输出低电平P1OUT nP10 = P1IN if (nP10 = 0) nRes = 5;nP11 = P1IN if (nP11 = 0) nRes = 6;nP12 = P1IN if (nP12 = 0) nRes = 7;nP13 = P1IN if (nP13 = 0) nRes = 8;/P1.7 输出低电平P1OUT nP10 = P1IN if (nP10 = 0) nRes = 1;nP11 = P1IN if (nP11 = 0) nRes = 2;nP12 = P1IN if (nP12 = 0) nRes = 3;nP13 =
5、P1IN if (nP13 = 0) nRes = 4;P1OUT = 0x00;/恢复以前值。/读取各个管脚的状态nP10 = P1IN nP11 = P1IN nP12 = P1IN nP13 = P1IN for(;)if(nP10 = 1 return nRes;/*/ 处理来自端口 1 的中断interrupt PORT1_VECTOR void PORT_ISR(void)Delay();KeyProcess();if(P1IFG / 清除中断标志位if(P1IFG / 清除中断标志位if(P1IFG / 清除中断标志位if(P1IFG / 清除中断标志位/*/时基初始化程序void Init_CLK(void)unsigned int i;BCSCTL1 = 0X00; /将寄存器的内容清零/XT2 震荡器开启/LFTX1 工作在低频模式/ACLK 的分频因子为 1 do IFG1 / 清除 OSCFault 标志for (i = 0x20; i 0; i-); while (IFG1 / 如果 OSCFault =1 BCSCTL2 = 0X00; /将寄存器的内容清零BCSCTL2 += SELM1; /MCLK 的时钟源为 TX2CLK,分频因子为 1BCSCTL2 += SELS; /SMCLK 的时钟源为 TX2CLK,分频因子为 1