MSP430-Digital Input

Photo of author

By Jackson Taylor

Home » Hardware » MSP430-Digital Input

As an Amazon Associate we earn from qualifying purchases.

A digital input lets a microcontroller find out whether a pin is at a logic high or a logic low. That is how you read a push button, a switch or the output of a digital sensor. This article shows how to read a digital input on the MSP430 using pin P1.3, and how to react to it by switching on two outputs. The example is written in assembly.

How digital input works on the MSP430

Each pin of port 1 is controlled by a few 8-bit registers. Each bit in a register matches one pin, so bit 3 belongs to P1.3.

  • P1DIR sets the direction of each pin. A 0 makes the pin an input and a 1 makes it an output.
  • P1IN holds the current logic level on the pins. You read this register to get the input state.
  • P1OUT holds the value driven on pins configured as outputs.
  • P1REN enables the internal pull resistors for the pins you select.

After a reset, all port 1 pins are inputs, so reading a pin often needs no direction setup. You only need to set P1DIR for the pins you want as outputs.

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

Why you need a pull-down resistor

A pin that is not connected to anything is “floating”. Its voltage can drift, and the value you read may flip between 0 and 1 at random. To avoid this, the input needs a defined level when nothing is driving it.

In this example, P1.3 is held low by a pull-down resistor, and a switch connects the pin to the supply voltage when pressed. The pin reads 0 while the switch is open and 1 while it is closed. A resistor in the range of a few kilohms to tens of kilohms is commonly used for this. You can also use the internal pull-down resistor of the chip instead of an external part. See the dedicated article for how to enable it.

Digital input using P1.3 (pull-down resistor required)

The program below waits until P1.3 goes high. It then turns on two output pins, P1.0 and P1.6. On many LaunchPad boards these pins are connected to the two LEDs, but check your own board before relying on that.

.include "msp430g2x31.inc"
org 0xf800
mov.w #(WDTPW|WDTHOLD), &WDTCTL
CLR.B P1OUT
CLR R5
mov.b #0x41, &P1DIR
L1:   MOV.b &P1IN,  r5
      AND.b #0x8,r5
      tst r5
      JZ L1
      mov.b #0x41, &P1OUT
L3:   jmp L3
org 0xfffe
dw 0xf800

The directives and syntax shown here follow the style used by the original example. Depending on your assembler, you may need to adjust small details such as the include file or the way the origin is declared.

See also
MSP430-TIMER

How the program works

Setup

  • .include "msp430g2x31.inc" loads the register and bit names for the device family, such as P1IN, P1DIR and WDTCTL.
  • org 0xf800 places the code at the start of program memory.
  • mov.w #(WDTPW|WDTHOLD), &WDTCTL stops the watchdog timer. If you leave it running, it resets the chip after a short time.
  • CLR.B P1OUT sets all port 1 output latches to 0, so the outputs start low.
  • CLR R5 clears register R5, which is used to hold the input reading.
  • mov.b #0x41, &P1DIR sets bits 0 and 6 to 1, which makes P1.0 and P1.6 outputs. All other pins, including P1.3, stay inputs.

Reading the input

  • MOV.b &P1IN, r5 copies the state of all eight port 1 pins into R5.
  • AND.b #0x8, r5 keeps only bit 3 (0x08) and clears the rest. R5 is now 0 if P1.3 is low, or 8 if it is high.
  • tst r5 tests the value. The AND instruction already updates the zero flag, so this line is not strictly needed, but it does no harm.
  • JZ L1 jumps back to L1 while the result is zero. The program keeps reading the pin until it goes high. This technique is called polling.

Acting on the input

  • mov.b #0x41, &P1OUT sets P1.0 and P1.6 high.
  • L3: jmp L3 is an endless loop that jumps to itself. Without it, the CPU would keep running into whatever follows in memory.
  • org 0xfffe and dw 0xf800 store the reset vector, which tells the MSP430 to start executing at address 0xf800 after a reset.

Choosing the right bit mask

The mask in the AND instruction decides which pin you test. Use the value with only that pin’s bit set:

  • P1.0 is 0x01
  • P1.1 is 0x02
  • P1.2 is 0x04
  • P1.3 is 0x08
  • P1.4 is 0x10
  • P1.5 is 0x20
  • P1.6 is 0x40
  • P1.7 is 0x80

The output value 0x41 in this example is simply 0x01 plus 0x40, which selects P1.0 and P1.6 together.

See also
AC LAMP BLINKING USING MSP430 AND TRIAC

Limitations of this example

The program is meant to show the basics, so it is simple on purpose.

  • It reacts only once. After the outputs go high, the program stays in the final loop. Only a reset starts it again. To follow the button, put the read and the P1OUT update inside one loop.
  • There is no debouncing. Mechanical switches can bounce for a short time when pressed. For a one-shot action this does not matter, but if you count presses you need a short delay or a software filter.
  • Polling keeps the CPU busy. If the chip has other work to do or you want low power, use interrupts instead of polling.

Related reading

To drive pins instead of reading them, see the article on MSP430 digital output. For a wider introduction to this instruction set and the port registers, read about MSP430 assembly language.

Troubleshooting

  • The outputs never turn on. Check that the switch really connects P1.3 to the supply, and that the pull-down resistor goes to ground.
  • The outputs turn on by themselves. The pin is probably floating. Check that the pull-down resistor is connected.
  • The chip keeps resetting. Make sure the watchdog is stopped in the first instruction.
  • The program does not assemble. Check that the include file matches your device and that your assembler accepts these directives.

FAQ

How do I set an MSP430 pin as an input?

Clear its bit in the P1DIR register. Port pins are inputs after reset, so you only need to do this if you changed the direction earlier.

Which register do I read to get the pin state?

Read P1IN for port 1. Mask the bit you care about to isolate a single pin.

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

Do I always need an external pull-down resistor?

No. The MSP430 has internal pull resistors that you enable with P1REN. The P1OUT bit then selects whether the resistor pulls up or down. An external resistor is also fine and works the same way.

See also
ARM M4 Digital Input

What is the difference between polling and interrupts?

With polling, the program reads the pin repeatedly in a loop, as in this example. With interrupts, the hardware signals the CPU when the pin changes, so the CPU can do other tasks or sleep in the meantime.

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