As an Amazon Associate we earn from qualifying purchases.
Many MSP430 chips can connect a resistor to a pin for you, so you don’t need an external one for a simple button or switch. This article shows how to enable the MSP430 internal pull-down resistor on a port 1 pin, why it matters, and walks through a short assembly program that waits for a high level on P1.4.
The example uses the msp430g2x31.inc header, so it targets the MSP430G2x31 family (such as the G2231). The same idea applies to other MSP430 devices with port resistor registers, but check the datasheet and family guide for your chip.
What an internal pull-down resistor does
A digital input that isn’t driven by anything is “floating”. It can pick up noise and read as 0 or 1 at random. A pull-down resistor ties the pin weakly to ground, so the pin reads low until something drives it high, such as a button connected to the supply voltage.
The MSP430 has a resistor on each port pin that can be switched in by software. The value is typically in the tens of kilohms, which is enough to hold the pin low but weak enough that an external signal overrides it easily. Check the datasheet for the exact range on your device.
The registers involved
Enabling the pull-down takes two register bits for the pin you want. For P1.4, that is bit 4 (mask 0x10).
- P1DIR: sets each pin as input (0) or output (1). After reset, all pins are inputs, so P1.4 is already an input.
- P1REN: resistor enable. Setting bit 4 connects the internal resistor to P1.4.
- P1OUT: for an input pin with its resistor enabled, this bit selects the direction of the resistor. 0 = pull-down, 1 = pull-up.
- P1IN: the register you read to get the current logic level on the pins.
So the recipe for a pull-down is: leave the pin as an input, set its P1REN bit, and clear its P1OUT bit. If you set the P1OUT bit instead, you get an internal pull-up resistor rather than a pull-down. The article on the internal pull-up covers that case.
Example: digital input with internal pull-down on P1.4
The program below enables the pull-down on P1.4, then waits in a loop until the pin goes high. When it does, it writes to P1OUT and stops in an endless loop.
.include "msp430g2x31.inc"
org 0xf800
mov.w #(WDTPW|WDTHOLD), &WDTCTL
mov.b #0x10, P1REN
CLR.B P1OUT
CLR R5
mov.b #0x1, &P1DIR
L1: MOV.b &P1IN, r5
AND.b #0x10,r5
tst r5
JZ L1
mov.b #0x41, &P1OUT
L3: jmp L3
org 0xfffe
dw 0xf800
How the code works
- Program start:
org 0xf800places the code at the start of flash on the G2x31 parts. The last two lines place that address in the reset vector at 0xFFFE, so the chip begins running there after reset. - Watchdog:
mov.w #(WDTPW|WDTHOLD), &WDTCTLstops the watchdog timer. Without this, the chip would keep resetting. - Enable the resistor:
mov.b #0x10, P1RENturns on the resistor for P1.4 only. - Choose pull-down:
CLR.B P1OUTclears all P1OUT bits. Bit 4 being 0 makes the resistor a pull-down. - Clear a working register:
CLR R5sets R5 to zero before use. - Set directions:
mov.b #0x1, &P1DIRmakes P1.0 an output. All other pins, including P1.4, stay inputs. - Poll the input: at label
L1, the code reads P1IN into R5 and masks it with 0x10 so only P1.4 remains.tst r5sets the zero flag, andJZ L1jumps back while the result is zero (pin low). - React to the press: once P1.4 reads high,
mov.b #0x41, &P1OUTwrites 0x41, which sets bits 0 and 6 of P1OUT. Because P1.0 is an output, it goes high, which can drive an LED. Bit 6 is not configured as an output here, so it does not drive the pin. - Stop:
L3: jmp L3loops forever so the program doesn’t run into undefined memory.
Wiring the circuit
With a pull-down, the idle state is low, so the switch must bring the pin high when pressed. Connect one side of a pushbutton to the supply voltage (VCC) and the other side to P1.4. No external resistor to ground is needed, because the internal one does that job. Connect an LED, with a suitable series resistor, to P1.0 to see the result.
Do not connect the button from P1.4 to ground when using a pull-down. The pin would always read low and the program would never leave the loop.
Things to watch for
- Order of setup: set P1REN and clear the P1OUT bit before you start reading the pin, so the first read is not a floating value.
- Writing P1OUT later: any write to P1OUT can change bit 4 and flip the resistor to a pull-up. In the example, 0x41 has bit 4 clear, so the pull-down stays active.
- Weak resistor: the internal resistor is fine for buttons and switches. It is not a substitute for an external resistor when you need a specific, stronger or more precise value, or when the line is long and noisy.
- Debouncing: mechanical buttons bounce. This example reacts to the first high reading, which is fine for a demo, but real projects usually need a short delay or a software filter.
- Polling vs. interrupts: the loop above keeps the CPU busy. Port interrupts let the chip sleep until the pin changes.
Where to go next
If you are new to the topic, the post on digital I/O in assembly covers the basics of port registers. To see how reading and driving pins is handled on their own, read the articles on MSP430 digital input and digital output. For interrupts combined with pull up/down resistors, see the part 1.1 assembly tutorial.
FAQ
How do I enable the internal pull-down on an MSP430 pin?
Make the pin an input (the default), set its bit in the PxREN register, and clear its bit in PxOUT. For P1.4 that means setting bit 4 of P1REN and clearing bit 4 of P1OUT.
What is the difference between internal pull-up and pull-down on the MSP430?
Both use the same PxREN bit to connect the resistor. The PxOUT bit decides which one you get: 0 gives a pull-down to ground, and 1 gives a pull-up to the supply.
Do all MSP430 devices have internal pull-down resistors?
Not all of them. Many newer value-line parts such as the G2 series do, but some older devices have no resistor registers. Check your chip’s datasheet and user guide.
Why does my input still read randomly?
Common causes are forgetting to set P1REN, leaving the P1OUT bit set (which makes it a pull-up), or setting the pin as an output. Also check that your button connects the pin to VCC and not to ground when you use a pull-down.