9 Inputs and Outputs

Inputs are signals which go into the microcontroller, and Outputs are the signals which come out of the microcontroller. Inputs and Outputs  (or IOs for short) are what makes the microcontroller able to interact with the real outside world!

The MSP430 Microcontroller has several IO pins,  and most can be programmed to be either an Input or an Output pin. Each IO pin has a unique pinname which looks like Px_y. Where x is the port number and can be 1 or 2, and y is the bit number and can be any number from 0 to 7.  Pins are labelled P1_0 through to P2_7.

Outputs

To use an output pin we must first select the pin to be an output using the command  pindir( pinname, OUT ) This sets the pin direction to be an output, and then we use the command out( pinname , value ) to set the output voltage on that pin. For example:

pindir(P1_6, OUT)
out(P1_6, 1)

There are other commands that output signals on the microcontroller. The pulse commands are used to output a pulse from the microcontoller. A pulse is a signal which momentarily goes from 0 to 1 and then back to 0 again. These pulse commands are pindir(pin, PULSEOUT ), pulse_enable( ), pulse_period( ), and pulse_width( ). These commands can be used to control RC servo motors using pulse width modulation (or PWM).

Inputs

To use a pin for an input it must first be programmed as an input pin. This is done with the pindir(pinname, IN)  command. It can then be read using the inp( pinname ) command, and the value stored in a variable.

For example:

pindir(P1_6, OUT)
pindir(P1_0, OUT)
pindir(P1_3, INPU)             # Setup the pin as an input

while 1:
    sw = inp(P1_3)        # Read the input pin, pressed == 0, not pressed == 1
    if sw == 1:
        out(P1_6,1)       # Flash the green led
        wait(500)
        out(P1_6,0)
        wait(500)
    else:
        out(P1_0,1)       # Flash the red led
        wait(500)
        out(P1_0,0)
        wait(500)

We setup the input pin using thepin pindir( pinname, INPU ) command. Then the inp( pinname ) command is used to read the  input value from the switch. While  the switch is not pressed it will give a 1, but when it is pressed it will give a value of 0.  The program will flash the green LED when the switch is not pressed and flash red when it is pressed.

The following options are available for input pins.

pindir( pinname, IN )          selects the pin to be an input pin  (input is floating)
pindir( pinname, INPU )   selects the pin to be an input pin  with a pullup resistor
pindir( pinname, INPD )   selects the pin to be an input pin  with a pulldown resistor

The INPU input has a pullup resistor which will read a 1 when it is not connected to anything (ie if the switch is off). Whereas the INPD input has a pulldown resistor which will make the input look like a 0 when it is not connected to anything (ie if the switch is off). A IN input has no resistor so when it is not connected the voltage at the input will be floating and the may be read as a 1 or a 0 (ie if the switch is off).  It is not normally good to leave inputs floating, as it can sometimes cause hardware problems. When the mpy program starts all pins are automatically programmed as dirinpd inputs with a pulldown resistor, this is a safety measure to stop any of the pins from floating.

adc

There are other mpy commands which can be used to provide input to our program such the adc( pinname) command which measures the voltage at an input pin and gives an integer number.   adc(VCC) can be used to measure the battery supply voltage  and adc(TEMPERATURE) will measure the temperature of the chip.

random

The random( seed ) command generates a random number, is not really an input as such but it is commonly used to provide a random number input into your program to make it behave in a random and non-predictable way.