NiliBasic PIC24 interpreter
---------------------------
NiliBasic is a very simple BASIC interpreter for the 16bit PIC 24FJGA002
microcontroller from Microchip. The interpreter runs on the chip only, no
compiler or tokenizer is needed. For communication with the PC and for text
input and output, a serial RS232 connection is needed in form of a
USB-to-serial converter cable.
Features
--------
There are two versions: with integer arithmetic (16 bit signed) or with
single precision floating point arithmetics. The floating-point version
occupies about 30KBytes of program memory.
This README file contains descriptions of NiliBasic PIC24 which may not be
valid for the current version.
Compiling the sources
---------------------
This software has been developed with Linux. To compile the C source of the
interpreter, download the C30 compiler from Microchip
http://ww1.microchip.com/downloads/mplab/X/index.html
(if the link address changes, search for MPLAB X Linux).
You compile then nilibasicpic24.c to nilibasicpic24.hex. The hex file is to
be burnet into the microcontroller.
./c nilibasicpic24
If you want to compile the interpreter as a PC program, then include the file
utils-pc.c instead of utils.c and compile from the command line under Linux:
cc nilibasicpic24.c -o nilibasicpic24 -lm
cc is the GNU C compiler
-lm means: link the mathematical functions (such as sin, cos).
-o indicates the output filename
Then invoke the simulator by ./nilibasicpic24
The FLASH is simulated, but most of the hardware functions are not. The very
first version of the interpreter has been written in Pascal (nilibasicpic24.pas)
which can be compiled by Free Pascal 2.4.4 and probably by older versions from
2.0.0 on. This program has been converted initially to C by p2c with further
modifications. Then, only the C version has been further developed.
Burning a hex file into the microcontroller
-------------------------------------------
You need a PICKit2 and a circuit described below (see "Schematic"). Under
windows, you can use MPLAB and under Linux, you use pk2cmd (which requires
one other file named "PK2DeviceFile.dat"). To simplify the command line
options, use the script p:
./p nilibasicpic24
Instructions
------------
All instructions must be written in UPPER CASE. Lines may be 40 chars long.
In direct mode, there are the following instructions (enter only the first
letter):
NEW clears the program in Flash, user can enter new lines until blank
LIST lists the actual program
RUN runs the actual program from Flash
HELP displays all available commands.
The program must be entered with line numbers. Editing is not supported
because program lines are directly stored in Flash and the microcontroller
can only erase blocks of 3x512 bytes.
Syntax
------
There are 26 numerical variables, A..Z. In Expressions, there must be no
blanks.
A = B*C
Between the other parts of statements, there must be one blank (in this
example indicated by _):
FOR_X_=_1_TO_A+B*C-D
BASIC commands
--------------
LET Assignments are possible without LET
FOR A = X TO Y Optional STEP. FOR loops are execute always at least once.
NEXT A The variable name A may not be omitted in the expression.
GOTO X A computed GOTO is possible. GOTO X+10*Y
GOSUB X 10 subroutine levels. GOSUB X+Y*Z
RETURN
INPUT X Only one numerical variable, a prompt text may be given
INPUT "X=" X
PRINT Without arguments writes a new line. Expressions or a string
constant "..." may be used. No CRLF is written if terminated
by a semicolon ; Multiple arguments are not supported.
IF expression
THEN expression
The first expression is tested for 0 = false, the second
expression indicates a line number as destination. in IF
statements also .AND. .OR. .XOR. are possible
REM Comment line
END Stop execution
REPEAT Begin of loop
UNTIL A = B end of loop
Expression precedence
---------------------
Bracketed expressions
number (only decimal) or variable
Unary -
* (multiplication) / (division) % (modulo)
+ -
= <> > < >= <=
Comparisons evaluate to 0 for false and everything else for true.
.OR.
.AND.
.XOR. binary or, and, xor. for not use function not().
Microcontroller commands
------------------------
WRITE address value Poke value to address in data memory (RAM).
X = READ(address)
Peek data at address in data memory (RAM), store to
variable. Example RAM addresses in PIC24FJ64002, use
only decimal form in BASIC programs: TRISB() this
function returns the decimal value 712, the address
of the TRISB register. Other functions are (brackets
are necessary): PORTB() = 714, LATB() = 716.
Example: WRITE PORTB() 0 (writes zero to PORTB)
WRITELOW address value Poke 16-bit value to flash memory.
WRITEHIGH address value Poke to flash program memory, only one byte to the
upper location.
X = READLOW(address) Peek 16-bit value from flash memory into variable.
X = READHIGH(address) Peek upper 8-bit value into variable.
X = ANALOG(channel) Start one analog-digital conversion on indicated
channel, store result into variable.
STARTPWM pin Start PWM production at indicated pin.
PWM duty Set duty cycle 0..1023. To stop PWM, use -1 as
parameter.
X = RANDOM(limit) Put random number from 0..limit-1 into variable,
integer result.
Integer functions
-----------------
SQR(X) square = X * X
ABS(X) if X<0 then -X else X
NOT(X) 0=false, everything else = true; binary not.
Floating point functions
------------------------
SIN(X)
COS(X)
ATAN(X)
TAN(X)
EXP(X)
LN(X)
SQRT(X)
TRUNC(X)
ROUND(X)
PI() is implemented as a function,
returns 3.14159265358979323846
Arrays
------
LET @(10) = 20 One single numerical array @ is supported.
PRINT @(A)+10 Indexes must be in brackets ()
Strings
-------
One char array is supported, it is called $(), indexes must be in brackets.
$(0) = 65 set position 0 to 'A'
B = $(20) assign the ASCII value of position 20 to variable B
INPUT$ "Name: " 0 Input chars to $(0), end with 0
PRINT$ 10 Print chars from $(10) until 0 is reached
Bit functions
-------------
They have been introduced in 2011, but worked only on PORTA and PORTB. The new functions require as
a first parameter a RAM address (e.g. PORTB() = 714)
SETBIT PORTB() 7 set bit7 in PORT B
CLEARBIT PORTB() 1 clear bit1 in PORT B
TOGGLEBIT PORTB() 2 toggle bit2 in PORT B
A = TESTBIT(PORTB(),2) store result in variable A, 0 if bit2 of PORT B is zero, else -1
TESTBIT PORTB() 2 A other syntax
Schematic
---------
For a minimal example schematic look at nilipascal-chip.pdf
The procedures PRINT and INPUT use pin RB9 = RxD and RB8 = TxD. Hardware
inverters are not necessary because the serial signals are produced and
analyzed by software including software inverters.
The microcontroller runs at 32 MHz with internal clock generation, no crystal
is required.
All of the needed parts for the NiliPascal or NiliBasic demoboard are available at
www.reichelt.de (prices in Euro from their catalogue 6/2012):
No. Best. Nr. Euro Description
1 USB2 SERIELL 5.95 serial to usb converter with cable
1 24FJ64GA002-ISP 2.90 16 bit PIC microcontroller 64KB Flash, 8KB RAM SDIL-28
1 LED 5MM 5V GN 0.18 LED 5mm with (!) resistor for 5V, green
1 LED 5MM 5V GE 0.20 yellow LED
1 LED 5MM 5V RT 0.17 red LED
1 STECKBOARD 1K2V 2.55 breadboard 640+200 contacts, 66x174mm
1 D-SUB BU 09GW 0.73 D-SUB connector female, 9 pin for breadboard
2 MKS-2 100N 0.08 100nF condensator 40 volts
2 RAD 10/100 0.04 10µF electrolyte condensator 100 volts
2 1N 4148 0.02 universal diode
1 1/4W 10K 0.10 resistor 10k ohm
1 PT 10-S 10K 0.23 trimmer poti 10k ohm, 10mm
1 TASTER 3301 0.10 switch, 6x6mm, 4.3mm height
optional e.g.
1 SL 1X36W 2,54 0.27 36pin connector with angle for PICkit-2 programmer (ICSP, cut to 6 pins)
1 NTC-0.2 10K 0.36 NTC, resistor used for temperature measuring
1 LM2937IMP 3.3 Volt regulator (1 N4148, 100nF, 10µF)
There are discount prices if you buy 10 or 100 pieces (e.g. resistors 10pc=0.33EUR).At the time of writing (April 2010), there is no Windows 7 driver available
for the USB2-Seriell converter from Logilink. As an alternative, the Delock
61640 converter may be used (which is slightly more expensive). Under Linux,
both converters work without installing drivers.
NiliTerminal
------------
Use CTRL-D to download file. NiliTerminal tries to open /dev/ttyUSB0 by
default. Serial connections are 9600 Baud, 8n1.
Copyright
---------
NiliBasic for PIC24 copyright (c) 2010-2012 Dr. med. O'Niel Som
In den Seiten 30/1, 70825 Korntal-Münchingen, Germany
www.nili.de
www.nili.com
This is open source software. NiliBasic PIC24 may be used and copied only
under the terms of the GNU General Public License (GPL), which can be found
in the file GPL within this directory.
I am not a lawyer, and I cannot apply a lot of tests to this software. So
please consider the following statements.
You may use NiliBasic PIC24 only at your own risk without any guarantee.
This software is at an experimental state, so it may be not useful for your
purposes, and it may contain software bugs including the manual, the terminal
program and the interpreter.
It is prohibited to use NiliBasic PIC24 in commercial products or in
applications which can damage the life of persons, as for example in
airplanes, medical systems, weapons, nuclear reactors.
This software does not include free support in any form. Please inform the
author if you find bugs or want to improve the software.
The run-time-module has been compiled with the free version of Microchip's
C-Compiler for PIC24 available from www.microchip.com on the command line on
a Linux system with wine (Windows emulator).
Nili is a trade mark of Dr. O'Niel Som. PIC is a trademark of Microchip.
Linux is a trade mark of Linux Torvalds, Windows is a trademark of Microsoft.
All other mentioned trade marks belong to their owners and should be treated
as such.