MSP432 LaunchPad初始化

piwo6bdm  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(75)

我正在努力初始化我的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;
        }


我在初始化中做错了什么?

zengzsys

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已经在阳极中有一个电阻,我不认为需要有上拉或下拉电阻。没有电阻你应该很好:

由于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驱动程序

ds97pgxw

ds97pgxw2#

TheMulittle做了一个很好的帖子解释了很多。我将给予3个代码示例如何使用按钮

按下按钮时打开LED

// 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;
    }
  }
}

字符串

按下按钮时关闭LED

// 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;
    }
  }
}

检测按钮按下(也是一个很酷的uart功能)

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");
  }
}

相关问题