c - 仅按下一个按钮时 MSP432 Launchpad 无法识别
问题描述
我正在尝试在 MSP432 Launchpad 上创建一个程序,当两个板载按钮(P1.4 和 P1.1)都没有被按下时,它会打开绿色 LED,如果只有 P1.4 按钮,它会打开红色 LED正在按下。使用下面的代码,Launchpad 在没有按下任何按钮时会正确打开绿色 LED,但在我仅按下 P1.4 按钮时不会打开红色 LED。我的代码或引脚配置有什么问题吗?
#include "msp.h"
#include "clock.h"
#include "TExaS.h"
void main()
{
Clock_Init48MHz();
/* Configuration of MSP42 */
P2->SEL0 &= ~0x03; // configure P2.0 and 2.1 as GPIO
P2->SEL1 &= ~0x03; // configure P2.0 and 2.1 as GPIO
P2->DIR |= 0x03; // configure P2.0 and P2.1 as output
//configure buttons on P1.1 and P1.4 as GPIO pull up inputs
P1->SEL0 &= ~0x12;
P1->SEL1 &= ~0x12;
P1->DIR &= ~0x12;
P1->REN |= 0x12;
P1->OUT |= 0x12;
//Application
while(1){
Clock_Delay1ms(100);
//if P1.4 and P1.1 are both not being pressed
if(P1->IN & 0x12){
P2->OUT |= 0x02; //turn on green light
P2->OUT &= ~0x01; //turn off red light
}
//if only P1.4 is pressed
else if(P1->IN & 0x10){
P2->OUT |= 0x01; //turn on red light
P2->OUT &= ~0x02; //turn off green light
}
}
}
解决方案
您的 if 语句不同意上面的评论。
如果您的开关处于低电平有效状态(这意味着打开上拉电阻),那么第一个 if 语句测试是否有一个按钮未按下,而不是两者都未按下,并且您的 else-if 会检查 P1.4 是否未按下而与 P1.1 无关(这可以永远不会是这种情况,因为它已经匹配了 if - 也就是说,除非读取之间的值发生变化)。
if ((x & m) == m)
我认为您可能需要考虑和之间的区别if (x & m)
。
推荐阅读
- oracle - 在springboot中通过hikaricp设置oracle数据库模式
- javascript - react-form 和 redux-form 有什么区别?
- node.js - MongoDB 可以接受超过 12 个字节和 24 个十六进制字符的自定义 Objectid 吗?
- c# - 无法使用 System.Management 获取 azure vm 机器的已安装软件详细信息
- android - Android资源链接失败(未找到任何解决方案)
- sql - 在gridview中生成下拉字段,其中选项作为另一列中具有相同ID的列中的值
- c# - 使用 c# 将受密码保护的 Zip 文件转换为 Enc 文件
- azure - 使用 Azure SQLDW Polybase 使用 vnet 服务终结点从 ADLS Gen 1 引入数据
- c# - 在过滤器属性中使用 Autofac 进行属性/方法注入
- pentaho-spoon - 在 pentaho PDI V7 中未选中表输出中的“指定数据库字段”时,“列名无效”