You are here: Dr. O'Niel Som Publishers :
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.
Open source to download from sourceforge.net
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, you must first install wine (the windows emulator), then the C30
compiler by Microchip.
Then you can use the script (c under linux, c.bat under windows) to compile
nilibasicpic24.c to nilibasicpic24.hex. The hex file can be used to program the
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
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.0 and probably by older versions from
2.0.0 on. This program has been converted to C by p2c with further modifications.
Then, only the C version has been further developed.
-lm means: link the mathematical functions (such as sin, cos).
-o indicates the output filename
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:
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
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.
There are 26 numerical variables, A..Z. In Expressions, there must be no
A = B*C
Between the other parts of statements, there must be one blank (in this
example indicated by _):
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
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.
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
number (only decimal) or variable
* (multiplication) / (division) % (modulo)
= <> > < >= <=
Comparisons evaluate to 0 for false and everything else for true.
.XOR. binary or, and, xor. for not use function not().
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
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
X = RANDOM(limit) Put random number from 0..limit-1 into variable,
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
PI() is implemented as function,
LET @(10) = 20 One single numerical array @ is supported.
PRINT @(A)+10 Indexes must be in brackets ()
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
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
All of the needed parts for the example schematic to be built on a breadboard
are available from Reichelt.de (prices in Euro in Germany from their catalogue
6/2010 + shipping)
No. Best. Nr. Euro Description
1 USB2 SERIELL 4.60 serial/usb converter cable with 25pin adaptor
1 DELOCK 61460 7.95 serial to usb adapter with cable
1 24FJ64GA002-ISP 3.50 16 bit PIC microcontroller 64KB Flash, 8KB RAM SDIL-28
1 LED 5MM 5V GN 0.16 LED 5mm with resistor for 5V, green
1 LED 5MM 5V GE 0.17 yellow
1 LED 5MM 5V RT 0.17 red (3MM alle=0.16)
1 STECKBOARD 1K2V 2.90 breadboard 640+200 contacts, 66x174mm
1 D-SUB BU 09GW 0.50 D-SUB connector female, 9 pin for breadboard
2 MKS-2 100N 0.07 100nF condensator
2 RAD 10/100 0.04 10ÂµF electrolyte condensator
2 1N 4148 0.02 diodes
1 1/4W 10K 0.10 resistor 10k ohm (10=0.33 EUR)
1 PT 10-S 10K 0.20 trimmer poti 10k ohm
1 TASTER 3301 0.11 switch, 6x6mm, 4.3mm height
Shipping 5.60 inside Germany
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.
Use CTRL-D to download file. NiliTerminal tries to open /dev/ttyUSB0 by
default. Serial connections are 9600 Baud, 8n1.
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
Copyright © 2020 Dr. med. O'Niel Som Publisher · Wollreffenweg 64 · D-74613 Öhringen · Germany
Nili is a registered trade mark of Dr. O'Niel Som.
All other mentioned trade marks belong to their respective owners and are to be treated as such.