📄 key.c
字号:
#include <msp430x14x.h>
#define uint unsigned int
#define uchar unsigned char
uint key[100];
uint j=0;
void Delay(uint ntemp)
{
for(uint i=ntemp;i>0;i--)
{
;
}
}
//****************
//**时钟初始化
//***************
void Init_CLK(void)
{
uint i;
BCSCTL1&=~XT2OFF;//启动XT2,ACK为XT1
BCSCTL2|=SELM1;
BCSCTL2&=~SELM0;//mclk为XT2;
do
{
IFG1&=~OFIFG;
for(i=0xFF;i>0;i--);
}
while((IFG1&OFIFG)!=0);//等待时钟源转换成功
}
//****************
//**端口初始化
//***************
void Init_Port(void)
{
_DINT(); //关总中断
P5SEL=0X00;
P5OUT=0X00;
P5DIR=0XFF;
P1DIR = 0; //将P1口所有的管脚在初始化的时候设置为输入方式
P1SEL = 0; //将P1口所有的管脚设置为一般I/O口
P1IFG = 0;
P1IE = 0x0f; //打开中断
P1IES = 0x0f; //对应的管脚由高到低电平跳变使相应的标志置位
_EINT(); //开总中断*/
}
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; //关狗便于调试
Init_CLK();
Init_Port();
P5OUT=0XFF;
while(1)
{
;
}
}
int KeyProcess(void)
{
int temp; //分析键值
int nRes;
temp = P1IN & 0x0f;
if(temp!=0x0f)
{
Delay(20);
temp=P1IN & 0x0f;
if(temp!=0x0f)
{
if (temp==0x0e)
{
nRes=1;
}
if(temp==0x0d)
{
nRes=2;
}
if(temp==0x0b)
{
nRes=3;
}
if(temp==0x07)
{
nRes=4;
}
if(temp==0x05)
{
nRes=5;
}
}
}
temp = P1IN & 0x0f; //读取各个管脚的状态
while(temp!=(0x0f)) //等待松开按键。。+一延时,如果键还按着就退出程序,准备
{ //接受下次中断,目的实现多键操作
temp = P1IN & 0x0f; //注意重新读一次
}
//键值返回
return nRes;
/* switch(nRes)
{
case 1:
P5OUT=0x00;break;
case 2:
P5OUT=0X01;break;
case 3:
P5OUT=0X02;break;
case 4:
P5OUT=0X03;break;
default:break;
}*/
}
// 处理来自端口 1的中断
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
Delay(20);//防止按键过快
uint key1=0;
key1= KeyProcess();
key[j] = key1;
//key[i]= i;
j++;
switch(key1)
{
case 1:
P5OUT=0x00;break;
case 2:
P5OUT=0X01;break;
case 3:
P5OUT=0X02;break;
case 4:
P5OUT=0X03;break;
case 5:
P5OUT=0XFF;break;
default:break;
}
//P1IFG=0x00;
if(P1IFG & BIT0)
{
P1IFG &= ~(BIT0);// 清除中断标志位
}
if(P1IFG & BIT1)
{
P1IFG &= ~(BIT1);// 清除中断标志位
}
if(P1IFG & BIT2)
{
P1IFG &= ~(BIT2);// 清除中断标志位
}
if(P1IFG & BIT3)
{
P1IFG &= ~(BIT3);// 清除中断标志位
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -