我正在努力初始化我的MSP 432 p401 R LaunchPad上的LED和按钮。
问题是:
编写一个代码,读取两个按钮S1和S2(P1.1和P1.4)的输入,并相应地改变LED的状态:无-关闭S1 -红色S2 -绿色都-蓝色
下面是一些代码:
#define RED BIT0 // Red LED connected to Port 1.0
#define GREEN BIT1
#define BLUE BIT2
#define S1 BIT1
#define S2 BIT4
#define SBOTH (BIT1 | BIT4)
#define PB1_PRESSED() ((P1->IN & S1) == 0)
#define PB2_PRESSED() ((P1->IN & S2) == 0)
#define BOTH_PRESSED() ((P1->IN & SBOTH) == 0)
P2->OUT |= OFF; // turn off LEDs
P2->DIR |= (RED | GREEN | BLUE); // set LEDs for output
P2->REN |= (RED | GREEN | BLUE); // turn on resistors
字符串
我的if语句看起来像这样:
if (BOTH_PRESSED())
{
P2->REN |= BLUE;
P2->OUT |= BLUE;
}
型
我在初始化中做错了什么?
2条答案
按热度按时间zengzsys1#
我正在浏览文档,并将指出一些您可能遗漏的细节。您能否提供LED的当前行为?是否只有一种颜色被激活(红色,绿色,蓝色)变亮,没有颜色被激活或RGB组件之间的混合被激活?
1. P1.1和P1.4都需要使能上拉电阻
转到schematics of the launchpad并查找与微控制器的按钮连接,我们可以在**图29原理图(2/6)**中看到,连接到P1.1和P1.4的按钮都接地:
因此,应根据MSP432P4xx SimpleLink™ Microcontrollers Technical Reference Manual第12.2.4节的表12.1在两个I/O处启用上拉电阻:
按钮接地且存在上拉电阻器的这种拓扑的结果是负逻辑:如果按钮被按下,则输入接地,因此MCU读取0;如果按钮未被按下,则输入经由上拉电阻器从VCC获得信号,因此MCU读取1;总之:按下按钮= 0,打开按钮= 1。
2.无需为LED输出启用电阻
回到图29中的schematics of the launchpad,在那里我们可以看到连接到P2.0的LED,P2.1和P2.2已经在阳极中有一个电阻,我不认为需要有上拉或下拉电阻。没有电阻你应该很好:
3.您可能需要在输出端口启用高驱动强度
由于LED以共阴极方式排列,因此MSP 432 I/O引脚将需要驱动电流以点亮LED(与共阳极配置中发生的情况相反)。因此,您需要激活引脚P2.0的高强度驱动器,P2.1和P2.2。再次回到MSP432P4xx SimpleLink™ Microcontrollers Technical Reference Manual,在第**12.2.5节输出驱动强度选择寄存器(PxDS)**中,我们可以看到需要设置BIT 0,P2 DS寄存器中的BIT 1和BIT 2,以允许它们使用高强度IO驱动程序
ds97pgxw2#
TheMulittle做了一个很好的帖子解释了很多。我将给予3个代码示例如何使用按钮
按下按钮时打开LED
字符串
按下按钮时关闭LED
型
检测按钮按下(也是一个很酷的uart功能)
型