历史上的今天
今天是:2025年01月29日(星期三)
2018年01月29日 | MSP430单片机按键程序
2018-01-29 来源:eefocus
MSP430单片机按键程序
#include
#include "key.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(void)
{
int i;
for(i = 100;i > 0;i--) ;//延时一点时间
}
int KeyProcess(void)
{
int nP10;
int nP11;
int nP12;
int 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;//恢复以前值。
//读取各个管脚的状态
nP10 = P1IN & BIT0;
nP11 = (P1IN & BIT1) >> 1;
nP12 = (P1IN & BIT2) >> 2;
nP13 = (P1IN & BIT3) >> 3;
for(;;)
{
if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
{
//等待松开按键
break;
}
}
return nRes;
}
// 处理来自端口 1 的中断
#if __VER__ < 200
interrupt [PORT1_VECTOR] void PORT_ISR(void)
#else
#pragma vector=PORT1_VECTOR
__interrupt void PORT_ISR(void)
#endif
{
Delay();
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
}
key.h
void Init_CLK(void);
int KeyProcess(void);
void Delay(void);
int KeySCAN(void);
void Init_Port(void);
史海拾趣
|
以前从没接触过这类芯片。现在要用啊。但又不会。 谁能告诉下我m2716在Proteus中如何加载数据,还有在现实中用什么把数据烧到它里面去? 不甚感谢啊... … 查看全部问答> |
|
当设备与 Visual Studio 机器没有 ActiveSync 连接,但有一个有效的 TCP 连接时,部署无效。 解决办法:使用单独提供的 Keyman 工具来执行初始的设备设置. 我想请问KEYMAN工具是什么,在哪里可以找到它,或者下到它? 谢谢~… 查看全部问答> |
|
几乎所有的电子设计中都会使用到电阻,我稍微提供问题,你知道的电阻有哪些参数指标呢?你敢不敢说个大家都不知道的指标?这个大家现在用的多了也许再过多少年后就看不到这位了… 查看全部问答> |
|
做了个东西用的DHT11温湿度传感器 显示是用的数码管动态扫描 由于执行驱动函数用的时间比较长就把显示函数放在了定时器中断里面 问题出来了 定时器中断把DHT11驱动函数的时序给打乱了 读出来的数据一直在跳动 乱 我在驱动函 ...… 查看全部问答> |
|
Error connecting to the target: (Error -151 @ 0x0) One of the FTDI driver functions used during the connect returned bad status or an error. The cause may one or more of: invalid emulator serial number, blank emulator EEPROM, ...… 查看全部问答> |




