0% found this document useful (0 votes)
51 views

ff5 Sheet

This document provides a quick reference for the FlashForth 5 interpreter for PIC and AVR microcontrollers. It summarizes the interpreter, data stack, notation, and keywords for displaying and manipulating data, numbers, addresses, and the stack. Arithmetic operations are listed for single-cell, double-cell, triple-cell, and quad-cell numbers.

Uploaded by

pawaller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

ff5 Sheet

This document provides a quick reference for the FlashForth 5 interpreter for PIC and AVR microcontrollers. It summarizes the interpreter, data stack, notation, and keywords for displaying and manipulating data, numbers, addresses, and the stack. Arithmetic operations are listed for single-cell, double-cell, triple-cell, and quad-cell numbers.

Uploaded by

pawaller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FlashForth 5 Quick Reference Displaying data */ Scale.

( n1 n2 n3 -- n1*n2/n3 )
Uses 32-bit intermediate result.
for PIC and AVR Microcontrollers .
u.
Display a number. ( n -- )
Display u unsigned. ( u -- )
*/mod Scale with remainder. ( n1 n2 n3 -- n.rem n.quot )
Uses 32-bit intermediate result.
u.r Display u with field width n, 0 < n < 256. ( u n -- ) u*/mod Unsigned Scale u1*u2/u3 ( u1 u2 u3 -- u.rem u.quot )
d. Display double number. ( d -- ) Uses 32-bit intermediate result.
Interpreter ud. Display unsigned double number. ( ud -- ) abs Absolute value. ( n -- u )
The outer interpreter looks for words and numbers delimited by .s Display stack content (nondestructively). negate Negate n. ( n -- -n )
whitespace. Everything is interpreted as a word or a number. .st Emit status string for base, current data section, ?negate Negate n1 if n2 is negative. ( n1 n2 -- n3 )
Numbers are pushed onto the stack. Words are looked up and acted and display the stack contents. ( -- ) min Leave minimum. ( n1 n2 -- n )
upon. Names of words are limited to 15 characters. Some words are ? Display content at address. ( addr -- ) PIC24-30-33 max Leave maximum. ( n1 n2 -- n )
compile-time use only and cannot be used interpretively. These are dump Display memory from address, for u bytes. ( addr u -- ) umin Unsigned minimum. ( u1 u2 -- u )
coloured blue. umax Unsigned maximum. ( u1 u2 -- u )
Stack manipulation
dup Duplicate top item. ( x -- x x )
Data and the stack ?dup Duplicate top item if nonzero. ( x -- 0 | x x )
swap Swap top two items. ( x1 x2 -- x2 x1 ) Arithmetic with double-cell numbers
The data stack (S:) is directly accessible and has 32 16-bit cells for
over Copy second item to top. ( x1 x2 -- x1 x2 x1 )
holding numerical values. Functions get their arguments from the
drop Discard top item. ( x -- )
stack and leave their results there as well. There is also a return Some of these words require core.txt, math.txt and qmath.txt.
nip Remove x1 from the stack. ( x1 x2 -- x2 )
address stack (R:) that can be used for temporary storage. d+ Add double numbers. ( d1 d2 -- d1+d2 )
rot Rotate top three items. ( x1 x2 x3 -- x2 x3 x1 )
tuck Insert x2 below x1 in the stack. ( x1 x2 -- x2 x1 x2 ) d- Subtract double numbers. ( d1 d2 -- d1-d2 )
Notation pick Duplicate the u-th item on top. m+ Add single cell to double number. ( d1 n -- d2 )
( xu ... x0 u -- xu ... x0 xu ) m* Signed 16*16 to 32-bit multiply. ( n n -- d )
n, n1, n2, n3 Single-cell integers (16-bit). d2* Multiply by 2. ( d -- d )
2dup Duplicate top double-cell item. ( d -- d d )
u, u1, u2 Unsigned integers (16-bit). d2/ Divide by 2. ( d -- d )
2swap Swap top two double-cell items. ( d1 d2 -- d2 d1 )
x, x1, x2, x3 Single-cell item (16-bit). um* Unsigned 16x16 to 32 bit multiply. ( u1 u2 -- ud )
2over Copy second double item to top. ( d1 d2 -- d1 d2 d1 )
c Character value (8-bit). ud* Unsigned 32x16 to 32-bit multiply. ( ud u -- ud )
2drop Discard top double-cell item. ( d -- )
d ud Double-cell signed and unsigned (32-bit). um/mod Unsigned division. ( ud u1 -- u.rem u.quot )
t ut Triple-cell signed and unsigned (48-bit). >r Send to return stack. S:( n -- ) R:( -- n )
r> Take from return stack. S:( -- n ) R:( n -- ) 32-bit/16-bit to 16-bit
q uq Quad-cell signed and unsigned (64-bit). ud/mod Unsigned division. ( ud u1 -- u.rem ud.quot )
f Boolean flag: 0 is false, -1 is true. r@ Copy top item of return stack. S:( -- n ) R:( n -- n )
rdrop Discard top item of return stack. S:( -- ) R:( n -- ) 32-bit/16-bit to 32-bit
flt flt1 flt3 Floating-point value (32-bit). fm/mod Floored division. ( d n -- n.rem n.quot )
PIC24-30-33 only, with build option. sp@ Leave data stack pointer. ( -- addr )
sm/rem Symmetric division. ( d n -- n.rem n.quot )
addr, addr1, addr2 16-bit addresses. sp! Set the data stack pointer to address. ( addr -- )
32-bit/16-bit to 16-bit.
a-addr cell-aligned address. m*/ Scale with triple intermediate result. d2 = d1*n1/n2
c-addr character or byte address. Operators ( d1 n1 n2 -- d2 )
Arithmetic with single-cell numbers um*/ Scale with triple intermediate result. ud2 = ud1*u1/u2
Numbers and values ( ud1 u1 u2 -- ud2)
Some of these words require core.txt and math.txt. dabs Absolute value. ( d -- ud )
2 Leave integer two onto the stack. ( -- 2 ) + Add. ( n1 n2 -- n1+n2 ) sum dnegate Negate double number. ( d -- -d )
#255 Leave decimal 255 onto the stack. ( -- 255 ) - Subtract. ( n1 n2 -- n1-n2 ) difference ?dnegate Negate d if n is negative. ( d n -- -d )
%11 Leave integer three onto the stack. ( -- 3 ) * Multiply. ( n1 n2 -- n1*n2 ) product
$10 Leave integer sixteen onto the stack. ( -- 16 ) / Divide. ( n1 n2 -- n1/n2 ) quotient
23. Leave double number on the stack. ( -- 23 0 ) mod Divide. ( n1 n2 -- n.rem ) remainder Arithmetic with triple- and quad-numbers
decimal Set number format to base 10. ( -- ) /mod Divide. ( n1 n2 -- n.rem n.quot )
hex Set number format to hexadecimal. ( -- ) u/ Unsigned 16/16 to 16-bit division. ( u1 u2 -- u2/u1 )
bin Set number format to binary. ( -- ) u/mod Unsigned division. ( u1 u2 -- u.rem u.quot ) For PIC18, these words require core.txt, math.txt and qmath.txt.
s>d Sign extend single to double number. ( n -- d ) 16-bit/16-bit to 16-bit q+ Add a quad to a quad. ( q1 q2 -- q3 )
Since double numbers have the most significant bits 1 Leave one. ( -- 1 ) For PIC24-30-33.
in the cell above the least significant bits, you can 1+ Add one. ( n -- n1 ) qm+ Add double to a quad. ( q1 d -- q2 )
just drop the top cell to recover the single number, 1- Subtract one. ( n -- n1 ) For PIC18 and PIC24-30-33.
provided that the value is not too large to fit in a 2+ Add two. ( n -- n1 ) uq* Unsigned 32x32 to 64-bit multiply. ( ud ud -- uq )
single cell. 2- Subtract 2 from n. ( n -- n1 ) For PIC18 and PIC24-30-33.
d>q Extend double to quad-cell number. ( d -- q ) 2* Multiply by 2; Shift left by one bit. ( u -- u1 ) ut* Unsigned 32x16 to 48-bit multiply. ( ud u -- ut )
Requires qmath.h to be loaded. PIC18, PIC24-30-33. 2/ Divide by 2; Shift right by one bit. ( u -- u1 ) ut/ Divide triple by single. ( ut u -- ud )
uq/mod Divide quad by double. ( uq ud -- ud-rem ud-quot )
Relational AVR8 Memory map The following come from bit.txt
= Leave true if x1 x2 are equal. ( x1 x2 -- f ) bit1: name Define a word to set a bit. ( addr bit -- )
<> Leave true if x1 x2 are not equal. ( x1 x2 -- f ) All operations are restricted to 64kB byte address space that is bit0: name Define a word to clear a bit. ( addr bit -- )
< Leave true if n1 less than n2. ( n1 n2 -- f ) divided into: bit?: name Define a word to test a bit. ( addr bit -- )
$0000 – (RAMSIZE-1) SRAM
> Leave true if n1 greater than n2. ( n1 n2 -- f ) When executed, name leaves a flag. ( -- f )
RAMSIZE – (RAMSIZE+EEPROMSIZE-1) EEPROM
0= Leave true if n is zero. ( n -- f )
($ffff-FLASHSIZE+1) – $ffff Flash
Inverts logical value.
The SRAM space includes the IO-space and special function
0< Leave true if n is negative. ( n -- f )
registers. The high memory mark for the Flash context is set by the
within Leave true if xl <= x < xh. ( x xl xh -- f )
u< Leave true if u1 < u2. ( u1 u2 -- f )
combined size of the boot area and FF kernel. The Dictionary
u> Leave true if u1 > u2. ( u1 u2 -- f )
d= Leave true if d1 d2 are equal. ( d1 d2 -- f ) Dictionary management
Memory Context
d0= Leave true if d is zero. ( d -- f )
d0< Leave true if d is negative. ( d -- f ) ram Set address context to SRAM. ( -- ) marker -my-mark Mark the dictionary and memory
d< Leave true if d1 < d2. ( d1 d2 -- f ) eeprom Set address context to EEPROM. ( -- ) allocation state with -my-mark.
d> Leave true if d1 > d2. ( d1 d2 -- f ) flash Set address context to Flash. ( -- ) -my-mark Return to the dictionary and allotted-memory
fl- Disable writes to Flash, EEPROM. ( -- ) state that existed before -my-mark was created.
Bitwise find name Find name in dictionary. ( -- n )
invert Ones complement. ( x -- x ) fl+ Enable writes to Flash, EEPROM, default. ( -- )
iflush Flush the flash write buffer. ( -- ) Leave 1 immediate, -1 normal, 0 not found.
dinvert Invert double number. ( du -- du ) forget name Forget dictionary entries back to name.
and Bitwise and. ( x1 x2 -- x ) here Leave the current data section dictionary
empty Reset all dictionary and allotted-memory
or Bitwise or. ( x1 x2 -- x ) pointer. ( -- addr )
pointers. ( -- )
xor Bitwise exclusive-or. ( x -- x ) align Align the current data section dictionary
words List words in dictionary. ( -- )
lshift Left shift by u bits. ( x1 u -- x2 ) pointer to cell boundary. ( -- )
rshift Right shift by u bits. ( x1 u -- x2 ) hi Leave the high limit of the current
data space. ( -- u )
Defining constants and variables
Memory
Typically, the microcontroller has three distinct memory contexts: Accessing Memory constant name Define new constant. ( n -- )
Flash, EEPROM and SRAM. FlashForth unifies these memory 2constant name Define double constant. ( x x -- )
! Store x to address. ( x a-addr -- )
spaces into a single 64kB address space. name Leave value on stack. ( -- n )
@ Fetch from address. ( a-addr -- x )
@+ Fetch cell and increment address by cell size. variable varname Define a variable in the current data
PIC18 Memory map ( a-addr1 -- a-addr2 x ) section. ( -- )
The address ranges are: 2! Store 2 cells to address. ( x1 x2 a-addr -- ) Use ram, eeprom or flash to set data section.
$0000 – $ebff Flash 2@ Fetch 2 cells from address. ( a-addr -- x1 x2 ) 2variable name Define double variable. ( -- )
$ec00 – $efff EEPROM c! Store character to address. ( c addr -- ) varname Leave address on stack. ( -- addr )
$f000 – $ff5f SRAM, general use c@ Fetch character from address. ( addr -- c ) value valname Define value. ( n -- )
$ff60 – $ffff SRAM, special function registers c@+ Fetch char, increment address. to valname Assign new value to valname. ( n -- )
The high memory mark for each context will depend on the ( addr1 -- addr2 c ) valname Leave value on stack. ( -- n )
particular device used. Using a PIC18F26K22 and the default values +! Add n to cell at address. ( n addr -- ) user name Define a user variable at offset +n. ( +n -- )
in p18f-main.cfg for the UART version of FF, a total of 423 bytes -@ Fetch from addr and decrement addr by 2.
is dedicated to the FF system. The rest (3473 bytes) is free for ( addr1 -- addr2 x )
application use. Also, the full 64kB of Flash memory is truncated to cf! Store to Flash memory. ( dataL dataH addr -- )
fit within the range specified above. PIC24-30-33 only. Examples
cf@ Fetch from Flash memory. ( addr -- dataL dataH )
PIC24 Memory map PIC24-30-33 only. ram Set SRAM context for variables and
>a Write to the A register. ( x -- ) values. Be careful not to accidentally
A device with EEPROM will have its 64kB address space divided
a> Read from the A register. ( -- x ) define variables in EEPROM or Flash
into:
$0000 – $07ff SRAM, special function registers memory. That memory wears quickly
$0800 – ($0800+RAMSIZE-1) SRAM, general use with multiple writes.
($0800+RAMSIZE) – $fbff Flash Accessing bits in RAM $ff81 constant portb Define constant in Flash.
$fc00 – $ffff EEPROM 3 value xx Define value in SRAM.
The high memory mark for the Flash context will depend on the mset Set bits in file register with mask c. ( c addr -- ) variable yy Define variable in SRAM.
device. Also, the full Flash memory of the device may not be For PIC24-30-33, the mask is 16 bits. 6 yy ! Store 6 in variable yy.
accessible. mclr Clear bits in file register with mask c. ( c addr -- ) eeprom 5 value zz ram Define value in EEPROM.
mtst AND file register byte with mask c. ( c addr -- x ) xx yy zz portb yy @ Leaves 3 f172 5 ff81 6
warm Warm restart clears SRAM data. : mk-byte-array Defining word ( n -- ) Predefined variables
xx yy zz portb yy @ Leaves 0 f172 5 ff81 0 create allot ...to make byte array objects
base Variable containing number base. ( -- a-addr )
4 to xx Sets new value. does> + ; ...as shown in FF user’s guide.
irq Interrupt vector (SRAM variable). ( -- a-addr )
xx yy zz portb yy @ Leaves 4 f172 5 ff81 0 10 mk-byte-array my-bytes Creates an array object
Always disable user interrupts and clear
hi here - u. Prints the number of bytes free. my-bytes ( n -- addr ).
related interrupt enable bits before zeroing
$ff8a constant latb PortB latch for the PIC18. 18 0 my-bytes c! Sets an element
interrupt vector.
$ff93 constant trisb PortB direction-control register. 21 1 my-bytes c! ...and another.
di false to irq ei
%00000010 trisb mclr Sets RB1 as output. 255 2 my-bytes c!
turnkey Vector for user start-up word. ( -- a-addr )
latb 1 bit1: pb1-high Defines a word to set RB1 high. 2 my-bytes c@ Should leave 255.
EEPROM value mirrored in SRAM.
pb1-high Sets RB1 high. : mk-cell-array Defining word ( n -- )
prompt Deferred execution vector for the info displayed
create cells allot ...to make cell array objects.
by quit. Default value is .st ( -- a-addr )
does> swap cells + ;
’emit EMIT vector. Default is tx1 ( -- a-addr )
5 mk-cell-array my-cells Creates an array object
’key KEY vector. Default is rx1 ( -- a-addr )
my-cells ( n -- addr ).
’key? KEY? vector. Default is rx1? ( -- a-addr )
Defining compound data objects 3000 0 my-cells ! Sets an element
’source Current input source. ( -- a-addr )
45000 1 my-cells ! ...and another.
s0 Variable for start of data stack. ( -- a-addr )
63000 2 my-cells !
create name Create a word definition and store r0 Bottom of return stack. ( -- a-addr )
1 my-cells @ . Should print 45000
the current data section pointer. rcnt Number of saved return stack cells. ( -- a-addr )
does> Define the runtime action of a created word. tib Address of the terminal input buffer. ( -- a-addr )
allot Advance the current data section dictionary tiu Terminal input buffer pointer. ( -- a-addr )
pointer by u bytes. ( u -- ) >in Variable containing the offset, in characters,
, Append x to the current data section. ( x -- ) Memory operations from the start of tib to the current
c, Append c to the current data section. ( c -- ) parse area. ( -- a-addr )
," xxx" Append a string at HERE. ( -- ) pad Address of the temporary area for strings. ( -- addr )
i, Append x to the flash data section. ( x -- ) Some of these words come from core.txt. : pad tib ti# + ;
ic, Append c to the flash data section. ( c -- ) cmove Move u bytes from address-1 to address-2. Each task has its own pad but has zero default size.
( addr1 addr2 u -- ) If needed the user must allocate it separately
cf, Compile xt into the flash dictionary. ( addr -- )
Copy proceeds from low addr to high address. with allot for each task.
c>n Convert code field addr to name field addr.
wmove Move u cells from address-1 to address-2. dp Leave the address of the current data section
( addr1 -- addr2 )
( addr1 addr2 u -- ) PIC24-30-33 only dictionary pointer. ( -- addr )
n>c Convert name field addr to code field addr.
fill Fill u bytes with c starting at address. EEPROM variable mirrored in RAM.
( addr1 -- addr2 )
( addr u c -- ) dps End address of dictionary pointers. ( -- d )
n>l Convert nfa to lfa. ( nfa -- lfa )
erase Fill u bytes with 0 starting at address. Absolute address of start of free Flash.
Not implemented; use 2- instead.
( addr u -- ) Library and C code can be linked,
>body Leave the parameter field address of the created blanks Fill u bytes with spaces starting at address.
word. ( xt -- a-addr ) starting at this address. PIC24, dsPIC33
( addr u -- ) hp Hold pointer for formatted numeric output.
:noname Define headerless forth code. ( -- addr )
cells Convert cells to address units. ( u -- u ) ( -- a-addr )
>xa Convert a Flash virtual address to a real executable chars Convert chars to address units. ( u -- u )
address. PIC24-30-33, ATmega ( a-addr1 -- a-addr2 ) up Variable holding a user pointer. ( -- addr )
char+ Add one to address. ( addr1 -- addr2 ) latest Variable holding the address of the latest
xa> Convert a real executable address to a Flash virtual cell+ Add size of cell to address. ( addr1 -- addr2 )
address. PIC24-30-33, ATmega ( a-addr1 -- a-addr2 ) defined word. ( -- a-addr )
aligned Align address to a cell boundary. ( addr -- a-addr ) float? Interpreter defer for parsing floating-point values.
’ >float is float?
PIC24-30-33 only
Array examples

ram Example Predefined constants


create my-array 20 allot ...of creating an array, Floating-point for PIC24-30-33
my-array 20 $ff fill ...filling it with 1s, and cell Size of one cell in characters. ( -- n ) These words require that FlashForth has been built with the
my-array 20 dump ...displaying its content. true Boolean true value. ( -- -1 ) .eq FLOATS, 1 option in the relevant processor config file.
create my-cell-array false Boolean false value. ( -- 0 ) >float Convert a string into a float. ( c-addr u -- flt f )
100 , 340 , 5 , Initialised cell array. bl ASCII space. ( -- c ) Note that it works for decimal base only.
my-cell-array 2 cells + @ Should leave 5. ( -- x ) Fcy Leave the cpu instruction-cycle frequency in kHz. ( -- u ) Examples: 1e10 -1e10 1.234e10 -1.234e10
create my-byte-array ti# Size of the terminal input buffer. ( -- u ) f. Print in decimal format. ( flt -- )
18 c, 21 c, 255 c, Initialised byte array. fe. Print in engineering format. ( flt -- )
my-byte-array 2 chars + c@ Should leave 255. ( -- c ) fs. Print in scientific format. ( flt -- )
fdrop Discard top float item. ( flt -- ) fasin Arcine of flt, radians. ( flt -- asin(flt) ) Flow control
fdup Duplicate top float item. ( flt -- flt flt ) facos Arccosine of flt, radians. ( flt -- acos(flt) )
fover Copy second float item to top. fatan Arctangent of flt, radians. ( flt -- atan(flt) ) Structured flow control
( flt1 flt2 -- flt1 flt2 flt1 ) fatan2 Arctangent of flt1/flt2, radians. if xxx else yyy then Conditional execution. ( f -- )
fswap Swap top two float items. ( flt1 flt2 -- flt2 flt1 ) ( flt1 flt2 -- atan(flt1/flt2) ) begin xxx again Infinite loop. ( -- )
frot Rotate top three float items. fsqrt Square-root. ( flt -- sqrt(flt) ) begin xxx cond until Loop until cond is true. ( -- )
( flt1 flt2 flt3 -- flt2 flt3 flt1 ) fexp Exponential. ( flt -- exp(flt) ) begin xxx cond while Loop while cond is true. ( -- )
fnip Remove second top float. ( flt1 flt2 -- flt2 ) flog Natural logarithm. ( flt -- loge(flt) ) yyy repeat yyy is not executed on the last iteration.
ftuck Insert flt2 below flt1. flog10 Logarithm, base 10. ( flt -- log10(flt) ) for xxx next Loop u times. ( u -- )
( flt1 flt2 -- flt2 flt1 flt2 ) fcosh Hyperbolic cosine. ( flt -- cosh(flt) ) r@ gets the loop counter u-1 ... 0
nfswap Swap float and single. ( flt n -- n flt ) fsinh Hyperbolic sine. ( flt -- sinh(flt) ) endit Sets loop counter to zero so that we leave
fnswap Swap float and single. ( n flt -- flt n ) ftanh Hyperbolic tangent. ( flt -- tanh(flt) ) a for loop when next is encountered.
nfover Copy float item over single. ( flt n -- flt n flt ) ( -- )
fnover Copy single over float item. ( n flt -- n flt n) From doloop.txt, we get the ANSI loop constructs which iterate
f@ Fetch float item to stack. ( addr -- flt ) from initial up to, but not including, limit:
f! Store float item to address. ( flt addr -- ) The Compiler limit initial do words-to-repeat loop
fconstant name Define constant. ( flt -- ) Defining functions limit initial do words-to-repeat value +loop
fvariable name Define variable. ( -- ) i Leave the current loop index. ( -- n )
fliteral Compile in literal value. ( flt -- ) : Begin colon definition. ( -- ) Innermost loop, for nested loops.
f0 Leave value 0.0 on stack. ( -- flt ) ; End colon definition. ( -- ) j Leave the next-outer loop index. ( -- n )
f1 Leave value 1.0 on stack. ( -- flt ) [ Enter interpreter state. ( -- ) leave Leave the do loop immediately. ( -- )
f10 Leave value 10.0 on stack. ( -- flt ) ] Enter compilation state. ( -- ) ?do Starts a do loop which is not run if
f0.5 Leave value 0.5 on stack. ( -- flt ) state Compilation state. ( -- f ) the arguments are equal. ( limit initial -- )
s>f Convert single to float. ( n -- flt ) State can only be changed with [ and ].
d>f Convert double to float. ( d -- flt ) [i Enter Forth interrupt context. ( -- ) Loop examples
f>s Convert float to single. ( flt -- n ) PIC18, PIC24-30-33 decimal
f>d Convert float to double. ( flt -- d ) i] Enter compilation state. ( -- ) : sumdo 0 100 0 do i + loop ; sumdo leaves 4950
f0= Leave true if flt equal to zero. ( flt -- f ) PIC18, PIC24-30-33 : sumfor 0 100 for r@ + next ; sumfor leaves 4950
f0< Leave true if flt less than zero. ( flt -- f ) ;i End an interrupt word. ( -- ) : print-twos 10 0 do i u. 2 +loop ;
f= Leave true if floats are equal. ( flt1 flt2 -- f ) literal Compile value on stack at compile time. ( x -- )
f< Leave true if flt1 less than flt2. ( flt1 flt2 -- f ) At run time, leave value on stack. ( -- x ) Case example
f<= eave true if flt1 less than or equal to flt2. 2literal Compile double value on stack at compile time. From case.txt, we get words case, of, endof, default and endcase
( flt1 flt2 -- f ) ( x x -- ) to define case constructs.
f> Leave true if flt1 greater than flt2. At run time, leave value on stack. ( -- x x ) : testcase
( flt1 flt2 -- f ) inline name Inline the following word. ( -- ) 4 for r@
f>= Leave true if flt1 greater than or equal to flt2. inlined Mark the last compiled word as inlined. ( -- ) case
( flt1 flt2 -- f ) immediate Mark latest definition as immediate. ( -- ) 0 of ." zero " endof
fnegate Negate float value. ( flt -- -flt ) immed? Leave a nonzero value if addr contains 1 of ." one " endof
fabs Leave absolute value. ( flt1 -- flt2 ) an immediate flag. ( addr -- f ) 2 of ." two " endof
fround Round to nearest integral value. ( flt1 -- flt2 ) in? Leave a nonzero flag if nfa has inline bit default ." default " endof
fmin Leave minimum. ( flt1 flt2 -- flt ) set. ( nfa -- f ) endcase
fmax Leave maximum. ( flt1 flt2 -- flt ) postpone name Postpone action of immediate word. ( -- ) next
f2* Multiple by 2. ( flt -- flt*2) see name Show definition. Load see.txt. ;
f2/ Divide by 2. ( flt -- flt/2 )
Comments
The following functions call out to the Microchip math library. Unstructured flow control
f+ Addition ( flt1 flt2 -- flt1+flt2 ) ( comment text) Inline comment.
exit Exit from a word. ( -- )
f- Subtraction ( flt1 flt2 -- flt1-flt2 ) \ comment text Skip rest of line.
If exiting from within a for loop,
f* Multiplication ( flt1 flt2 -- flt1*flt2 ) we must drop the loop count with rdrop.
f/ Division ( flt1 flt2 -- flt1/flt2 ) Examples of colon definitions
abort Reset stack pointer and execute quit. ( -- )
fpow Power. ( flt1 flt2 -- flt1**flt2) : square ( n -- n**2 ) Example with stack comment. ?abort If flag is false, print message
fsin Sine of flt in radians. ( flt -- sin(flt) ) dup * ; ...body of definition. and abort. ( f addr u -- )
fcos Cosine of flt in radians. ( flt -- cos(flt) ) : poke0 ( -- ) Example of using PIC18 assembler. ?abort? If flag is false, output ? and abort. ( f -- )
ftan Tangent of flt in radians. ( flt -- tan(flt) ) [ $f8a 0 a, bsf, ] ; abort" xxx" if flag, type out last word executed,
followed by text xxx. ( f -- )
quit Interpret from keyboard. ( -- )
warm Make a warm start. Interrupt example place Place string from a1 to a2
Reset reason will be displayed on restart. as a counted string. ( addr1 u addr2 -- )
S: Reset instruction ram variable icnt1 ...from FF source. n= Compare strings in RAM(addr) and Flash(nfa).
E: External reset pin : irq_forth It’s a Forth colon definition Leave true if strings match, n < 16.
W: Watchdog reset [i ...in the Forth interrupt context. ( addr nfa u -- f )
U: Return stack underflow icnt1 @ 1+ scan Scan string until c is found.
O: Return stack overflow icnt1 ! c-addr must point to RAM and u < 255.
B: Brown out reset ]i ( c-addr u c -- caddr1 u1 )
P: Power on reset ;i skip Skip chars until c not found.
M: Math error ’ irq_forth 0 int! Set the user interrupt vector. c-addr must point to RAM and u < 255.
A: Address error ( c-addr u c -- caddr1 u1 )
Note that irq vector is cleared. : init Alternatively, compile a word
/string Trim string. ( addr u n -- addr+n u-n )
[’] irq_forth 0 int! ...so that we can install the
>number Convert string to a number.
; ...interrupt service function
( 0 0 addr1 u1 -- ud.l ud.h addr2 u2 )
’ init is turnkey ...at every warm start.
number? Convert string to a number and flag.
Vectored execution (Function pointers) ( addr1 -- addr2 0 | n 1 | d.l d.h 2 )
The P register Prefix: # decimal, $ hexadecimal, % binary.
sign? Get optional minus sign.
’ name Search for name and leave its The P register can be used as a variable or as a pointer. It can be
( addr1 n1 -- addr2 n2 flag )
execution token (address). ( -- addr ) used in conjunction with for..next or at any other time.
type Type line to terminal, u < #256. ( addr u -- )
[’] name Search for name and compile !p Store address to P(ointer) register. ( addr -- )
accept Get line from the terminal. ( c-addr +n1 -- +n2 )
it’s execution token. ( -- ) @p Fetch the P register to the stack. ( -- addr )
At most n1 characters are accepted, until the line
execute Execute word at address. ( addr -- ) !p>r Push contents of P to return stack and
is terminated with a carriage return.
The actual stack effect will depend on store new address to P. ( addr -- ) ( R: -- addr )
source Leave address of input buffer and number of
the word executed. r>p Pop from return stack to P register. ( R: addr -- )
characters. ( -- c-addr u )
@ex Fetch vector from addr and execute. p+ Increment P register by one. ( -- )
evaluate Interpret a string in SRAM. ( addr n -- )
( addr -- ) p2+ Add 2 to P register. ( -- )
interpret Interpret the buffer. ( c-addr u -- )
defer vec-name Define a deferred execution vector. ( -- ) p++ Add n to the p register. ( n -- )
parse Parse a word in TIB. ( c -- addr length )
is vec-name Store execution token in vec-name. p! Store x to the location pointed to
word Parse a word in TIB and write length into TIB.
( addr -- ) by the p register. ( x -- )
Leave the address of length byte on the stack.
vec-name Execute the word whose execution token pc! Store c to the location pointed to
( c -- c-addr )
is stored in vec-name’s data space. by the p register. ( c -- )
int! Store interrupt vector to table. p@ Fetch the cell pointed to
( xt vector-no -- ) by the p register. ( -- x ) Pictured numeric output
PIC18: vector-no is dummy vector number (0) pc@ Fetch the char pointed to
by the p register. ( -- c ) Formatted string representing an unigned double-precision integer is
for high priority interrupts.
In a definition, !p>r and r>p should always be used to allow proper constructed in the end of tib.
PIC30: Alternate interrupt vector table in Flash. <# Begin conversion to formatted string. ( -- )
PIC33: Alternate interrupt vector table in RAM. nesting of words.
# Convert 1 digit to formatted string. ( ud1 -- ud2 )
PIC24H: Alternate interrupt vector table in RAM. #s Convert remaining digits. ( ud1 -- ud2 )
PIC24F: Alternate interrupt vector table in RAM. Note that ud2 will be zero.
PIC24FK: Alternate interrupt vector table in Flash. Characters
digit? Convert char to a digit according to base. hold Append char to formatted string. ( c -- )
PIC24E: Main interrupt vector table in RAM. sign Add minus sign to formatted string, if n<0. ( n -- )
ATmega: Interrupt vector table in RAM. ( c -- n f )
>digit Convert n to ascii character value. ( n -- c ) #> End conversion, leave address and count
int/ Restore the original vector to the interrupt vector of formatted string. ( ud1 -- c-addr u )
table in flash. PIC30 PIC24FK ( vector-no -- ) >pr Convert a character to an ASCII value. ( c -- c )
Nongraphic characters converted to a dot. For example, the following:
ivt Activate the normal interrupt vector table. ( -- ) -1 34. <# # # #s rot sign #> type
Not PIC24E, dsPIC33E. char char Parse a character and leave ASCII value. ( -- n )
For example: char A ( -- 65 ) results in -034 ok
aivt Activate the alternate interrupt vector table. ( -- )
[char] char Compile inline ASCII character. ( -- )

Strings Interaction with the operator


Autostart example
Some of these words come from core.txt.
s" text" Compile string into flash. ( -- ) Interaction with the user is via a serial communications port,
’ my-app is turnkey Autostart my-app. At run time, leaves address and length. typically UART1. Settings are 38400 baud, 8N1, using Xon/Xoff
false is turnkey Disable turnkey application. ( -- addr u ) handshaking. Which particular serial port is selected is determined
." text" Compile string to print into flash. ( -- ) by the vectors ’emit, ’key and ’key?.
emit Emit c to the serial port FIFO. ( c -- ) Other Hardware Assembler words for PIC18
FIFO is 46 chars. Executes pause. cwd Clear the WatchDog counter. ( -- ) In the stack-effect notaion for the PIC18 family, f is a file register
space Emit one space character. ( -- ) PIC18, PIC24-30-33 address, d is the result destination, a is the access bank modifier,
spaces Emit n space characters. ( n -- ) ei Enable interrupts. ( -- ) and k is a literal value.
cr Emit carriage-return, line-feed. ( -- ) di Disable interrupts. ( -- )
key Get a character from the serial port FIFO. Pause for +n milliseconds. ( +n -- )
Execute pause until a character is available. ( -- c )
ms Conditions for structured flow control
ticks System ticks, 0–ffff milliseconds. ( -- u ) cc, test carry ( -- cc )
key? Leave true if character is waiting
nc, test not carry ( -- cc )
in the serial port FIFO. ( -- f )
mi, test negative ( -- cc )
Multitasking pl, test not negative ( -- cc )
Load the words for multitasking from task.txt. z, test zero ( -- cc )
Serial communication ports task: Define a new task in flash memory space nz, test not zero ( -- cc )
tx0 Send a character via UART0 on ATmega. ( c -- )
( tibsize stacksize rstacksize addsize -- ) ov, test overflow ( -- cc )
Use ram xxx allot to leave space for the PAD nov, test not overflow ( -- cc )
rx0 Receive a character from UART0 on ATmega. ( -- c )
of the prevously defined task. not, invert condition ( cc -- not-cc )
rx0? Leave true if the UART0 receive buffer
The OPERATOR task does not use PAD.
is not empty. ATmega ( -- f )
u0- Disable flow control for UART1 interface. ( -- )
tinit Initialise a user area and link it Destination and access modifiers
to the task loop. ( taskloop-addr task-addr -- ) w, Destination WREG ( -- 0 )
u0+ Enable flow control for UART1 interface, default. ( -- )
Note that this may only be executed from f, Destination file ( -- 1 )
tx1 Send character to UART1. ( c -- ) a, Access bank ( -- 0 )
the operator task.
Buffered via an interrupt driven queue. b, Use bank-select register ( -- 1 )
task Leave the address of the task definition table. ( -- addr )
rx1 Receive a character from UART1. ( -- c )
run Makes a task run by inserting it after operator
Buffered by an interrupt-driven queue. Byte-oriented file register operations
in the round-robin linked list. ( task-addr -- )
rx1? Leave true if the UART1 receive buffer
May only be executed from the operator task. addwf, Add WREG and f. ( f d a -- )
is not empty. ( -- f )
end Remove a task from the task list. ( task-addr -- ) addwfc, Add WREG and carry bit to f. ( f d a -- )
u1- Disable flow control for UART1 interface. ( -- )
May only be executed from the operator task. andwf, AND WREG with f. ( f d a -- )
u1+ Enable flow control for UART1 interface, default. ( -- )
single End all tasks except the operator task. ( -- ) clrf, Clear f. ( f a -- )
tx2 Send character to UART2. ( c -- ) comf, Complement f. ( f d a -- )
Removes all tasks from the task list.
PIC24-30-33
May only be executed from the operator task. cpfseq, Compare f with WREG, skip if equal. ( f a -- )
rx2 Receive a character from UART2. ( -- c )
tasks List all running tasks. ( -- ) cpfsgt, Compare f with WREG, skip if greater than. ( f a -- )
PIC24-30-33
pause Switch to the next task in the round robin task list. cpfslt, Compare f with WREG, skip if less than. ( f a -- )
rx2? Leave true if the UART1 receive buffer
Idle in the operator task if allowed by all tasks. ( -- ) decf, Decrement f. ( f d a -- )
is not empty. PIC24-30-33 ( -- f )
his Access user variables of other task. decfsz, Decrement f, skip if zero. ( f d a -- )
u2- Disable flow control for UART2 interface. ( -- )
( task.addr vvar.addr -- addr ) dcfsnz, Decrement f, skip if not zero. ( f d a -- )
u2+ Enable flow control for UART2 interface, default. ( -- )
load Leave the CPU load on the stack. ( -- n ) incf, Increment f. ( f d a -- )
txu Send a character via the USB UART. ( c -- )
Load is percentage of time that the CPU is busy. incfsz, Increment f, skip if zero. ( f d a -- )
PIC18-USB
Updated every 256 milliseconds. infsnz, Increment f, skip if not zero. ( f d a -- )
rxu Receive a character from the USB UART. ( -- c )
load+ Enable the load LED on AVR8. ( -- ) iorwf, Inclusive OR WREG with f. ( f d a -- )
PIC18-USB
load- Disable the load LED on AVR8. ( -- ) movf, Move f. ( f d a -- )
rxu? Leave true if the USB UART receive buffer
busy CPU idle mode not allowed. ( -- ) movff, Move fs to fd. ( fs fd -- )
is not empty. PIC18-USB ( -- f )
idle CPU idle is allowed. ( -- ) movwf, Move WREG to f. ( f a -- )
operator Leave the address of the operator task. ( -- addr ) mulwf, Multiply WREG with f. ( f a -- )
Character queues on PIC24-30-33 ulink Link to next task. ( -- addr ) negf, Negate f. ( f a -- )
rlcf, Rotate left f, through carry. ( f d a -- )
cq: name Create character queue. ( u -- ) rlncf, Rotate left f, no carry. ( f d a -- )
cq0 Initialize or reset queue. ( queue-addr -- ) Structured Assembler rrcf, Rotate right f, through carry. ( f d a -- )
>cq? Is there space available in queue. ( queue-addr -- f ) To use many of the words listed in the following sections, load the rrncf, Rotate right f, no carry. ( f d a -- )
>cq Put character into queue. ( c queue-addr -- ) text file asm.txt. The assembler for each processor family provides setf, Set f. ( f d a -- )
cq>? Number of characters in queue. ( queue-addr -- u ) the same set of structured flow control words, however, the subfwb, Subtract f from WREG, with borrow. ( f d a -- )
cq> Get character from queue. ( queue-addr -- c ) conditionals that go with these words are somewhat subwf, Subtract WREG from f. ( f d a -- )
u1rxq Leave UART1 RX queue address. ( -- queue-addr ) processor-specific. subwfb, Subtract WREG from f, with borrow. ( f d a -- )
u1txq Leave UART1 TX queue address. ( -- queue-addr ) if, xxx else, yyy then, Conditional execution. ( cc -- ) swapf, Swap nibbles in f. ( f d a -- )
u2rxq Leave UART2 RX queue address. ( -- queue-addr ) begin, xxx again, Loop indefinitely. ( -- ) tstfsz, Test f, skip if zero. ( f a -- )
u2txq Leave UART2 TX queue address. ( -- queue-addr ) begin, xxx cc until, Loop until condion is true. ( -- ) xorwf, Exclusive OR WREG with f. ( f d a -- )
Bit-oriented file register operations Conditions for structured flow control R0 ( -- 0 ) R16 ( -- 16 )
R1 ( -- 1 ) R17 ( -- 17 )
bcf, Bit clear f. ( f b a -- ) z, test zero ( -- cc )
R2 ( -- 2 ) R18 ( -- 18 )
bsf, Bit set f. ( f b a -- ) nz, test not zero ( -- cc )
R3 ( -- 3 ) R19 ( -- 19 )
btfsc, Bit test f, skip if clear. ( f b a -- ) not, invert condition ( cc -- not-cc )
R4 ( -- 4 ) R20 ( -- 20 )
btfss, Bit test f, skip if set. ( f b a -- ) R5 ( -- 5 ) R21 ( -- 21 )
btg, Bit toggle f. ( f b a -- ) Low-level flow control operations R6 ( -- 6 ) R22 ( -- 22 )
bra, Branch unconditionally. ( rel-addr -- ) R7 ( -- 7 ) R23 ( -- 23 )
Literal operations rcall, Call subroutine. ( rel-addr -- ) R8 ( -- 8 ) R24 ( -- 24 )
return, Return from subroutine. ( -- ) R9 ( -- 9 ) R25 ( -- 25 )
addlw, Add literal and WREG. ( k -- )
retfie, Return from interrupt enable. ( -- ) R10 ( -- 10 ) R26 ( -- 26 )
andlw, AND literal with WREG. ( k -- )
R11 ( -- 11 ) R27 ( -- 27 )
daw, Decimal adjust packed BCD digits in WREG. ( -- )
R12 ( -- 12 ) R28 ( -- 28 )
iorlw, Inclusive OR literal with WREG. ( k -- ) Bit-oriented operations R13 ( -- 13 ) R29 ( -- 29 )
lfsr, Move literal to FSRx. ( k f -- )
bclr, Bit clear. ( bit ram-addr -- ) R14 ( -- 14 ) R30 ( -- 30 )
movlb, Move literal to BSR. ( k -- ) R15 ( -- 15 ) R31 ( -- 31 )
movlw, Move literal to WREG. ( k -- ) bset, Bit set. ( bit ram-addr -- )
mullw, Multiply literal with WREG. ( k -- ) btst, Bit test to z. ( bit ram-addr -- )
sublw, Subtract WREG from literal. ( k -- ) btsc, Bit test, skip if clear. ( bit ram-addr -- )
xorlw, Exclusive OR literal with WREG. ( k -- ) btss, Bit test, skip if set. ( bit ram-addr -- )

Data memory – program memory operations Assembler words for AVR8 Arithmetic and logic instructions
tblrd*, Table read. ( -- ) For the ATmega instructions, Rd denotes the destination (and
tblrd*+, Table read with post-increment. ( -- ) source) register, Rr denotes the source register, Rw denotes a
tblrd*-, Table read with post-decrement. ( -- ) register-pair code, K denotes constant data, k is a constant address, b add, Add without carry. ( Rd Rr -- )
tblrd+*, Table read with pre-increment. ( -- ) is a bit in the register, x,Y,Z are indirect address registers, A is an adc, Add with carry. ( Rd Rr -- )
tblwt*, Table write. ( -- ) I/O location address, and q is a displacement (6-bit) for direct adiw, Add immediate to word. ( Rw K -- )
tblwt*+, Table write with post-increment. ( -- ) addressing. Rw = {XH:XL,YH:YL,ZH:ZL}
tblwt*-, Table write with post-decrement. ( -- ) sub, Subtract without carry. ( Rd Rr -- )
tblwt+*, Table write with pre-increment. ( -- ) subi, Subtract immediate. ( Rd K -- )
Conditions for structured flow control
sbc, Subtract with carry. ( Rd Rr -- )
cs, carry set ( -- cc ) sbci, Subtract immediate with carry. ( Rd K -- )
Low-level flow control operations eq, zero ( -- cc ) sbiw, Subtract immediate from word. ( Rw K -- )
bra, Branch unconditionally. ( rel-addr -- ) hs, half carry set ( -- cc ) Rw = {XH:XL,YH:YL,ZH:ZL}
call, Call subroutine. ( addr -- ) ie, interrupt enabled ( -- cc )
and, Logical AND. ( Rd Rr -- )
goto, Go to address. ( addr -- ) lo, lower ( -- cc )
andi, Logical AND with immediate. ( Rd K -- )
pop, Pop (discard) top of return stack. ( -- ) lt, less than ( -- cc )
or, Logical OR. ( Rd Rr -- )
push, Push address of next instruction to mi, negative ( -- cc )
ori, Logical OR with immediate. ( Rd K -- )
top of return stack. ( -- ) ts, T flag set ( -- cc )
eor, Exclusive OR. ( Rd Rr -- )
rcall, Relative call. ( rel-addr -- ) vs, no overflow ( -- cc )
com, One’s complement. ( Rd -- )
retfie, Return from interrupt enable. ( -- ) not, invert condition ( cc -- not-cc )
neg, Two’s complement. ( Rd -- )
retlw, Return with literal in WREG. ( k -- ) sbr, Set bit(s) in register. ( Rd K -- )
return, Return from subroutine. ( -- ) Register constants cbr, Clear bit(s) in register. ( Rd K -- )
Z ( -- 0 ) inc, Increment. ( Rd -- )
Other MCU control operations Z+ ( -- 1 ) dec, Decrement. ( Rd -- )
clrwdt, Clear watchdog timer. ( -- ) -Z ( -- 2 ) tst, Test for zero or minus. ( Rd -- )
nop, No operation. ( -- ) Y ( -- 8 ) clr, Clear register. ( Rd -- )
reset, Software device reset. ( -- ) Y+ ( -- 9 ) ser, Set register. ( Rd -- )
sleep, Go into standby mode. ( -- ) -Y ( -- 10 ) mul, Multiply unsigned. ( Rd Rr -- )
X ( -- 12 ) muls, Multiply signed. ( Rd Rr -- )
X+ ( -- 13 ) mulsu Multiply signed with unsigned. ( Rd Rr -- )
Assembler words for PIC24-30-33 -X ( -- 14 ) fmul, Fractional multiply unsigned. ( Rd Rr -- )
As stated in the wordsAll.txt, there is only a partial set of words XH:XL ( -- 01 ) fmuls, Fractional multiply signed. ( Rd Rr -- )
for these families of microcontrollers. YH:YL ( -- 02 ) fmulsu, Fractional multiply signed with unsigned. ( Rd Rr -- )
ZH:ZL ( -- 03 )
Branch instructions sec, Set carry. ( -- ) Alternate set I2 C words for PIC18
clc, Clear carry. ( -- )
rjmp, Relative jump. ( k -- ) sen, Set negative flag. ( -- )
ijmp, Indirect jump to (Z). ( -- ) Load these words from i2c_base.txt for a PIC18 microcontroller.
cln, Clear negative flag. ( -- ) They make use of the structured assembler for the PIC18.
eijmp, Extended indirect jump to (Z). ( -- ) sez, Set zero flag. ( -- ) i2cinit Initializes I2 C master mode, 100 kHz clock. ( -- )
jmp, Jump. ( k16 k6 -- ) clz Clear zero flag. ( -- ) i2cws Wake slave. Bit 0 is R/W bit. ( slave-addr -- )
k6 is zero for a 16-bit address. sei, Global interrupt enable. ( -- ) The 7-bit I2 C address is in bits 7-1.
rcall, Relative call subroutine. ( k -- ) cli, Global interrupt disable. ( -- ) i2c! Write one byte to I2 C bus and wait for ACK. ( c -- )
icall, Indirect call to (Z). ( -- ) ses, Set signed test flag. ( -- ) i2c@ak Read one byte and continue. ( -- c )
eicall, Extended indirect call to (Z). ( -- ) cls, Clear signed test flag. ( -- ) i2c@nak Read one last byte from the I2 C bus. ( -- c )
call, Call subroutine. ( k16 k6 -- ) sev, Set two’s complement overflow. ( -- ) i2c-addr1 Write 8-bit address to slave. ( addr slave-addr -- )
k6 is zero for a 16-bit address. clv, Clear two-s complement overflow. ( -- ) i2c-addr2 Write 16-bit address to slave ( addr slave-addr -- )
ret, Subroutine return. ( -- ) set, Set T in SREG. ( -- ) Lower-level words.
reti, Interrupt return. ( -- ) clt, Clear T in SREG. ( -- ) ssen Assert start condition. ( -- )
cpse, Compare, skip if equal. ( Rd Rr -- ) seh, Set half carry flag in SREG. ( -- ) srsen Assert repeated start condition. ( -- )
cp, Compare. ( Rd Rr -- ) clh, Clear half carry flag in SREG. ( -- ) spen Generate a stop condition. ( -- )
cpc, Compare with carry. ( Rd Rr -- ) srcen Set receive enable. ( -- )
MCU control instructions
cpi, Compare with immediate. ( Rd K -- ) break, Break. ( -- ) snoack Send not-acknowledge. ( -- )
sbrc, Skip if bit in register cleared. ( Rr b -- ) nop, No operation. ( -- ) sack Send acknowledge bit. ( -- )
sbrs, Skip if bit in register set. ( Rr b -- ) sleep, Sleep. ( -- ) sspbuf! Write byte to SSPBUF and wait for
sbic, Skip if bit in I/O register cleared. ( A b -- ) wdr, Watchdog reset. ( -- ) transmission. ( c -- )
sbis, Skip if bit in I/O register set. ( A b -- )

Data transfer instructions Synchronous serial communication


mov, Copy register. ( Rd Rr -- ) I2 C communications as master SPI communications as master
movw, Copy register pair. ( Rd Rr -- ) The following words are available as a common set of words for
ldi, Load immediate. ( Rd K -- ) PIC18FXXK22, PIC24FV32KX30X and ATmega328P The following words are available as a common set of words for
lds, Load direct from data space. ( Rd K -- ) microcontrollers. Load them from a file with a name like PIC18FXXK22, PIC24FV32KX30X and ATmega328P
ld, Load indirect. ( Rd Rr -- ) i2c-base-XXXX.txt where XXXX is the specific microcontroller. microcontrollers. Load them from a file with a name like
Rr = {X,X+,-X,Y,Y+,-Y,Z,Z+,-Z} i2c.init Initializes I2 C master mode, 100 kHz clock. spiN-base-XXXX.txt where XXXX is the specific microcontroller and N
ldd, Load indirect with dosplacement. ( Rd Rr q -- ) ( -- ) identifies the particular SPI module. Because SPI devices are so
Rr = {Y,Z} i2c.close Shut down the peripheral module. ( -- ) varied in their specification, you likely have to adjust the register
i2c.ping? Leaves true if the addressed slave device settings in spi.init to suit your particular device.
sts, Store direct to data space. ( k Rr -- )
acknowledges. ( 7-bit-addr -- f ) spi.init Initializes SPI master mode, 1 MHz clock.
st, Store indirect. ( Rr Rd -- )
i2c.addr.write Address slave device for writing. ( -- )
Rd = {X,X+,-X,Y,Y+,-Y,Z,Z+,-Z}
Leave true if the slave acknowledged. spi.close Shut down the peripheral module. ( -- )
std, Store indirect with displacement. ( Rr Rd q -- )
( 7-bit-addr -- f ) spi.wait Poll the SPI peripheral until the operation
Rd={Y,Z}
i2c.c! Send byte and leave ack bit. ( c -- ack ) has finished. ( -- )
in, In from I/O location. ( Rd A -- )
Note that the ack bit will be high spi.cexch Send byte c1, leave incoming byte c2 on stack.
out, Out to I/O location. ( Rr A -- )
if the slave device did not acknowledge. ( c1 -- c2 )
push, Push register on stack. ( Rr -- )
i2c-addr-read Address slave device for reading. spi.csend Send byte c. ( c -- )
pop, Pop register from stack. ( Rd -- )
Leave true if slave acknowledged. spi.select Select the external device. ( -- )
Bit and bit-test instructions ( 7-bit-addr -- f ) spi.deselect Deselect the external device. ( -- )

lsl, Logical shift left. ( Rd -- ) i2c.c@.ack Fetch a byte and ack for another.
lsr, Logical shift right. ( Rd -- ) ( -- c ) This guide assembled by Peter Jacobs, School of Mechanical Engineering,
rol, Rotate left through carry. ( Rd -- ) i2c.c@.nack Fetch one last byte. ( -- c ) The University of Queensland, February-2016 as Report 2016/02.
( -- ) It is a remix of material from the following sources:
ror, Rotate right through carry. ( Rd -- ) FlashForth v5.0 source code and word list by Mikael Nordman
asr, Arithmetic shift right. ( Rd -- ) Low level words.
i2c.idle? Leave true if the I2 C bus is idle. ( -- f ) http://flashforth.sourceforge.net/
swap, Swap nibbles. ( Rd -- ) i2c.start Send start condition. ( -- ) EK Conklin and ED Rather Forth Programmer’s Handbook 3rd Ed.
bset, Flag set. ( s -- ) 2007 FORTH, Inc.
i2c.rsen Send restart condition. ( -- ) L Brodie Starting Forth 2nd Ed., 1987 Prentice-Hall Software Series.
bclr, Flag clear. ( s -- ) i2c.stop Send stop condition. ( -- ) Robert B. Reese Microprocessors from Assembly Language to C Using
sbi, Set bit in I/O register. ( A b -- ) i2c.wait Poll the I2 C hardware until the operation the PIC18Fxx2 Da Vinci Engineering Press, 2005.
cbi, Clear bit in I/O register. ( A b -- ) has finished. ( -- ) Microchip 16-bit MCU and DSC Programmers Reference Manual
bst, Bit store from register to T. ( Rr b -- ) i2c.bus.reset Clock through bits so that slave devices Document DS70157F, 2011.
bld, Bit load from T to register. ( Rd b -- ) Atmel 8-bit AVR Insturction Set Document 08561-AVR-07/10.
are sure to release the bus. ( -- )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy