Connections and commands
Time for real Hardware
How to connect the real world to a MSP430G2553 running Mecrisp:
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.
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
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 )
16 0 do
10 analog 1 and