Egel project

for
MSP430G2553
on Launchpad or Egel Kit

egel
Willem Ouwerkerk with help from Albert Nijhof
juli 2016 (Added chapter 17, 18 & 112, februari 2017)

The 'Egel project' consists of about 50 elementary examples of hardware control with the MSP430 in the language forth. In each example you find a file with forth source code, documentation and links to more information on the internet.

photo
It is our aim to lower the hurdles that beginners with the MSP430 MPU find on their way to hardware programming using noForth software. With the many examples in the Egel project we hope to make it easier to understand the Texas Instruments documentation.

We recommend to read the chapters 1, 2 and 3 before you choose the chapters that have your special interest!
History
The original Egel werkboek was written in 1997 for the AT89C2051 microcontrollers by a group of Dutch Forth-gg members. It was initially named 'Postzegelboek' (dutch) but that was considered to be too long, so it was shortened to 'Egelboek'.
postzegel 'poss say ghull' = stamp
egel 'ay ghull' = hedgehog
boek 'book' = book

egel

Egel project table of content

egelegel

1-9 Simple I/O examples

egel

1
Basic port output

Byte wide output to I/O-port 2 wired with 6 to 8 3mm leds
photo The eight leds or a ledboard are placed on the breadboard with anode connected to P2.0, .. P2.7 (See the port doc. in SLAU144J.PDFpage 328 and beyond. P2SEL info on page 50 and beyond ofSLAS735J.PDF)

scheme All the cathodes of the leds must be connected together on the breadboard, and wired to ground using a 100 Ohm resistor.
Because this is the first example, we add some remarks. Once you have setup the hardware, you can paste the example in your terminal program.

The example sources have a rather technical comment. This is done in order to give you the background for making changes of your own if you use the hardware differently.

You may type the Forth commands shown below and see what happens. They terminate if you press any key. Study the source code, if you want to know how it is done!

Forth commands:
COUNTER ( -- )  Binary counter on P2
RUNNER ( -- )   Running light on P2
* Open images in a new window for a better resolution.

2
Basic port input and output

Byte output to I/O port wired with 6 to 8 leds and bit input using a switch
scheme
This chapter is an extension of chapter 1. As an addition the S2‑switch (already on the board) is wired to P1.3 for input.

The forth commands remain the same:
COUNTER ( -- )  Binary counter on P2
RUNNER ( -- )   Running light on P2
When the S2‑switch is held down the leds will run at a lower speed in COUNTER and RUNNER.

* Open images in a new window for a better resolution.

3
Onboard ADC

Analog to digital conversion using the internal AD converter, output to leds
photo This chapter is an extension of chapter 1 and 2. This time a potentiometer of 4K7 is added. Connect the center wire to P1.7 and the other two wires to VCC and ground.
More info about the ADC10, inSLAU144J.PDFon page 534 and beyond.
The forth commands:
SHOW-ADC1 ( -- )  Show ADC result in binary
SHOW-ADC2 ( -- )  Show ADC result as VU-meter
SHOW-ADC3 ( -- )  Show ADC result on screen as text
scheme
The only way these routines differ are in the way the results are shown.
* Open images in a new window for a better resolution.

4
RS232 using a Bluetooth module

Bluetooth communication, visualized on the leds
photo

The HC06 Bluetooth module behaves like any standard serial port. Only without wires! These modules needs a voltage from 3V to 4V. We only use the RS232 (TX/RX TTL levels) interface here. HC06 datasheet.

Connect the HC06 with the Launchpad:
On the board are 8 leds as in chapter 1.

scheme photo

photo
Two jumpers at J3 (TX and RX) must be removed.

Type STARTUP or press the reset key and all communication will go thru Bluetooth. Take care that you pair the HC06 first! The most used key is: 1234

All received characters are shown on the leds.

5
Relay control

Relay control, with or without a delay
Place a 5-Volt relay on the shield, as shown in the schematic and photo. A relay may be used as an on/off switch for high currents and/or high voltages. This example emulates the delay used for elevator doors.
Note that the driving transistor and the voltage powering the relay may need adaptation. The Egel kit already contains a mosfet, to drive the relay. Just connect the relay to the PWR-output!
photo
Bit input comes from P1.3 (S2).
Bit output goes to P2.4.

Forth command:  RELAY-CONTROL ( -- )
By default an off delay is performed here.
scheme
Try an other kind of delay, by changing the lines in the word RELAY-CONTROL with the backslashes. But select only one option at a time!

6
Unipolar stepper motor

Controlling a four phase unipolar stepper motor
photo
scheme Editing the word ONE-STEP you may select; single phase, two phase and half step driven stepper control.

The used module has a ULN2003 driver on it. It needs only 5 wires to control the motor.



Launchpad - driver connections:
Forth command:  STEPPER ( -- )
The default method is a one-phase stepper, change the word ONE-STEP to use other methods. But only one at a time!

Change direction with S2‑switch.

About stepper motors

7
Bipolar stepper motor

Controlling a two phase bipolar stepper motor
photo
scheme Editing the progam you may select; single phase, two phase and half step driven stepper control.

The used module has two L9110S drivers on it. It needs only 4 wires to control the motor.



Launchpad - driver connections:

Forth command:  STEPPER ( -- )
The default method is a one-phase stepper, change the word ONE-STEP to use other methods. But only one at a time!

Change direction with S2‑switch.

About stepper motors

8
Ultrasonic distance measurement

Controlling a HC-SR04 ranging sensor
photo

scheme Connect the SR04 sensor with the MSP430 and place the led-board on the shield.
P1.3 is the input bit, the pulselength gives the distance in microseconds.
P1.4 is the output bit, a positive pulse starts a new measurement.

This example is software timed, so the result is very much dependent of
1) The clock frequency
2) Active interrupts
3) The Forth implementation

Forth command:
MEASURE - Show distance in steps of 2cm binary on leds

The usable range of some cheap Chinese HC-SR04 is only 2cm to 220cm.

signals
About ultrasonic sensors.

9
PWM using compare unit

Pulsewidth modulation
Connect two switches, a transistor and a power led using the schematic and photo.

Timer TA1 is used as a pulsewidth modulator with the output on P2.4.

The PWM freqency is about 4KHz, the resolution is 1000 steps.

Forth commands:
CYCLUS ( -- )       Cycle once thru PWM range
POWERCONTROL ( -- ) Set PWM value using two switches

photo The two switches control the duty cycle. This becomes visible as the varying brightness of a medium power led.

The 'up' key is connected to P1.4,
the 'down' key is connected to P1.5.
scheme This is a simple program, because we use the internal MSP430 hardware. A full software solution would use all available CPU time.

A Video on PWM

10-19 More advanced I/O examples

egel

10
RC-5 coder/transmitter

RC-5-bitpatterns generated by software
scheme
photo
Load the assembler before the e10 noForth programs, the software RS232 routines are in assembler code.
For the Egel Kit: just connect an IR-led to the LED connector and ready. Using the launchpad you have to add the resistor too both mounted on the shield.

Timer A1 generates 36KHz pulses 25% PWM at P2.1.
The S2‑switch activates the transmitter.

More info about timer-A1, inSLAU144J.PDFon page 356 and beyond. Configuration of the I/O-pins from page 50 and beyond ofSLAS735J.PDF

Forth command:
RC-TRANSMITTER ( -- ) Send the RC5 on/off command

This program will startup after every reset. If you do not want this program to start, press reset while S2 is held down.

About the RC-5 protocol

11
Seven segment driver with CD4094

Serial to parallel using a shiftregister
photo
scheme
The eight output pins (4 to 11) of the 74HC4094 must be connected to a 7-segment display. See schematic.

scheme
The eight output pins (15 and then 1 to 7) of the 74HC595 must be connected to a 7-segment display. See also schematic.

For more digits several 74HC4094s or 74HC595s may be connected to each other, strobe and clock in parallel. See it's schematics also.

You may also use common anode displays. The software e11a is for common anode displays. Software e11b is for the shown common cathode displays. For common anode displays the ground connections (see schematics) on the displays must be connected to VCC.


Forth commands:
CHARACTERS ( -- ) Show all implemented characters
COUNTER ( -- )    Show a decimal counter
DICE ( -- )       Simulate a tumbling dice

A 3 cm. high common anode 7-segment module
Background on seven segment displays

12
Controlling a character lcd

With a 4-bit parallel interface & contrast using PWM
photo
scheme Connect 3 Volt version of an 1602 LCD display with P2.0 .. P2.5.
P1.6 is the contrast control output for the LCD.
Timer-A1 is used for contrast control of the LCD, see schematic.


When the text can not be read, just play with the contrast by changing the value using >CONTRAST ( +n -- ) the value may be from 0 to 16.

Forth commands:
>CONTRAST ( +n -- ) Set contrast to +n
DEMO1 ( -- )        Show text message 1
DEMO2 ( -- )        Show text message 2

13
Hi-resolution US distance measurement

Using timer-A0 for increased accuracy, with timeout!
photo

scheme Connect the SR04 sensor or similar, with four wires to the MSP430, place a ledboard using the schematics. In this example we use timer‑A0 with a microsecond resolution.
When timer‑A0 is allready in use, just change the register addresses to those of a similar timer that is free.

Note that, activated interrupts will not influence the result. This variant will never hang, it times out after about 65 milliseconds.

photo The usable range of some cheap Chinese HC-SR04 modules is 2cm to 220cm. Other variants may have a different range.
The word DISTANCE starts the US-sensor, and gives a distance back. This result is the distance to the nearest object in centimeter!

When the word >MM is used in DISTANCE instead of >CM the resulting distance is converted to millimeters.

The photo shows three other US-sensors, all three are pincompatible replacements for the HC-SR04. The US-015 is more stable and has more accurate readings. The RCW-0001 is much smaller and works from 3 Volt.

The US-100 works from 2.4 Volt, but also has a separate serial mode that has an increased resolution. It is very stable due to the built-in temperature compensation. The distance is given in millimeters, see chapter E17 for example code!

Forth commands:
US-ON ( -- )       Activate ultrasonic hardware
DISTANCE ( -- cm ) Show object distance in centimeter
MEASURE ( -- )     Show object distance on leds

14
Generating dual frequencies with timers (only)

Frequency output using timer-A0 & timer-A1
scheme
The outputs are P1.6 and P2.1. photo Port-1.6 must be wired to a 32 to 50 Ohm speaker, the other connection of the speaker to ground, P2.1 ditto.

Program 14a plays simple music scores, program 14b generates DTMF tones!


  Play the hunting horn

The frequency range of both outputs is 61 Hz to 39000 Hz.

signals
  Call the Forth users group

The forth commands for e14a:
HONK    ( -- )  Sound a heavy horn
HUNTING ( -- )  Play the hunting horn
BJ      ( -- )  Play Brother John
And for e14b:
>DTMF ( addr u -- ) Convert string to DTMF tones
WILLEMO ( -- )      Call the author
FORTHGG ( -- )      Call Forth-gg chairman

15
MSP430 clocks

How to activate the different clocks on the MSP430
Connect an ampere-meter to check the current used at different frequencies. Program e15a uses 9600 baud, and works fine on all versions of noForth for MSP430G2553 Launchpad.

Program e15b does the same, but uses 38400 baud, and works on the Egel Kit only. Because the oscillator frequency is to low, there is no baudrate of 38K4 possible on 32kHz and using the VLO.

The VLO and 32kHz noForth activates the Very Low Frequency or 32kHz oscillator and outputs the selected clock on P1.0. You may check with a scope or logic analyser if they are working. Information about the built-in capacitators for the 32kHz oscillator, see SLAU144J.PDF page 274 and beyond.

photo The data for the DCO starts at address 10F6 in the info-flash segment A at 10C0. The flash clock must be in the range from from 257KHz to 476KHz! Note that the flash clock will not function with a 32kHz crystal. The default selected baudrate here is always 9600 baud.
photo With the 12kHz VLOCLK mode no baudrate is possible, this is because the VLO frequency is to low and not accurate enough!
photo The Forth commands:
12KHZ ( -- ) Activate VLO oscillator
32KHZ ( -- ) Activate 32kHz oscillator
1MHZ ( -- )  Activate DCO on 1 MHz
8MHZ ( -- )  DCO on 8MHz
16MHZ ( -- ) DCO on 16MHz
FLASH ( ccc -- ) Run flasher on freq. 'ccc'

Use the word FLASH as follows: FLASH 32KHZ or one of the other frequencies. It shows that the timing of the MSP430 keeps working correct, not dependig on the used system clock. It does this by flashing both leds every 6 seconds.
After the program is finished, the default MPU frequency is restored.

16
Half, 3/4 & full duplex software UART

Bit input on P2.1 bit output to P2.0
photo
scheme
Load the assembler before the e16 noForth programs, the software RS232 routines are in assembler code.

photo First make a second RS232 connection. Connection RX to P2.0 (logic level) and TX to P2.1 do not forget to connect ground. Then open a second terminal window for the new connected port. Press reset button, the output should now appear in the newly opened terminal window.
To go back to the old configuration hold S2 and then press reset. The newly added RS-ON routine is skipped and your back in the original configuration.



The first variant (e16a) is half duplex and uses bitbanging only. The second variant (e16b) reads incoming characters using a timer interrupt. This gives you about 3/4 duplex, that is receiving works fabulous. The sending of characters is occasionally spoiled by the reception interrupt. The third and last variant (e16c) uses a transmit interrupt too. This gives you full duplex, but uses all the timers on board of the MSP430G2553.

signals
Test commands in the old RS232 configuration (before giving reset):

Type: RS-ON To initialise software RS232.
Type: CH # RS-EMIT MANY To test new RS232 output.
Type: COLD When it these routines work.
Check the results on your screen in the terminal windows.

17
US-100 ultrasonic distance sensor

Using timer-A0 for software RS232, with timeout!
photo

scheme Connect the US-100 sensor with four wires to the MSP430. In this example we use timer‑A0 for RS232 reception.



When timer‑A0 is allready in use, just change the register addresses to those of a similar timer that is free.

photo The distance measured by US-100 modules is very stable. The result is given in mm. The range of the sensor is about 30mm to 4000mm.
The word DISTANCE starts the US-sensor, and gives a distance to the nearest object in millimeter back.

The US-100 works from 2.4 Volt to 5 Volt, the used serial mode that has an increased resolution. It is also very stable due to the built-in temperature compensation. The distance is given in millimeters.

tracer

Forth commands:
RS-ON ( -- )       Activate sofware serial interface
DISTANCE ( -- mm ) Show object distance in centimeter
MEASURE ( -- )     Show object distance on screen

18
Simple robotic sensor unit

Reading touch sensor and a PIR motion detector

Print layout for the simple robotic sensor unit, on it two feelers a PIR sensor and two leds.

The internal schematic of the SB312A smart PIR sensor unit. Note that the sensor has it's own 3 Volt voltage stabiliser. The output already has a 10 kOhm resistor.

Forth commands:
MOTION?      ( -- fl )  True when PIR sensor active
TOUCH-LEFT?  ( -- fl )  True when left feeler bend
TOUCH-RIGHT? ( -- fl )  True when right feeler bend
?LED         ( fl -- )  Leds on when fl is true
SENSORS      ( -- )     Test the sensor module

20-29 Use of (timer)interrupts

egel

20
Timer A0 compare mode

Flashing LEDS with a timer compare interrupt
photo Using timer A0 in compare mode to generate timer interrupts. SMCLK (high frequency) or ACLK (low frequency) are used to drive the timer.
Load the assembler before the e10 noForth programs, the software RS232 routines are in assembler code.

Look back to chapter 15 for more info about MSP430 clocks.
Compare mode 2 is used here, so we count up to FFFF.

Change the flash frequency of the leds by changing PERIOD.
Example: 5 TO PERIOD
Higher means slower. The default content of PERIOD is 1.

The forth commands:
SMCLK-ON ( -- ) Activate timer-A0 with SMCLK
ACLK-ON ( -- )  Activate timer-A0 with ACLK
TIMER-OFF ( -- )Deactivate timer-A0

21
RC servo controller

Bit output, using timer-A0 to control the pulsewidth of 2 model servos
photo
scheme
Load the assembler before the e21 noForth programs, the software RS232 routines are in assembler code.

Connect P1.4 and P1.5 to the signal line of each servo, see the schematics and photo. An example of the use of (timer)interrupts.

The word MOVE-SERVOS changes the position of each servo endless. A servo may be changed by hand using the word SERVO ( pos srv -- )
signals
The forth commands:
SERVO-ON ( -- )  Activate servo interrupt
SERVO-OFF ( -- ) Deactivate servo interrupt
SERVO ( position servo -- ) Set 'servo' to 'position'
MOVE-SERVOS ( -- ) Move both servos endlessly
About RC servos

22
Low frequency PWM using timer interrupt

Software PWM (on timer-A0) with three outputs
scheme photo
Load the assembler before the e22 noForth program.

Connect a motor, lamp or power led to P2.4 using a mosfet, see schematics. We have three outputs on P2.2, P2.3 and P2.4, in steps from 0 to 100. The frequency is 300 Hz, so there is a 3,3msec. update rate. The example program changes all 3 outputs differently every 64 milliseconds.

Due to the software structure, the PWM interrupt leaves small glitches when power is zero. Here is a small challenge left for the programmer.

signals
Forth command:
SHOW-PWM ( -- ) Change all three PWM outputs endlessly

About PWM

23
Hardware interrupt

Hardware interrupt with bit input and output on P1 and P2
scheme
photo
Load the assembler before the e23 noForth program, the interrupt routine is in assembler code.
Connect one toggle switch and a ledboard to the MSP430, using the schematics. S2 is already on the board. The red led is controlled in a high-level software loop (polling), the green led by an hardware interrupt!

SW1 triggers a hardware interrupt. S2 uses polling in the word LED. There is only simple debouncing in this example, the delay value in the polling loop is used for that!

Forth command:
LED ( -- )
One led toggle's with polling, the other one on interrupt.

24
Watchdog timer

Watchdog initialisation and use
photo
photo The primary function of a watchdog timer, is to perform a controlled system restart after a software problem occurs.

The activating time of the watchdog may be changed. This may be done with an other clock (ACLK or SMCLK) and/or an other clock divider.

In this example the clock is set to SMCLK. The lowest two bits of the WDTCL register set the clock divider. The divider is set to 00 = SMCLK/32768. More info on page 346 in SLAU144J.PDF


The value 'u' is a delay value for a simple software loop! When the loop is long enough the watchdog takes control by resetting the MPU!

Forth command:
    WATCHDOG  ( u -- )
Test watchdog trigger point to 'u' milliseconds. In steps of 160 millisec.

25
Watchdog as interval timer

Watchdog used as interval timer to implement an accurate MS routine
The lowest two bits of the WDTCL register set the clock divider. In this example the divider is set to 01 = SMCLK/8192. More info on page 344 and beyond in SLAU144J.PDF

photo The watchdog interrupt lowers the value MS) to zero.

Forth command:
INTERVAL  ( ms -- )
Set red led on when interval starts, green led goes on when the interval is finished.

26
RC-5 decoder

RC-5 remote control decoder and power control for MSP430 Launchpad
scheme
The code is an example of
two cooperating interrupt routines.
connection
Load the assembler before the e26 noForth program.
photo When you have an Egel kit, just connect a 36 KHz infrared receiver to the RC5 connector. For the Launchpad you have to add some wires, see schematic! The IR-receiver must be a 3 Volt version, like TSOP34536.
As output a power led must be connected to the LED connector with a 47 Ohm series resistor. It may also be connected to the PWR connector (max. 2 Amp).

For the example RC-TEST, connect 6 leds or a led-module with P2.2 to P2.7. RC-TEST shows the received 6-bits key-code on the leds.

Forth commands:
RC-LAMP  ( -- ) Control output P2.1 using on/off key
RC-POWER ( -- ) Control output P2.4 using on/off key
RC-TEST  ( -- ) Display RC-5 commands binary on led array
signals
Above: output from the RC-5 decoder; below: power led.

27
Period time measurement

Bit input on P1.3 to sample timer-A0
photo
photo
Load the assembler before the e27 noForth programs, the interrupt routines are in assembler code.
About the three example programs:
Pressing S2 twice, toggles P1.3 twice, then one measurement is done. When a 3,3 Volt logical pulse is connected to P1.3, external signals may be measured.
When the signal to measure has a to low or to high voltage, suitable conversion must be done. Add amplification (when the signal is too small) or an attenuator network (when the signal is too large).

signals
An other way to test this code is adding a second Egel kit or Launchpad. This second kit must be running the software E14. Connect output P1.6 of the E14 example to input P1.3 of this example.

Type: 100HZ TONE0 in the E14 example.
Type: FREQUENCY in the E27B example!
Check the results on your screen.

Each example has one forth command:
PERIOD    ( -- )    \ File e27a, measure period time
FREQUENCY ( -- )    \ File e27b, measure frequency
RPM       ( -- )    \ File e27c, measure rotation speed

30-39 I2C using USCI or bitbang interface

egel

30
I2C input & output to PCF8574

I2C input & output to an 8-bit parallel I/O-driver

photo
scheme
For the bitbang version it is necessary that the I2C-primitives are already loaded.
Please do not try to understand this file. A documented version comes in chapter 37!
The complete I2C-bitbang source code can be found in chapter 37!
All I2C examples, excluding chapter 38 come in a USCI and bitbang implementation! A description of USCI as I2C can be found in SLAU144J.PDF from page 449 to page 473, the register documention starts on page 467.

Connect for example the I2C-print from the Dutch Forth users group, or any other module with a PCF8574 and 8 leds. Connect the power lines first, then P1.7 to SDA and P1.6 to SCL. Take care, there are two versions of the PCF8574, the A-version has a different device address!
Do not forget to mount the two pullup resistors.

For RUNNER2 and SHOW we need a second PCF8574 with eight switches connected to it.

signals
The forth commands:
RUNNER1 ( -- ) Running light with output only
RUNNER2 ( -- ) Running light with switch input too
SHOW    ( -- ) Show I2C input on outputs
About I2C

31
I2C I/O and 24C02 EEPROM

I2C input & output to an 8-bit parallel I/O-driver and EEPROM
scheme photo

For both versions it is necessary that the correct I2C-primitives are already loaded.
Connect for example the I2C-print from the Dutch Forth users group, or any other module with an 24C02 EEPROM, a PCF8574 with 8 leds and a PCF8574 with 8 switches.
This example first samples the 8574 inputs for 10 seconds. These patterns in are stored in EEPROM. Then this pattern is played at the other 8574 with 8 leds connected.
Connect VCC, GND, P1.7 to SDA and P1.6 to SCL. Do not forget that two pullup resistors have te be mounted.

The forth command:
EEPROM-DEMO ( -- ) 
Sample switches to EEPROM for 10 sec. and display them.
About the I2C bus protocol

32
I2C timer and clock with PCF8583

I2C output to an 8-bit parallel I/O-driver and a clock chip
scheme photo
For both versions it is necessary that the correct I2C-primitives are already loaded.
Connect for example the I2C-print from the Dutch Forth users group or any other module with a PCF8574 and PCF8583.
Pin P1.7 is SDA and P1.6 is SCL. Two pullup resistors have to be mounted. 0A0 = PCF8583 I2C-bus identification address 0.

The forth commands:
TIMER ( -- ) Flash leds every 10 seconds
ALARM ( sec min -- ) Alarm goes off after given time
CLOCK ( -- ) Show 24-hour clock
SET-CLOCK ( sec min hr -- ) Set 24-hour clock
About the I2C bus protocol

33
I2C ADC and DAC with PCF8591

I2C input from an ADC and output to an 8-bit parallel I/O-driver
scheme photo
For both versions it is necessary that the correct I2C-primitives are already loaded.
Connect for example the I2C-print from the Dutch Forth users group or any other module with a PCF8574 and 8 leds and a PCF8591.
Connect VCC, GND, P1.7 to SDA and P1.6 to SCL.
Do not forget that two pullup resistors have te be mounted.
094 = PCF8591 I2C-bus identification address 2.

The forth commands:
ADC ( +n -- byte ) Read ADC input '+n'
DAC ( byte -- ) Write DAC output with 'byte'
ANALOG ( -- ) Output analog data on leds
About the I2C bus protocol

34
I2C DS1307 clock and 24C32 EEPROM

Using a Smart Electronics I2C RTC DS1307
photo
scheme
For both versions version it is necessary that the correct I2C-primitives are already loaded.
Connect the Tiny RTC I2C-module from AliExpress (or any other module) with a DS1307 RTC and 24C32 EEPROM and connect VCC, GND, P1.7 to SDA and P1.6 to SCL. Do not forget that two pullup resistors have te be mounted.

Forth commands:
TIMER ( -- ) Flash leds every 10 seconds
ALARM ( sec min -- ) Alarm goes off after given time
SET-CLOCK ( sec min hr -- ) Set 24-hour clock
CLOCK ( -- ) Show 24-hour clock
SHOW ( -- ) Show data from EEPROM
GATHER ( -- ) Store clock data in EEPROM

35
I2C character LCD using a PCF8574

I2C output to an LCD using an 8-bit parallel I/O-driver
photo
scheme
For both versions it is necessary that the correct I2C-primitives are already loaded.
Connect a 3 or 5 Volt version of an 1602 LCD display to an I2C adapter print. Only four wires are needed: VCC, GND, SDA=P1.7, & SCL=P1.6

Use the potmeter for contrast control, see schematic.

The forth commands:
LCD-SETUP ( -- ) Initialise LCD
LCD-EMIT ( ch -- ) Send character 'ch' to LCD
LCD-TYPE ( adr u -- ) Type string 'a' 'u' to LCD
DEMO ( -- ) Type a message on LCD

36
I2C adc module YL-40

I2C analog input using a PCF8591 module YL-40, from AliExpress
photo
photo
For both versions it is necessary that the correct I2C-primitives are already loaded.

Connect the I2C-module with PCF8591 to P1.7 to SDA and P1.6 to SCL.
090 = PCF8591 I2C-bus identification address 0.

The PCF8591 has four 8-bit ADC inputs and one 8-bit DAC output. The used module is wired as shown below.

0 ADC ( Read AIN0 = LDR )
1 ADC ( Read AIN1 = Thermistor )
2 ADC ( Read AIN2 = Free )
3 ADC ( Read AIN3 = Potmeter )
DAC is connected to an output and a green led.

Forth commands:
ADC  ( +n -- byte ) Read ADC input '+n' leaving 'byte'
DAC  ( byte -- ) Write 'byte' to DAC output
ANALOG  ( +n -- ) Read input '+n' send to screen & DAC

37
I2C basics at P1

Primitives for the bitbang & USCI versions of I2C
photo
photo The files can be used to construct either bitbang or USCI applications.
Connect for example the I2C-print from the Forth users group or any other module with I2C compatible chip(s).
Connect VCC, GND, P1.7 to SDA and P1.6 to SCL.
Do not forget that two pullup resistors have te be mounted.

Note that the bitbang implementation does not use clockbit stretching.

For the USCI implementation:

The most hard to find data, are those for the selection registers. To find the data for the selection register of Port-1 here 026 and 041 you have to go to the "Port Schematics". For P1.6 and P1.7 these are page 48 and 49 of SLAS735J.PDF These tables say which function will be on each I/O-bit, at a specific setting of the registers.

Address 026 - P1SEL, port-1 selection register
Address 041 - P1SEL2, port-1 selection register 2

A description of USCI as I2C can be found in SLAU144J.PDF from page 449 to page 473, the register description starts at page 468. The UCB0CTL0 register selects the SPI- or I2C-mode we are in.


About the I2C bus protocol

38
I2C USCI master and slave implementation

I2C slave input and output
photo
photo
Load the assembler before the e38a/b noForth programs, the software RS232 routines are in assembler code.
The bitrate values are for an 8 MHz DCO, for 16 MHz they should be doubled.

This code is improved, the routines work very solid, >SLAVE and SLAVE> give an error mesage when the addressed device does not respond.
Program e38a implements the I2C master. The program e38b implements the I2C slave. For both versions there are output- and input-examples. See the list below.

photo Connect one ore more MSP-EXP430G2 Launchpads. You may also use Egel Kits (from the Forth users group). Add 6 or 8 leds and connect VCC, GND, P1.7 to SDA and P1.6 to SCL.
Don't forget the two pullup resistors of 10k and two line protection resistors of 220 Ohm. See schematics.

The I2C-slave examples must be used in pairs, one MPU executes a word from the first table, the other has to execute the word on the second table.
The master command MASTER-TRIO implements a master with two slaves!

Forth commands e38um:
MASTER-OUT ( -- )   Show a running light on leds & slave 1
MASTER-IN ( -- )    Change flash speed with data from slave
MASTER-IO ( -- )    Running light, delay with S2 from slave
MASTER-TRIO ( -- )  Count on slave 1, running light slave 2
                    Change speed with S2 from slave 1
Forth commands e38us:
SLAVE-IN1 ( -- )    Display data from master
SLAVE-OUT ( -- )    Send S2 to master, flash leds
SLAVE-IO ( -- )     Get & send data from/to master
SLAVE-IN2 ( -- )    Display data in slave 2
scheme scheme
Start the I2C-slave before you start the I2C-master! The slave controller always waits for the master to claim the slave.
The second example file contains a master with two slaves.

signals
A video introduction to I2C master/slave I2C

40-49 SPI using USCI or bitbang interface

egel

40
3-wire USCI SPI doing I/O

Hardware SPI master & slave
photo
scheme Connect the SPI lines of USCI-B0 P1.5, P1.6 & P1.7 to the same pins on the other board. Connect 6 leds to P2 and start the slave on the unit with the led board.
More info on page 445 of SLAU144J.PDF.
Configuration of the pins on page 49 of SLAS735J.PDF.

The forth commands:
SPI-MASTER ( -- ) Send binary counter, display answer
SPI-SLAVE1 ( -- ) Display received data & sent it back
SPI-SLAVE2 ( -- ) Translation of TI-example

signals
About SPI

41
3-wire USCI SPI sending analog data

ADC & hardware SPI I/O
photo
scheme Connect the SPI lines of USCI-B0 P1.5, P1.6 & P1.7 to the same pins on the other board.
Connect 6 leds to P2 and start the slave on the unit with the led board.
P1.3 on the MASTER is used as analog input, it is wired to a 4k7 potmeter to 3,3 Volt and ground.

More info on page 445 of SLAU144J.PDF.
Pin configuration on page 49 of SLAS735J.PDF

The forth commands:
MASTER ( -- ) Send ADC to slave, receive dat from slave
SLAVE ( -- )  Display ADC, sent counter back
signals
About SPI

42
4-digit 7-segment tube

Using 3-wire bitbang SPI
The program uses a compare timer interrupt with machine code to control a seven segment LED-display with 4-digits.
scheme
The display module has two 74HC595 shiftregister chips as drivers.
It takes 4x39=156 microseconds every 12388 microseconds to fill the tube display, that is about 1.26% of the MSP430 cpu time.
photo
Load the assembler before the e42 noForth programs, the software RS232 routines are in assembler code.
Connect a 74HC595 display module:
Pin-1 = VCC,
Pin-2 = P2.2 (SCLK),
Pin-3 = P2.0 (RCLK),
Pin-4 = P2.1 (DIO),
Pin-5 = Ground.
All outputs are connected to a 7-segment display module. See schematic.

On the 74HC595:
OE\ = Low (when OE\ is high the outputs are disabled),
MR = High (when MR=low the shiftregister is reset),
DS = The data input,
SHCP = The clock input,
STCP = Strobe and acts on a positive transition.

Forth commands:
COUNTER ( -- )  Run counter on tube display
TU. ( u -- )    Print number on tube
TCLR ( -- )     Clear tube display
TDASH ( -- )    Print dashes on tube
TMESSAGE ( -- ) Print message on tube

43
nRF24L01+ send/receive

Using 4-wire USCI & bitbang SPI
scheme
Program 43a and 43b do the same, the a-version using a bitbang interface. The b-version uses the USCI interface from the MSP430.

photo Interfacing a nRF24L01+ thru SPI with two Launchpad boards and/or Egel Kits.
photo Carefully connect the SPI lines of USCI-B0 with the nRF24L01+ using Launchpad:

P1.5 = CLOCKPULSE,
P1.6 = DATA-IN,
P1.7 = DATA-OUT
P1.4 = CSN,
P2.3 = CE.
scheme On the Egel Kit you can simply put the module on the connector marked nRF24L01. Do the same with the other board. Connect a powerled & 47 Ohm resistor, and a LDR & 2k2 resistor for the remote control examples.
Note that, a good power supply and decoupling right near the nRF24L01+ module are very important.

The Egel Kit vsn-2 has an extra 22μF near the nRF24L01+ power connections. Both the Launchpad and the Egel Kit vsn-1 may need an extra 10μF to 22μF for decoupling!

photo SPI reg: SLAU144J.PDF page 445.
SPI pins: SLAS735J.PDF page 49.

nRF24L01+ background
nRF24L01+ antenna modification


nRF24L01+ module connections

Forth commands (these words are all stack neutral):

CHECK ( -- ) Control disturbance on the selected RF-channel (channel 112 of 128).
CONTROL & LAMP Simple one way remote control.
NRF-SEND & NRF-REC A counter demo, displayed on both boards (bi-directional communication).
REMOTE & RECEIVER Remote controlled lamp, with lamp status feedback.

table An nRF24L01 introduction

44
WS2812 RGB-led controller

Using 1-wire USCI SPI for bit output
signals photo
Program e44a is a slighty more easy to understand version of the WS2812 led controller. The program e44b has a virtual field length as extra for longer strings of leds. With this field function, longer led strings can be addressed in smaller chunks that are repeated to the end of the led string.

scheme Connect a WS2812 ledstrip with 4 to 1000 leds to P1.7, +5 Volt and ground. Note that this code runs on a 16MHz clock!
USCI B0 is used to generate the bit pulses. The pulse timing is a little over the maximum frequency of 800kHz, but the WS2812 has no problems with that. All the demo's are just within WS2812 timing parameters (see drawings), sometimes the low part of the bit-pulses are streched to 4700 nanosec. Note also, that the maximum is 5000 nanosec. Longer low pulses are seen as strobe!

It is possible to control over a 1000 leds using this code. It takes about 33 millisec. to address this much leds. Just type dm 1000 >leds or the actual number of leds connected.
The time to address one led is about 0.033 millisec.
Take care 30 leds take about 9 Watt, that is almost 2 Ampere!

signals


A few of the Forth user commands for e44a:
>LEDS ( +n -- )  - Set number of leds connected
ALL  ( dc -- )   - Set all leds to color (double number)
                   from the stack
S1 S2 S3 ( -- )  - Three colorful shiftreg. examples
S4 S5 ( -- )     - Two multidot shiftregister examples
RAINBOW ( -- )   - Set a rainbow divided over all leds
A few of the Forth user commands for e44b:
>FIELD ( +n -- ) - Set led field length
MSHIFT ( -- )    - Multidot shiftregister example
VOLUME ( -- )    - Volume effect example
BOUNCE ( -- )    - Bouncing dot example
RAINBOW> ( -- )  - Shift a rainbow divided over all leds
MSP430 & WS2812 demo

50-59 Use of Low Power Modes

egel

50
Use of Low Power modes (LPM)

Wake on hardware interrupt
scheme
photo
The assembler must be loaded before the e50 noForth program.
Connect a ledboard, see schematics, and ampere meter to check the current used in different modes.

Low Power Modes examples, using bit-input and bit-output. A hardware interrupt with machine code is used to wakeup, using Port-1 and Port-2.

The S2‑switch triggers the hardware-interrupt. This interrupt wakes up the MSP430 and activates a running light once. The MSP430 goes to a low power mode right after that.

The settings for P1.3 can be found from page 327 and beyond in SLAU144J.PDF

When you are using the Egel Kit, the jumper named ON/OFF may be used, to measure the current taken by the MSP430. Thus it is easy to see how the low power modes work out. Use on the launchpad, the jumper named VCC. This can be found on jumper strip J3.

diagram Forth commands:
SLEEP0 ( -- ) Test LPM mode 0
SLEEP2 ( -- ) Test LPM mode 2
SLEEP3 ( -- ) Test LPM mode 3
SLEEP4 ( -- ) Test LPM mode 4
(the sleep numbers indicate the low power mode )

A lecture on Low Power Modes and about Launchpad power usage

51
Using Low Power Modes (LPM)

Wakeup using the watchdog interval timer
photo photo
The assembler must be loaded before the e51 noForth program.

Connect an ampere meter to check the current used in different modes.

Watchdog as interval timer using LPM0, without use of LPM0 the CPU uses about 2,56mA (green led on ~10mA) with LPM0 it is ~0,45mA.
The lowest two bits of the WDTCL register do set the clock divider, here it is set to 01 = SMCLK/8192. The watchdog interrupt routine lowers the value MS) to zero.

diagram
The forth commands:
MS ( ms -- ) Wait in LPM0
INTERVAL ( ms -- ) Idem

52
Low power switching clocks

32KHz interval timer & 8MHz DCO clock on wakeup
scheme
photo
Load the assembler before the e52 noForth program.

Connect a ledboard, see schematics, and ampere meter to check the current used in different modes.
In this example we use Low Power Modes, bit output, watchdog interrupt and machine code. When the program enters a low power mode, it runs on the 32kHz crystal. When active it uses the 8MHz DCO. The watchdog is used as interval timer, in steps of one second. Each time when it runs out, a running light is activated.

photo Current while full running!

The forth commands:
SLEEP0 ( -- ) Test LPM mode 0
SLEEP2 ( -- ) Test LPM mode 2
SLEEP3 ( -- ) Test LPM mode 3
The number in name of the forth commands, indicates the low power mode that is used.

53
Low power RC-5 decoder

Consuming only 1/6 of the current
photo
The assembler must be loaded before the e53 noForth program.

Put a 36 KHz infrared receiver on the RC5 connector. It must be a 3 Volt version like TSOP34536.
Put a power led on the LED connector (with a 47 Ohm series resistor), or on the PWR connector (max. 2 Amp).

scheme Connect 6 leds or a led-module with P2.2 to P2.7.
Use the schematic and build a shield (see general intro.) when you have a Launchpad board.
RC-TEST puts the received 6-bits key-code on leds. This is an example of two cooperating interrupt routines. Note that the only changes in the code, to make this routine low power, is the addition of the word LPM4 and the code on the lines marked with *** !

This low power version uses only 1/6 of the original version. This could be less, by using an even more low power version of the infrared receiver, or by using more clever tricks!

Forth commands:
RC-LAMP  ( -- ) Control output P2.1
RC-POWER ( -- ) Control output P2.4
RC-TEST  ( -- ) Display RC-5 commands on leds

54
Low power noForth

Without loss of functionality

Load the assembler before you load the e54 noForth program.
photo photo photo
The code for this version shows all the functions needed to build a low power version of noForth. It uses an unused bit in the OK register to add a switch. This switch selects the depth of the used sleep mode. The word SLEEP must be implemented at the beginning of the forth word ACCEPT.

The word MS1 always uses LMP0. Bit-3 in the OK register holds the sleep mode for this example.

A zero means minimal use of sleep modes, in this example it uses LPM0, while waiting for a key. Most clocks keep on running in LPM0.

When bit-3 is set, this example uses deeper sleep modes. Like going into LPM4 in SLEEP and using LPM2 to further save power, while waiting for a key.

Connect an ampere meter to check the current used in different modes. Note that most used current meters are not accurate when measuring short current bursts. This can be rectified by adding a large capacitor.

    8 ADR OK **BIS      \ set bit 3
    8 ADR OK **BIC      \ clear bit 3

About the average current use.

Standard 8MHz noForth uses about 10mA, without leds that is ~2,6mA. The first number is the current while the system waits for a key, the second number is the average current while running.

DCO freq.   LPM4-sleep       LPM2            LPM0
 1 MHz      0.0007mA    0.020mA/0.18mA  0.070mA/0.18mA
 8 MHz      0.0007mA    0.025mA/0.50mA  0.346mA/0.50mA
16 MHz      0.0007mA    0.033mA/0.83mA  0.682mA/0.83mA

Forth commands:
1MHZ ( -- )      DCO on 1 MHz
16MHZ ( -- )     DCO on 16 MHz
SLEEP-ON ( -- )  Install LPM version of noForth
SLEEP-OFF ( -- ) Back to normal noForth version
MS1 ( ms -- )    LPM version of MS
SLEEP ( -- )     Go to LPM mode 4

60-69 Advanced interfacing

egel

60
128x64 bits graphics display

SSD1306 I2C display driver
photo Graphics & 8x14 characters
schematic
Load the USCI I2C master before the e60 noForth programs. The software for SSD1306 driver is based on it.

photo Fat 8x14 characters

Connect VCC to VDD on the display, Ground to GND, P1.7 to SDA and P1.6 to SCL. The display works without external pullupps but in case it does'nt work add a 4K7 pullup to SDA and SCL! The software is divided in several cooperating modules shown below.

Files needed for small characters example
Forth commands:
SMALL    ( -- )  Show small 5x8 characterset

Files needed for big fat characters example
Forth commands:
FAT     ( -- )  Show big fat 8x14 character set

photo Thin 7x16 characters






Files needed for big thin characters example
Forth commands:
THIN    ( -- )  Show big thin 7x16 character set

tracer

Files needed for graphic characters example
Forth commands:
GRAPHIC ( -- )  Show graphic and custom character set
SHOW    ( -- )  Animated view of the hedgehog


Animated hedgehog demo

80-89 MSP430FR5xxx specific

egel

80
MSP430FR5xxx hardware multiply

Hardware multiply versions of the basic noForth multiply routines
Hardware multiply versions of the noForth basic multiply routines. Forth commands:
*    ( x y -- x*y )
M*   ( x1 x2 -- dx3 )
UM*  ( u1 u2 -- du3 )
DU*S ( du1 u2 -- du3 )
photo
Load the assembler before the e80 noForth programs, the software RS232 routines are in assembler code.

90-99 MSP430F149 specific

egel

90
MSP430F149 hardware multiply

Hardware multiply versions of the basic noForth multiply routines
Forth commands:
*    ( x y -- x*y )
M*   ( x1 x2 -- dx3 )
UM*  ( u1 u2 -- du3 )
DU*S ( du1 u2 -- du3 )
photo
Load the assembler before the e90 noForth programs, the software RS232 routines are in assembler code.

100-109 Simple applications

egel

100
Wireless analog datalogger

Gathering data from a distance
photo photo photo
scheme
Connect all materials used in the first four chapters to the shield, see schematics.
The first 4 chapters taken as a whole describe in fact a wireless analog datalogger. HC06 datasheet. An LDR is used as an analog sensor.

The sensor is sampled 10 times every second, but only every tenth value is stored. After 20 samples are gathered, they are sent back to the receiving device.

The forth command DATALOGGER starts automatically after a reset.
To quit the program press 'reset' while S2 is held down.

101
Walking biped

Simple walking robot
photo
scheme
The small walking robot has two legs, using four servos. It is wireless controlled thru bluetooth. The code comes from the first eight chapters. The result is a cute walking robot.
Load the assembler before the e101 noForth programs.

The e101a version has very simple code, in just a few lines the robot really does something.


Version e101b implements a more sophisticated version. This version has smoother motion control, more movements. Finally a key interpreter was added for easy remote control using bluetooth.

drawings
The servos are connected to P1.4 to P1.7, the TX/RX of the HC06 are wired to P1.1 and P1.2, see also chapter E04!
photo Program e101s implements a servo tester. With this tester, it is easy to determine the pulse range of a servo. The main word is TESTER.
A potmeter on P1.7 changes the servo position. When S2 is pressed the current position of the servo is printed in microseconds. The pulse range is .6 ms to 2.7 ms.

The Forth commands are:

BIPED-ON ( -- )     - Activate Biped
BIPED-OFF ( -- )    - Deactivate Biped
WOBBLE ( u -- )     - Wobble u times
WALK ( u -- )       - Walk forward u steps
HELLO ( -- )        - Wave hello

scheme Basic Biped Forth commands, all stack neutral:
REST  RIGHT-UP  LEFT-UP  RIGHT-FORW
LEFT-FORW  DOWN  WAVE  TOES  REST 
These words can be used to build more complex functions. The code is kept very simple, so there is plenty room for improvement!

Simple Biped walking and saying hello


The second program, e101b, has one main words named DEMO. This starts the remote control DEMO and boots automatically after a reset.

102
WS2812 with RC-5 remote control

With tailored user interface
photo signals
Load the assembler before the e102 noForth programs.

RC-5 Remote
photo The program has three parts:
a. 16MHz version of the RC-5 decoder
b. WS2812 driver using USCI B0
c. User interface build around the SBCRP520 (photo)

The WS2812 code is for a 16MHz clock, the output bit is P1.7. The RC-5 input bit P2.0 is also adjusted for 16MHz DCO. The bit pulses for Ws2812(B) come from USCI B0.
This application is able to control 2 to 999 WS2812 leds. The configuration can be done via remote control.

The main program WS2812 starts up after a reset. You may save (in RAM) three individual color/brightness settings. The brightness of every setting can be changed in real time, without the loss off the selected color mix.
Take care: 30 leds take about 9 Watt, that is almost 2 Ampere!

signals Forth command: WS2812 ( -- )

110-119 More complex applications

egel

110
Autonomous walking biped

Using ultrasonic distance measuring from chapter 13
photo
scheme
The small walking robot has two legs, using four servos. It is wireless controlled thru Bluetooth. The code comes from the first thirteen chapters. The result is a cute autonomous walking robot. The used sensor is a HC-SR04 ultrasonic distance meter.
scheme When using a 7,4 Volt 900mAh Li-Po accu, a cheap LM2596 buck power supply and build a Li-Po undervoltage protection as shown in the schematic on the left. The biped has plenty of power to play a very long time.
drawings
The servos are connected to P1.4 to P1.7, the TX/RX of the HC06 are wired to P1.1 and P1.2 see also chapter E04!
photo The forth commands are:
BIPED-ON ( -- )  Activate Biped
BIPED-OFF ( -- )    Deactivate Biped
WOBBLE ( u -- )     Wobble u times
WALK ( u -- )       Walk forward u steps
LEFT ( u -- )       Turn left u steps
RIGHT ( u -- )      Turn right u steps
HELLO ( -- )        Wave hello
DEMO ( -- )         Remote control demo
photo Some basic Biped (stack neutral) forth commands:

REST  RIGHT-UP  LEFT-UP
RIGHT-FORW  LEFT-FORW
DOWN  WAVE  TOES  REST
These words may be used to build more complex functions.
photo photo The main noForth word DEMO is a key interpreter, that does handle one-touch commands, by means of a remote control. It will start automatically after a reset.

111
Cloning noForth

Using built-in BSL to copy noForth
photo
More info about the built-in BSL in SLAU319L.PDF
Read the pages 5 to 12, it contains the core of the BSL.

scheme
Place an 20-pin ziff-socket on the Egel kit as shown on the photo on top. Add a green and red led connect them using the schematic. Then connect the six wires for the programmer to the ziff socket. Finally connect a 32kHz crystal to pin 18 & 19 of the ziff socket. Information about the built-in capacitators see SLAU144J.PDF page 274 and beyond. The programmer now is ready to clone itself to an other MSP430G2553.

photo
Load the assembler before the e111 noForth program, there are several routines in assembler code.


signals The APP-vector is filled with the DCO restoration program. The CLONE-program ends with a reset of the "clone". When the DCO constants where erased, they are restored immediatly by executing the APP-vector.

What does the BSL-programmer do:
  1. Enter the BSL programmer on the target MPU
  2. Send password. When a password error occurs the target device is completely erased, including the DCO-constants!
  3. Erase info-B and main Flash memory
  4. Program info-B and main memory
  5. Give target a reset pulse The target MPU then starts the noForth clone. This will restore the DCO-constants. But only when they were erased using an incorrect password.
    After restoring calibration values you may use the chip like a new one. With or without the xtal.
signals

Forth commands:
EMPTY-PASS  ( -- )    Set password to all $FF
MODIFY-PASS ( a -- )  Set password, 'a' is reset vector
CLONE       ( -- )    Copy yourself to another MSP430G2553

112
Six legged walking robot

Controlling 20 RC-servo's using one MSP430F149
schematic
Load the noforth assembler before the e112 noForth programs.

Hexapod version 2
First load the assembler, then e112b to add extra RS232 routines. After that connect an USB cable, then download the rest of the software at 38k4. After this is done type: BLUETOOTH and FREEZE now the robot starts up, using the Bluetooth connection at 9600 baud.
Files needed for the basic hexapod
Forth commands:
READY    ( -- )   Startup the hexapod carefully
WALK     ( u -- ) Walk forward u steps
BACKW    ( u -- ) Walk backward u steps
REST2    ( u -- ) Go to resting position 2

File needed to test servo ranges using a MSP-EXP430G
Forth commands:
TESTER    ( -- )  A servo tester using a potmeter at P1.7

Files needed for some extra hexapod functonality
Forth commands:
LSTEPS    ( u -- )  Crawl forward
HSTEPS    ( u -- )  Walk on hi heels
ANT       ( -- )    Simulation of an ANT walking

A lecture about walking robots (Dutch)


Light images from walking robots lecture as PDF

*