我正在努力初始化 MSP432p401R 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;
}
我在初始化时做错了什么?
我正在查看文档,并会指出您可能缺少的一些细节。您能提供一下 LED 当前的行为吗?是否只有一种颜色被激活(红、绿、蓝)变亮,没有颜色被激活,还是 RGB 分量之间的混合被激活?
1。您需要为 P1.1 和 P1.4 启用上拉电阻
转到 启动板原理图并查找与微控制器的按钮连接,我们可以在图 29 原理图(第 2 幅,共 6 幅)中看到,连接到 P1.1 和 P1.4 的按钮均接地:
因此,您应该按照MSP432P4xx SimpleLink™ 微控制器技术参考手册第 12.2.4 节的表 12.1 启用两个 I/O 上的上拉电阻:
这种按钮接地且有上拉电阻的拓扑结构的结果是负逻辑:如果按下按钮,则输入接地,因此 MCU 读取为 0;如果按下按钮,则输入接地,因此 MCU 读取为 0;如果按钮没有按下,输入端通过拉电阻从VCC获取信号,因此MCU读到1;总之:按下按钮 = 0,打开按钮 = 1。
2。您无需为 LED 输出启用电阻器
回到图 29 中的发射板原理图,我们可以看到连接到 P2.0、P2.1 和 P2.2 的 LED 阳极已经有一个电阻,我认为没有必要有上拉或下拉电阻。没有电阻你应该没问题:
3.您可能需要在输出端口启用高驱动强度
由于 LED 以共阴极方式排列,MSP432 I/O 引脚需要驱动电流来点亮 LED(与共阳极配置中发生的情况相反)。因此,您需要激活引脚 P2.0、P2.1 和 P2.2 的高强度驱动器。再次回到MSP432P4xx SimpleLink™ 微控制器技术参考手册中的12.2.5 输出驱动强度选择寄存器 (PxDS),我们可以看到我们需要在 P2DS 寄存器中设置 BIT0、BIT1 和 BIT2,以便允许他们使用高强度IO驱动程序
TheMulittle 发表了一篇很好的文章,解释了很多。我将给出 3 个代码示例来说明如何使用按钮
// This program will turn off a red LED when a button is pressed
#include "msp.h"
#include <stdint.h>
int main(void) {
// configure P1.0 and P1.1 as GPIO
P1->SEL0 &= ~0x03;
P1->SEL1 &= ~0x03;
// set P1.0 as output and P1.1 as input
P1->DIR |= 0x1; // set P1.0 as output
P1->DIR &= ~0x2; // set P1.1 as input
// enable pull-up/pull-down resistors for P1.1
P1->REN |= 0x2;
// enable pull-up resistor for P1.1
P1->OUT |= 0x2;
while (1) {
// check if the button connected to P1.1 is pressed
if ((P1->IN & 0x2) == 0x2) {
// if the button is not pressed, turn on the red LED connected to P1.0
P1->OUT |= 0x1;
} else {
// if the button is pressed, turn off the red LED connected to P1.0
P1->OUT &= ~0x1;
}
}
}
// This program will turn off a red LED when a button is pressed
#include "msp.h"
#include <stdint.h>
int main(void) {
// configure P1.0 and P1.1 as GPIO
P1->SEL0 &= ~(BIT0 | BIT1);
P1->SEL1 &= ~(BIT0 | BIT1);
// set P1.0 as output and P1.1 as input
P1->DIR |= BIT0; // set P1.0 as output
P1->DIR &= ~BIT1; // set P1.1 as input
// enable pull-up/pull-down resistors for P1.1
P1->REN |= BIT1;
// enable pull-up resistor for P1.1
P1->OUT |= BIT1;
while (1) {
// check if the button connected to P1.1 is pressed
if ((P1->IN & BIT1) == BIT1) {
// if the button is not pressed, turn on the red LED connected to P1.0
P1->OUT |= BIT0;
} else {
// if the button is pressed, turn off the red LED connected to P1.0
P1->OUT &= ~BIT0;
}
}
}
void uart_init(void) {
EUSCI_A0->CTLW0 |= 0X1;
EUSCI_A0->MCTLW = 0X0;
EUSCI_A0->CTLW0 |= 0x80;
EUSCI_A0->BRW = 0x34;
EUSCI_A0->CTLW0 &= ~0x01;
P1->SEL0 |= 0x0C;
P1->SEL1 &= ~0x0C;
}
void uart_send_str(const char *str) {
uint32_t i = 0;
while (str[i] != '\0') {
EUSCI_A0->TXBUF = str[i];
while ((EUSCI_A0->IFG & 0x02) == 0) {}
i++;
}
}
// Use the other code examples with this function:
void digital_input(void) {
bool button1 = (P1->IN & BIT1) == 0;
bool button2 = (P1->IN & BIT4) == 0;
if (button1 && button2) {
uart_send_str("Buttons 1 and 2 are pressed\r\n");
} else if (button1) {
uart_send_str("Buttons 1 is pressed\r\n");
} else if (button2) {
uart_send_str("Button 2 is pressed\r\n");
} else {
uart_send_str("No Buttons are pressed\r\n");
}
}