MSP430 - How do I read a grounded I/O pin?
-
29-06-2021 - |
Вопрос
I have an MSP430 G2452 wired up with several toggle switches (read: NOT buttons). I know how to set up interrupts and debounce a button, but I am having a hard time setting up my port so I can just sample which switches are closed/on.
I want to be able to read P1IN (with masking) to figure out which switches are closed/on and which are open/off. The switches are connected to GND, and I have the pins they're connected to pulled up, so when a switch is closed/on, the pin it's connected to should be pulled down. But P1IN doesn't seem to change.
I am able to set up an interrupt that triggers when a pin is grounded, but I just want to sample the pins, not use an interrupt. How do I set up my port pins so that I can read their states with 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
(header file definitions:)
// 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
Решение
Since your "sw1to6" macro is not in parenthesis the negation will not do what you expect. You probably want:
#define sw1to6 (sw1 | sw2 | sw3 | sw4 | sw5 | sw6)
In general it is best to always put your macros in parenthesis.
After that you should be able to read the pins on P1IN.