Micro Vibe

My Micro Hobby Projects

  • Increase font size
  • Default font size
  • Decrease font size
Home MV4th Forth Documents New Timer COG Example

New Timer COG Example

This article contains an example of starting a propeller chip COG using MV4th16. The source files for this example are included with the third release of MV4th16 demo version 0.92 and above. The example uses a COG as a real time clock or timer. The timer COG will update the time every 100ms. The code for the COG is developed in the Parallax Spin IDE then saved to the eeprom.The forth code demonstrates how to start the COG and communicate with it. In this example we will run a 24 hour timer in a separate COG from the one running MV4th16. We will use MV4th16 to allocate a COG, load the COG program, and start the COG running. MV4th will communicate with the time COG using three 32 bit memory locations. The first memory location contains the current time. The time is stored as a packed BCD value with each 4 bit nibble holding one digit.  The format of the time value is HHMMSSddwhere; HH is hours, MM is minutes, SS is seconds and dd is hundredths of a second. The second long 32 bit value contains the index of the propeller chip lock to use when updating the time value to prevent both COGs from updating the time stored in memory at the same time.  The third long 32 bit value hold the value for the number of system clock tick per 100ms and can be used to calibrate the timer if needed.

1) Create the COG program.

The first step is to create the COG assembly language program to be loaded into our COG. For this example I used the Parallax Spin IDE to create the COG program. The COG program is created the same as any normal COG assembly object. The one difference is we will not be using any Spin language code at the start of the object. Since the Spin IDE requires at least one spin method before the DAT section containing the COG assembly language code we will include a stub function with no actual spin code. The following code snippet shows the start of the real time clock spin object source code.

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

PUB Start
''does nothing but must be there for spin IDE to compile

  org 0

Notice in the code snippet that the "Start" public method has no code to execute it is only there to satisfy the Parallax IDE.

For this example the source for the COG object can be found in the "Forth\Timer COG\timer.spin" file included with the MV4th demo version 0.91 and higher source files. To compile the COG object into a binary file load the "timer.spin" file in the parallax IDE and select the "Run/Compile Current/View Info" menu selection or press the F8 function key. If your object compiles correctly the IDE will display the Object Info Dialog shown below. If the hex display is not showing in the Object Info Dialog, press the "Show Hex" button.

Spin IDE save as binary file

Press the "Save Binary" button and save the file to your computer. Make sure the saved file has the ".binary" file extension. We will need this file later so remember where you put it.

2) Save the COG program to the EEPROM.

In order for MV4th to start the COG, MV4th will require access to a copy of the binary image to load into the COG. The only persistent storage MV4th has access to is the EEPROM based block device. Each MV4th block device block is 1k bytes in size. For this example we will store the binary image into device block index 31. Our binary COG test program uses less then 1k of space so it will fit in a single block. If your object binary is larger then 1k bytes (256 longs) it will require 2 forth device blocks.

The ForthTerm program available in the MV4th16 dowloads will be used to upload the binary image to the forth block device. See the article Getting Started With MV4th16 for more information on using ForthTerm.

With ForthTerm connected to the MV4th system reset the propeller board and wait for the MV4th prompt to appear. In the ForthTerm program select the "Upload Block File" command under the Communications main menu. In the displayed file select dialog select the "Binary File" option in the file extension selection,then slect the "timer.binary" file we created in the first step. ForthTerm will now display a dialog that the Forth block load words are not loaded and offer to upload the code to the MV4th system, select "OK" to continue.


Once the code has been transfered to the MV4th system the ForthTerm program will display the "Transfer Blocks" dialog. Set the "First Block" setting to 31 then press the "Upload" button.

Upload COG binary

If no error is reported by the ForthTerm program you will have placed a copy of the COG binary image into block 31 of the MV4th block device.

3) Starting the COGin MV4th16

The Forth source code for this exampleis provided in the text file "Forth\Timer COG\timer.forth". This source code has also been saved to the forth block file "Forth\Timer COG\timer.forth.blk" using the ForthTerm program. The timer Forth source code is comprised of two main parts; the first part of the code is used to set and read the real time clock, and the second part of the code is used to initialize and start the COG running the timer.

The ForthTerm program can be used to upload the block file "Forth\Timer COG\timer.forth.blk" file in the same maner we loaded the COG binary image file above. When loading this example save the block file to block index 30 on the MV4th system. After the block file has been uploaded you can run the forth real time clock COG. To run the real time clock COG type "30 LOAD" without the quotes and press enter. A prompt should indicate the COG index of the timer COG.

To set the time for the clock, type the following at the MV4th prompt; "$11223344. set-time" without the quotes and press enter. This will set the time to 11:22:33.44 (of course you would enter the correct time in 24 hour format). Do not forget the period at the end of the constant to indicate a double precision value.

To set the time to 0 used the forth word clr-timer.

To display the current time type "timer." without the quotes and press enter.

The first line of source code in "init-timer" word copies the COG binary image from the stored block and start the COG using the COG-LOAD forth word.
Last Updated on Friday, 01 June 2012 14:47