Version française

PicForth

Table of contents

Forth compiler for PIC 16F87x and 16F88

This Forth compiler, available under the GPL free software license, is hosted on Unix (or Linux) using gforth 0.6.x and generates code for the Microchip PIC 16F87x and 16F88 microcontrollers family. It includes two kinds of cooperative schedulers. You can download PicForth 1.2.5.

In the distribution, you will find some examples: a model railroad booster (see below), a DCC signal generator, some code for silver card smartcards.

Below you can find an example of code in charge of a model railroad booster (electronic design by Alexis Polti) using the DCC system (Digital Command Control).

Resources:

Forth code
\
\ Code for the DCC booster.
\
\ The DCC booster is in charge of driving a H bridge, which amplifies a
\ DCC signal so that it can power a model railroad circuit.
\
\ This device is in charge of checking for high current (short-circuit)
\ and thermal warning problems. Note that short-circuit may happen if the
\ circuit does have a reverse loop. In this case, if a jumper on the
\ board allows it, the signal phase will be reversed and a few µs later
\ we will recheck for the short-circuit. If it has disappeared, the DCC
\ signal will stay reversed until a new short-circuit occurs.
\

pic16f87x

\ Port mapping

5 pin-a jumper               \ Inversion allowed jumper (in)
0 pin-b thermal              \ H bridge thermal sensor (in)
1 pin-b presence             \ Presence detected (out)
2 pin-b inversion            \ Inversion active (out)
3 pin-b disconnection        \ Disconnection (out)
6 pin-b loco-detected        \ Engine detected on track (in)
7 pin-b short-detected       \ Short-circuit detected (in)
1 pin-c brake                \ H bridge BRAKE (out)
2 pin-c pwm                  \ H bridge PWM (out)

\ Enable and disable output (act and signal)

: enable-output ( -- ) brake low pwm high disconnection low ;

: disable-output ( -- ) brake high pwm low disconnection high ;

\ Timer routines. At 4MhZ, with a prescaler of 64, each tick corresponds
\ to 64µs. Call ticks with a "negate"d value. The minimum watchdog time is
\ 7ms; it needs to be cleared within the loop.

:: ticks ( -n -- )
    tmr0 !                                   \ Store -n into tmr0
    t0if bit-clr                             \ Clear overflow bit
    begin clrwdt t0if bit-set? until         \ Wait for overflow to occur
;

: 64µs ( -- ) -1 ticks ;
: 10ms ( -- ) -$9d ticks ;

variable scount
: 1s ( -- ) $64 scount v-for 10ms v-next ;

\ Handle track shortcut

: handle-shortcut ( -- )
    \ Toggle inversion if inversion is allowed (jumper high)
    jumper high? if inversion toggle then
    \ If shortcut is gone after 64µs, exit
    64µs short-detected low? if exit then
    \ We are still in shortcut condition, disable output for 10ms
    disable-output 10ms enable-output
;

\ Maintain output disabled as long as thermal alert is on. Wait for one
\ extra second before reenabling output.

: handle-thermal-alert ( -- )
    disable-output
    begin clrwdt thermal low? until
    1s enable-output
;

\ If there is a loco, repeat the signal

: check-loco ( -- )
    loco-detected high? if presence high then
    loco-detected low? if presence low then
;

\ Initialization

: init ( -- )
    $00 portb !                              \ Clear portb output latches
    $00 portc !                              \ Clear portc output latches
    $06 adcon1 !                             \ Disable A/C converter
    $ff trisa !                              \ All pins of porta as inputs
    $f1 trisb !                              \ Portb 1, 2 and 3 as outputs
    $f9 trisc !                              \ Portc 1 and 2 as outputs
    $05 option_reg !                         \ CLKOUT, prescaler(tmr0)=64
    enable-output
;

\ Main program

: mainloop ( -- )
  begin
    short-detected high? if handle-shortcut then
    thermal high? if handle-thermal-alert then
    check-loco
  again
;

main
: main ( -- ) init mainloop ;

\ Configuration word

fosc-hs set-fosc    \ High-speed oscillator
Assembly code
0x0000  018A    clrf    0x0A
0x0001  2846    goto    0x046   ; (init-picforth)
        ; name: enable-output
        ; max return-stack depth: 0
0x0004  1087    bcf     0x07,1
0x0005  1507    bsf     0x07,2
0x0006  1186    bcf     0x06,3
0x0007  0008    return
        ; name: disable-output
        ; max return-stack depth: 0
0x0008  1487    bsf     0x07,1
0x0009  1107    bcf     0x07,2
0x000A  1586    bsf     0x06,3
0x000B  0008    return
        ; name: ticks
        ; max return-stack depth: 0
0x000C  0081    movwf   0x01
0x000D  110B    bcf     0x0B,2
0x000E  0064    clrwdt
0x000F  1D0B    btfss   0x0B,2
0x0010  280E    goto    0x00E   ; ticks + 0x002
0x0011  0008    return
        ; name: 64µs
        ; max return-stack depth: 0
0x0012  30FF    movlw   0xFF
0x0013  280C    goto    0x00C   ; ticks
        ; name: 10ms
        ; max return-stack depth: 0
0x0014  3063    movlw   0x63
0x0015  280C    goto    0x00C   ; ticks
        ; name: 1s
        ; max return-stack depth: 1
0x0016  3064    movlw   0x64
0x0017  00A2    movwf   0x22
0x0018  2014    call    0x014   ; 10ms
0x0019  0BA2    decfsz  0x22,f
0x001A  2818    goto    0x018   ; 1s + 0x002
0x001B  0008    return
        ; name: handle-shortcut
        ; max return-stack depth: 1
0x001C  1E85    btfss   0x05,5
0x001D  2820    goto    0x020   ; handle-shortcut + 0x004
0x001E  3004    movlw   0x04
0x001F  0686    xorwf   0x06,f
0x0020  2012    call    0x012   ; 64µs
0x0021  1F86    btfss   0x06,7
0x0022  0008    return
0x0023  2008    call    0x008   ; disable-output
0x0024  2014    call    0x014   ; 10ms
0x0025  2804    goto    0x004   ; enable-output
        ; name: handle-thermal-alert
        ; max return-stack depth: 2
0x0026  2008    call    0x008   ; disable-output
0x0027  0064    clrwdt
0x0028  1806    btfsc   0x06,0
0x0029  2827    goto    0x027   ; handle-thermal-alert + 0x001
0x002A  2016    call    0x016   ; 1s (rs depth: 1)
0x002B  2804    goto    0x004   ; enable-output
        ; name: check-loco
        ; max return-stack depth: 0
0x002C  1B06    btfsc   0x06,6
0x002D  1486    bsf     0x06,1
0x002E  1F06    btfss   0x06,6
0x002F  1086    bcf     0x06,1
0x0030  0008    return
        ; name: init
        ; max return-stack depth: 0
0x0031  0186    clrf    0x06
0x0032  0187    clrf    0x07
0x0033  3006    movlw   0x06
0x0034  1683    bsf     0x03,5
0x0035  009F    movwf   0x1F
0x0036  30FF    movlw   0xFF
0x0037  0085    movwf   0x05
0x0038  30F1    movlw   0xF1
0x0039  0086    movwf   0x06
0x003A  30F9    movlw   0xF9
0x003B  0087    movwf   0x07
0x003C  3005    movlw   0x05
0x003D  0081    movwf   0x01
0x003E  1283    bcf     0x03,5
0x003F  2804    goto    0x004   ; enable-output
        ; name: mainloop
        ; max return-stack depth: 3
0x0040  1B86    btfsc   0x06,7
0x0041  201C    call    0x01C   ; handle-shortcut (rs depth: 1)
0x0042  1806    btfsc   0x06,0
0x0043  2026    call    0x026   ; handle-thermal-alert (rs depth: 2)
0x0044  202C    call    0x02C   ; check-loco
0x0045  2840    goto    0x040   ; mainloop (rs depth: 3)
        ; name: (init-picforth)
        ; max return-stack depth: 0
0x0046  3033    movlw   0x33
0x0047  0084    movwf   0x04
        ; name: main
        ; max return-stack depth: 3
0x0048  2031    call    0x031   ; init
0x0049  2840    goto    0x040   ; mainloop (rs depth: 3)

Development version

You can get the current development version using Darcs.

Changes

To understand the scope of these changes, please refer to the documentation.

New in version 1.2.5

New features

New optimization

Bug fixes

New in version 1.2.4

New words

New library

New optimization

Code and documentation enhancements

Bug fixes

New in version 1.2.3

Bug fix

New in version 1.2.2

New words

Enhancements

Bug fixes

New in version 1.2.1

Bug fixes

New in version 1.2

Optimizations

Enhancements

Cleanups

Bug fixes

New in version 1.1

New words

Bug fixes

Miscellenaous

New in version 1.0.1

Distribution glitches in 1.0

  (reported by David McNab <david@rebirthing.co.nz>)
  + Missing tools/bootloading directory
  + Missing dcc2.fs

New files

Changes

New in version 1.0

Port to GForth 0.6.2

Changes since PicForth 0.32

Enhancements

Changes since PicForth 0.31

Enhancements

Bug fixes

Doc fixes

Tools

Changes since PicForth 0.30

Glitch fixes

Changes since PicForth 0.29

Port for PIC16F88 architecture

License clarification

Changes since PicForth 0.28

Bug fix

Changes since PicForth 0.27

New words

Bug fixes

Changes since PicForth 0.26

New library

Incompatible change

Changes since PicForth 0.25

New words

New optimizations

Changes since PicForth 0.24

New words

Changes since PicForth 0.23

New words

New optimizations

Changes since PicForth 0.22

Feature change

New words

New documentation

Changes since PicForth 0.21

New features

Removed words

Changes since PicForth 0.20

New words

Changes since PicForth 0.19

New optimizations

Doc emphasis

Changes since PicForth 0.18

Bug fix

Changes since PicForth 0.17

Data area initialization

Changes since PicForth 0.16

New optimizations

New words

New libraries

New example

Removed words

Changes since PicForth 0.15

New optimizations

New words

Preparation for larger programs

New example

Changes since PicForth 0.14

New words

Behaviour changes

Parameters order changes

Renamed words

Renamed example

Changes since PicForth 0.13

New words

New optimizations

New example

New library

Bug fixes

Changes since PicForth 0.12

Bug fixes

Changes since PicForth 0.11

New multitasker

New optimizations

Changes since PicForth 0.10

New features

New optimizations

Changes since PicForth 0.9

New features

New words

Changes since PicForth 0.8

New words

New modes

New optimizations

Changes since PicForth 0.7

New parameter passing convention

New optimizations

New words

Changes since PicForth 0.6

New optimizations

New check

Bug fixes

Changes since PicForth 0.5

New disassembler and interactive mode

Changes since PicForth 0.4

New optimization

New example

New words

New features

Bug fixes

Changes since PicForth 0.3

New words

Changes since PicForth 0.2

New library files

Add library files libfetch.fs and libstore.fs which were forgotten in previous versions and are used by generator.fs.

Changes since PicForth 0.1

New words

Renamed words