Connections and commands

Time for real Hardware

How to connect the real world to a MSP430G2553 running Mecrisp:

Pinout of MSP430G2553

  • The dark blue pins with yellow letters are the one you really have to take care of to get Mecrisp running. Connect a voltage between 2.8V and 3,6V between Vcc (+) and GND (-). Put a pullup resistor of 4.7 kOhms to 47 kOhms between Reset and Vcc. Route Vcc, GND, Test and Reset to a well accessible four pin header for flashing the chip. Connect the serial lines RXD and TXD in your favourite way to your computer. Keep logic voltage levels in mind. (I use optocouplers for that.)
  • The light blue pins are those of Port 1. They can also serve as analog inputs, if desired. For example, to read an analog voltage on Pin 7 (A5):
    $4A constant ADC10AE0
    32 ADC10AE0 c!
    5 analog .
    
  • The violet pins are those of Port 2. Those are digital only. On startup, the Pins 18 and 19 are configured as low-frequency crystal oscillator and usually are used to connect a 32768 Hz clock crystal. If you want to use them as P2.6 and P2.7, clear the oscillator-select bits in P2SEL:
    $C0 $002E cbic!
    

Digital

Digital IO is basically done by this (shortened) list of registers:

$20 constant P1IN
$21 constant P1OUT
$22 constant P1DIR
$27 constant P1REN

$28 constant P2IN
$29 constant P2OUT
$2A constant P2DIR
$2F constant P2REN

I will explain those shortly:

  • PxDIR chooses between inputs (bits cleared) and outputs (bits set).
  • PxIN can be read to get the current electrical state of the Pins, whether they are inputs or outputs is not important.
  • PxOUT can be written with the states the outputs should have.
  • PxREN is a "Resistor enable" register. If a bit is set, it means that the state of PxOUT is given out through a internal resistor of about 35 kOhms (guranteed between 20 and 50 kOhms).

There are other registers for the Ports, but they are used for interrupt handling, which I will explain later and special functions.

Commands

For using ports and other registers those commands may be useful:

  • c! ( char c-addr ) Stores byte in memory
  • c@ ( c-addr -- char ) Fetches byte from memory
  • ! ( u|n a-addr -- ) Stores single number in memory
  • @ ( a-addr -- u|n ) Fetches single number from memory
  • cbit@ ( mask c-addr -- flag ) Test BIts in byte-location
  • bit@ ( mask a-addr -- flag ) Test BIts in word-location
  • cxor! ( mask c-addr -- ) Toggle bits in byte-location
  • xor! ( mask a-addr -- ) Toggle bits in word-location
  • cbic! ( mask c-addr -- ) Clear BIts in byte-location
  • bic! ( mask a-addr -- ) Clear BIts in word-location
  • cbis! ( mask c-addr -- ) Set BIts in byte-location
  • bis! ( mask a-addr -- ) Set BIts in word-location

Analog

There are three built-in words for analog input:

  • "analog": Reference voltage is 2.5V
  • "adc-1.5": Reference voltage is 1.5V
  • "adc-vcc": Reference voltage is current Vcc. This is useful for read sensors that put out a voltage relative to Vcc instead of an absolute voltage.

Don't forget to enable the pins as analog inputs in ADC10AE0 before, else you won't get the desired results.

The analog readings are given by Nadc = 1023 * Uin / Uref

There are two special analog channels, that dont' have to be enabled first:

  • 10 analog gives a reading of the temperature sensor
  • 11 analog gives a reading of current Vcc/2

So for printing the current Vcc value you can use:

: vcc. ."  Vcc is " 0  11 analog  204,6 f/ f. ." V " ;

The temperature sensor is less accurate and should be calibrated, but it least significant bit is known to fluctuate freely and provides a source of random noise. This can be exploited to get random numbers:

: random ( -- u )
 ( Random numbers with noise of temperature sensor on ADC )
   0
   16 0 do
     shl
    10 analog 1 and
    xor
  loop
;