MSP430 - 如何读取接地I / O引脚?

时间:2012-09-01 05:00:00

标签: io switch-statement microcontroller msp430

我有一个MSP430 G2452连接了几个拨动开关(读取:NOT按钮)。我知道如何设置中断和去抖按钮,但是我很难设置我的端口,所以我可以采样关闭/打开哪些开关。

我希望能够读取P1IN(带屏蔽)以确定哪些开关已关闭/打开以及哪些打开/关闭。开关连接到GND,我将它们连接的引脚上拉,所以当一个开关闭合/打开时,它所连接的引脚应该被拉下。但P1IN似乎没有改变。

我能够设置一个在引脚接地时触发的中断,但我只想对引脚进行采样,而不是使用中断。如何设置端口引脚以便我可以用PxIN读取它们的状态?

// Port 1 setup
    P1SEL = 0;                  // set P1 as digital I/O for all pins
    P1DIR = ~sw1to6;            // set switches 1-6 for input
    P1OUT = sw1to6;             // enable pull-ups on switches 1-6
    P1REN |= sw1to6;            // enable pull-up resistors on switches 1-6

(头文件定义:)

// P1.0-1.5
#define sw1 0x01
#define sw2 0x02
#define sw3 0x04
#define sw4 0x08
#define sw5 0x10
#define sw6 0x20
#define sw1to6 sw1 | sw2 | sw3 | sw4 | sw5 | sw6

1 个答案:

答案 0 :(得分:3)

由于你的“sw1to6”宏不在括号中,否定将不会达到预期效果。你可能想要:

#define sw1to6 (sw1 | sw2 | sw3 | sw4 | sw5 | sw6)

一般情况下,最好始终将宏放在括号中。

之后你应该能够读取P1IN上的引脚。