MSP430-INTERNAL PULL DOWN

Photo of author

By Jackson Taylor

Home » Hardware » MSP430-INTERNAL PULL DOWN

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.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

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.

See also
Best Compact Camera for Low Light: 5 Picks Ranked by Equivalent Aperture

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

  1. Program start: org 0xf800 places 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.
  2. Watchdog: mov.w #(WDTPW|WDTHOLD), &WDTCTL stops the watchdog timer. Without this, the chip would keep resetting.
  3. Enable the resistor: mov.b #0x10, P1REN turns on the resistor for P1.4 only.
  4. Choose pull-down: CLR.B P1OUT clears all P1OUT bits. Bit 4 being 0 makes the resistor a pull-down.
  5. Clear a working register: CLR R5 sets R5 to zero before use.
  6. Set directions: mov.b #0x1, &P1DIR makes P1.0 an output. All other pins, including P1.4, stay inputs.
  7. Poll the input: at label L1, the code reads P1IN into R5 and masks it with 0x10 so only P1.4 remains. tst r5 sets the zero flag, and JZ L1 jumps back while the result is zero (pin low).
  8. React to the press: once P1.4 reads high, mov.b #0x41, &P1OUT writes 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.
  9. Stop: L3: jmp L3 loops 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.

See also
WIRELESS MODULES

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.

See also
MSP430

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.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

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.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works