Piccolino Manual Installation of the required software, and connection of the hardware

Piccolino Manual
Installation of the required software,
and connection of the hardware
.
(c)2013 Bert van Dam
Rev. 01
INTRODUCTION
INSTALLATION INSTRUCTIONS
EXAMPLE PROGRAM
PROGRAMMING FAILS
USE THE PICCOLINO WITH SMALL BASIC
USE THE PICCOLINO WITH JAL BOOKS
USE THE PICCOLINO WITH HYPER TERMINAL OR PYSERIAL
DONE! WHAT’S NEXT?
PICCOLINO (JAL) LANGUAGE INSTRUCTIONS
INDEX
3
8
11
24
24
26
28
29
32
53
2
Introduction
Congratulations with the purchase of your new Piccolino. The Piccolino is a prototyping
platform that can be used for rapid prototyping and testing. All basic parts are available
on the Piccolino, and the headers allow you to connect extra components with ease.
Optionally you can connect expansion modules, or shields. You can purchase those, but
you can also make them yourself. If you always use the same components in your
prototypes why not make an expansion module of your own. If you place headers on your
own expansion modules you can stack them on top of each other. Without heat sink the
power stabilizer of the Piccolino can supply about 100 mA through the headers.
The Piccolino has the following properties:
1.
2.
3.
4.
No expansive programmer required: the Piccolino can program itself.
Built in stabilized 5 volt power supply with indicator LED.
Built in RS232 port (USB convertor sold separately).
Equipped with LED, pushbutton switch and variable resistor so you can start
experimenting immediately.
5. Reset pushbutton switch and auto-reset jumper.
6. Room for stackable expansion modules.
7. Modern PIC16f887 microcontroller:
• 20 MHz (5 MIPS).
• 8192 words Flash memory.
• 768 bytes RAM.
• 256 bytes EEPROM.
• 35 I/O, with 25 on the headers.
• 14 analog inputs.
• 2 PWM outputs.
• 2 Comparators.
• Communication: RS232-TTL, RS232, I2C, SPI, one wire.
• Ground, 5 volt and Vin on the header.
The 16f887 is placed in a socket, so you can exchange it for another one. But you can't
take just any PIC. The Piccolino PIC contains a bootloader which enables it to program
itself. So if you want to use another PIC it also has to have this special bootloader
software, otherwise the Piccolino will not work anymore. Replacement PICs can be
purchased at Elektor.
3
Please note: if you remove the PIC from the Piccolino and program it with a regular
programmer the bootloader will be overwritten! That means the PIC can no longer be
used in the Piccolino.
Figure 1. The Piccolino.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Power supply connector.
RS232 connector.
Power LED.
5 volt power regulator.
User button.
User LED.
User variable resistor.
Auto-reset jumper.
Manual reset button.
Headers.
16F887 PIC microcontroller with bootloader.
High speed 20 MHz crystal.
RS232 convertor chip.
The Piccolino needs an external power supply. It doesn't have to be stabilized, but it must
be decent current with the plus on the center pin. The optimum voltage is between 7 and
10 volt. You can use a higher voltage but then the voltage regulator on the Piccolino may
get a bit hot. Optionally you can power the Piccolino with a 9 volt battery. That way you
can use the Piccolino "on the road". The Piccolino side kick package contains an
appropriate plug. You are advised to use a rechargeable battery.
The headers contain the following connections:
1
Name
a0 AN0
a1 AN1
a2 AN2
a3 AN3
a4
a5 AN4
e0 AN5
c
Description
Pin a0, connected to the analog unitAN0.
Pin a1, connected to the analog unitAN1.
Pin a2, connected to the analog unitAN2.
Pin a3, connected to the analog unitAN3.
Pin a4.
Pin a5, connected to the analog unitAN4.
Pin a6, connected to the analog unitAN5.
Pin c0.
Name
b7 TTL out
b6 TTL in
b5 AN13
b4 AN11
b3 AN9
b2 AN6
b1 AN10
b0 AN12
Description
Pin b7, serial RS232-TTL out.
Pin b6, serial RS232-TTL in.
Pin b5, connected to the analog unitAN13.
Pin b4, connected to the analog unitAN11.
Pin b3, connected to the analog unitAN9.
Pin b2, connected to the analog unitAN6.
Pin b1, connected to the analog unitAN10.
Pin b0, connected to the analog unitAN12.
Name
Vin
+5 V
NC
GND
d0
d1
d2
d3
Description
Voltage on the Piccolino powerplug.
Stabilized +5 volt.
Not used.1
Ground.
Pin d0.
Pin d1.
Pin d2.
Pin d3.
The NC pins are not in used, and meant for future Piccolino models.
5
Name
c3 SCL
c5 SDO
c4 SDA
c2 CCP1
c1 CCP2
NC
NC
NC
Description
Pin b7, serial clock line (SPI en I2C)
Pin b6, serial data out (SPI en I2C)
Pin b5, serial data in (SPI en I2C)
Pin b4, CCP1 module.
Pin b3, CCP2 module.
Not used.
Not used.
Not used.
Table 1.Header information.
These descriptions, as well as the markings on the Piccolino itself, are just for reference.
Of course all pins, when connected, have the full functionality as described in the
datasheet. That means you can use the Piccolino with JAL books written by the same
author.2
Figure 2. Function of the pins according to the 16f887 datasheet.
Not all pins are connected to the header, and likewise not all headers are connected to
pins. Some are used for internal purposes, for example for the built-in LED and variable
2
See "Done! What's next" for more information.
resistor. Apart from that future models of the Piccolino have been taken into account.
These models have different functionalities, and may require different pins.
Please note: to allow automatic programming the auto-reset jumper must be placed. There
is no real need to ever remove this jumper, unless you prefer to manually reset the
Piccolino during programming.
Some communication software insists on using flow control, which cannot be disabled,
for example Hyper Terminal. If you want to use this kind of software you must remove
the jumper during communication. The same applies to the Python pySerial module.
For your ease a Piccolino Sidekick expansion pack is available. This extensive parts
package contains a wealth of parts that you need for the book "Super quick PC
Interfacing", and for many other fun and exciting experiments.
Figure 3. The Sidekick kit.
3
The content Piccolino Sidekick:
1
1
20
7
5
2
Breadboard.
Jumper wires for breadboard and Piccolino.
Resistors: 47 - 3x330 - 3x1k - 47 - 3x2k2 - 2k7 - 4x10k - 100k 1M - 2x10M.
Capacitors: 100nF - 220nF - 1µF - 2,2µF - 22µF - 220µF - 1000 µF.
Coils: 1,0mH - 4,7mH - 10mH - 22mH - 47mH.
OPAMPs: LM358 - MAX492.
3
The components and set composition may vary. The information in the Elektor webshop is
correct.
7
2
2
2
1
4
1
3
1
1
1
1
1
1
BJTs: BC547C - BC679A.
MOSFETs: BS170 - IRF740.
LEDs.
IC: NE555.
Variable resistors: 2k2 LIN - 2 x 10k LIN - 100k LIN.
Mini stereo connector, male.
Breadboard switches.
Resistor array 10k.
Diode 1N4007.
Servomotor Hitec HS-422 or similar.
LDR.
TC74 A0 5.0 VAT.
9 volt battery connector with wire.
Installation Instructions
The Piccolino is programmed using JAL (Just Another Language), a Pascal-like high
level language. It is the only high level free language, and has a large and active global
user base. An extensive series of book on JAL is available.
JAL is configurable and expandable using libraries, and can be combined with assembler.
This is an example of a JAL program that makes the LED on the Piccolino flash.
include 16f887_bert_piccolino
forever loop
pLED = on
delay_100ms(1)
pLED = off
delay_1s(1)
end loop
You see that even without any knowledge of JAL or the Piccolino you're still able to
figure out what this program does. The LED is switched on, then there is a short delay
(100 milliseconds), then the LED is switched off, and this time we wait longer: 1 second.4
4
If you don't know much about programming, or if you want to expand your knowledg,e the book
"PIC Microcontroller Programming in 10 captivating lessons" (ISBN 978-1-907920-17-2) comes
highly recommended. It teaches you step by step how to program in JAL. Each lessons comes
complete with questions. The answers are in the back of the book so you can check your work.
We use JALedit to write our JAL programs. This freeware editor has a number of unique
advantages:
•
•
•
•
•
•
•
•
•
•
JAL commands are color coded.
Overview of variables, constants, and procedures used in a program.
Block comment add or remove.
One click compile and download.
Built in ASCII table, calculator, and terminal.
Source export to html (for publication on a website).
Automatic backup.
Compiler output in a separate window.
Load multiple files at the same time.
Jump to offending program line on error.
Before you continue reading please download the Piccolino software package. We will
use the package that belongs to the (Dutch) book "Supersnel PC Interfacen". Don't worry:
everything will be in English! Go to www.boekinfo.tk, and find the link to this book.
Click on the link to go to the support page, en then download the package.
The package contains JALedit, a great editor in which we will be writing our JAL
programs, the JAL compiler, the software that communicates with the bootloader in the
Piccolino and an extensive collection of libraries and functions.5
Unzip the download while maintaining the directory structure; you need about 18 Mb of
space. It doesn't matter where you unzip the download to - we will move it later - but it is
important that the directory structure remains intact.6
All programs and files have been preconfigured; all you need to do is to move the entire
directory to the correct location. Copy the PICcolino directory and everything in it to disk
C. The result should be as shown in the next Figure. For clarity all directories that have
no bearing on the Piccolino are not shown.
5
The download also contains the source codes of the "Supersnel PC Interfacen" book, but those
are of course quite useless without the book. So you can delete this directory (c:\PICcolino\book)
once the installation is completed. Another option would be to buy the book and learn everything
about interfacing between PC and Piccolino.
6
If you don't own an unzip program you can download a free one at this website: http://www.7zip.org/
9
Figure 4. The unzipped download file in the correct location on drive C.
If it doesn't look like the previous Figure you did something wrong. You either unzipped
it wrong (the directory structure was lost) of you moved the directories incorrectly. Try
again until you get it right. If this is done incorrectly then none of the programs will work.
Now go to the jaledit.exe file in the c:\PICcolino\JAL\JALedit directory and make a link
to your desktop. This program is JALedit, the editor in which you will be writing your
programs. You will need it regularly, hence the link.
Figure 5. Desktop link to JALedit.
7
Verify that jaledit.exe is indeed in the directory "c:\PICcolino\JAL\JALedit". You can
check this by looking at the properties of the link you just made.
7
All PC screen-prints were made on a Windows 7 64 bit machine, Dutch version. So on your PC it
may look a bit different, but the content should be the same.
Figure 6. Verify the correct path in the link.
If this check fails then do not continue! Go back and redo the installation. Continuing is
pointless because the programs will not work.
Example program
In order to program the Piccolino we need to know to which serial or COM port it is
connected to. If you use a COM cable you can see at the back of your computer which
COM port it is connected to. If you are using a regular serial port, and there is no marking
on the back of your computer, than simple select port 1. If programming should fail try 2,
3 and 4. Regular COM port numbers are normally four or below.
If you use a serial-to-USB convertor you can find the correct COM port number in the
Windows Device Manager. Open the Device Manager on your PC - with the Piccolino
connected and powered - to see what the number is, and make a note of it.8
If you have more than one virtual port and you're not sure which one the Piccolino is
connected to, make a list of all the ports. Should programming fail, then try the other
8
On Windows XP select: Start - Execute and then enter devmgmt.msc followed by the enter key.
On a Windows 7 machine select Start, and then enter devmgmt.msc in the little search box. In the
list of programs found, click on devmgmt.msc.
11
ports on your list until you find the right one. Another option is to remove the serial-toUSB cable from your PC and see which virtual port disappears from your list.
Figure 7. The virtual COM port that the Piccolino is connected to.
On my PC the virtual port number is 5, but on your PC it is probably a different number.
Whenever this manual refers port 5 simply replace it with the port number that your PC is
using. To help you remind which number that is you can make a note in this box:
Piccolino port number in this manual
5
On your PC
Now is the time to start JALedit. The program is controlled from a single screen shown in
the next Figure.9 The screen consists of four parts. At the top is the row of buttons that
let's you control the program
On the left you fine the code explorer.10 Here you get an overview of the important code
elements present in your program such as includes, functions, procedures and variables.
9
To get the same screen go to the File menu and select the option New.
On the right, next to the code explorer is the work area with worksheets. In the next
Figure the work area contains one worksheet named "untitled1.jal". Your program will be
in a worksheet. You can load multiple worksheets - program source codes basically - at
the same time. This allows you to easily copy code from one program to the next. Only
one of the worksheets is active, which means that this is the program that will actually be
transferred to the Piccolino. The tab of the active program is blue.
The compile results area is at the bottom of the screen. Here you will get messages from
the compiler. Compiling means translating the code to a program that the microcontroller
can read.
Figure 8. JALedit screenshot.
A quick tour of JALedit.
1. Code Explorer area, this allows you to quickly find key elements from your
program.
10
The code is the program in human readable form. A program is this code, translated into
computer or microcontroller readable form. Programmers often mix the terms code and program,
and also use the words source, source code.
13
2. Work area, this is where the worksheet is that contains your program (as well as
other code).
3. Compile results area, this is where messages from the compiler are shown, for
example if your program contains any errors.
4. Memory overview. This shows how much Piccolino memory is used. The display
is in percentages. If any of the values is over 100% the program will not fit.
5. Start a new program. This will open a new worksheet called "untitled.jal", with a
trailing number behind "untitled" if you have more than one unnamed worksheet.
6. Open a program from your harddrive. You can load the program by double
clicking on it.
7. Save the current program. A suitable name for your program is required. You can
pick any name you want, but do make sure it contains no spaces, and don't add
.jal to it; JALedit will do that for you.11 To keep yourself organized it is a good
idea to make a separate directory for each project. Apart from the program you
can store other materials related to your project such as document, schematics
and pictures in that project directory.
8. Compile. That means you program is converted from JAL to Assembly in HEX
format. You will not be able to read the output. The program is not send to the
Piccolino just yet, this menu button is used to see if your programs contains
errors, or runs out of memory.
9. Compile and program the Piccolino. This button does the same as button 8,
except this time then program is send to the Piccolino.
10. The ASCII table. Computers (and thus microcontrollers) work with number, and
not with letters or other characters. In order to still be able to use letters it was
agreed that numbers can also represent letters or other characters. Which numbers
represent what, is listed in the ASCII table.12 The standard was made when
computers used 7 bits to communicate, so only 127 characters have been defined.
Higher numbers are also used, but they have a different meaning depending on
the application and the manufacturer. So these are by no means standard!
11. The terminal program of JALedit which allows you to communicate with the
Piccolino. You need to use the following settings:
11
Please note that the name of your program, nor the path to it, may contain spaces. If you want to
use multiple words you can use underscores (my_first_program), or be creative with capitals - the
so called camelback style: MyFirstProgram. Note that "c:\my documents" contains a space, so you
cannot use that directory. Not even when you make a subdirectory in it without space (for example
"c:\my documents\nospace".
12
American Standard for Computer Information Interchange.
Setting
Port13
Baud rate
Data bits
Stop bits
Parity
Flowcontrol
Value
The COM port that the Piccolino is
connected to, so in my case COM5.
115200
8
1
None
None
Table 2. Communication settings.
Please note: this is a standard terminal program, so it can only handle printable
ASCII characters in the range 32 through to 255 (where 129 through to 255 are
not standardized).
12. The name of the active JAL file. This is the source code that will actually be
compiled if you press buttons 8 or 9. The active JAL file's little tab is blue, do
pay attention this this when you are working with multiple JAL files at the same
time. You wouldn't be the first to wonder why a program doesn't work, only to
find out that the wrong source code was active.
13. The name of the active JAL file with path. You can use this to quickly verify if
the path and program name do not contain spaces.
Figure 9. Changing the COM port in JALedit.
13
You can only select COM ports that actually exist. If you use a USB2RS232 convertor, and the
convertor is not connected then the associated COM port doesn't exist. The solution is to first
connect the cable, if required switch on the Piccolino, and then set the COM port.
15
Now that JALedit is running we can enter our program. First however we will make sure
the COM port is set correctly. Select the "Tools" menu and then "Environment Options".
Now click the "Programmer" tab. At the bottom you will find the text "%F -p com5 -r
300". Replace the 5 with the number of the serial port on your PC. Make sure you do not
accidentally change anything else! Now click on the OK button and JALedit is ready for
use.
Figure 10. JALedit with the demo program.
A program consists of a series of commands. These commands tell the Piccolino what to
do. They are executed sequentially from the top down, unless you specify otherwise. The
programming language used in for the Piccolino (JAL) is not picky about the way you put
the commands on the page. But of course you need to understand it yourself, even years
later, so we make a couple of rules:
1. There is only one command on each line.
2. We will use short comments to explain the program.
3. At loops or conditional jumps we will indent.
We will get back to the meaning of rule three later. To enhance the readability of your
program JALedit color codes your program. That it for your ease of use only, it has no
impact on the function of the program.
The very first thing that we need to do is load a library. A library is a set of additional
commands. Those are mainly commands that cannot be part of the programming
language itself, because they are different for each microcontroller. Using libraries the
differences between microcontrollers are hidden from the user, which makes
programming a lot easier. The only thing the user needs to do is load the correct library.
The Piccolino is equipped with a 16f887 microcontroller, so we use the library
16f887_bert_piccolino. For loading the library the following command is used:
include 16f887_bert_piccolino
It is probably best if you wait with entering these commands until all parts of the program
have been explained, because they are not necessarily discussed in the correct order. At
the end of this section you will find the completed program. The standard JAL commands
are recognized by the editor, and given a suitable color. Variables and library commands
for example are blue. The include command is purple. The include command includes
everything that is in that specific library at that location in the program, without you
seeing anything of that.
The Piccolino contains a few standard hardware components that you can use without
having to declare them first.
Component
Red LED
Name
pLED
Button
pButton
Variable resistor
pPot
On analog channel pAN
Table 3. Standard components of the Piccolino.
In this demo project we will flash the red LED of the Piccolino. We can switch the LED
on and off with the statements "on" and "off".
pLED=on
pLED=off
In between we will need a small delay, otherwise the flashing will be so fast that you will
not see any of it. We will leave the LED on for 1 second:
delay_1s(1)
The number between brackets indicates how often the program needs to wait 1 second. If
the command were for example delay_1s(3) then the program would wait 3 x 1 second
equals 3 seconds.
17
We will leave the LED off for 100 milliseconds (0.1 second):
delay_100ms(1)
Using these commands the LED will flash just once, but a real flashing LED doesn't stop
after one flash. So we need to repeat the previous commands, in fact we need to repeat
them forever:
forever loop
end loop
Putting it all together results in the following program:
include 16f887_bert_piccolino
forever loop
pLED = on
delay_1s(1)
pLED=off
delay_100ms(1)
end loop
You see that the section that is repeated endlessly, the loop, contains commands that are
indented. This was rule three: at loops and conditional jumps we indent the code. This
makes it very easy to see where the loop starts and were it ends.
This is a good moment to enter the entire program in JALedit, and then save your work.
Use the File menu, and then select Save As. Enter a suitable name, for example demo (the
extension .jal will be added automatically) and place the program in a convenient
directory, for example:
c:\PICcolino\tmp
It is very convenient to make a separate directory for every project. That way you can
keep everything that pertains to that project in the same location. Please note: the file
name and the path name may not contain spaces! If they do contain spaces you get rather
weird error messages. 14
14
If you want to use multiple words you can use underscores (my_first_program), or be creative
with capitals (MyFirstProgram, also known as camelback writing). Do note that "c:\my
documents" contains a space, and it still contains a space even if you make a space-free
subdirectory like "c:\my documents\mydirectory".
The first step is to see if the program contains any errors. Click on the button with the
green triangle (compile active JAL file), or press F9.
Figure 11. Compile Active JAL
You can open multiple files (programs) at the same time, but only one of them is active.
The active program has a blue tab. If you followed the instructions you only have one
program (the one you just entered) so this is automatically the active one.
A new window appears for a short moment, and then the "compile results" window at the
very bottom contains new text.
Compilation started at :5/22/2012 4:35:49 PM
jal 2.4o (compiled May 8 2011) 15
Compiler CommandLine: C:\PICdev\JAL\Compiler\jalV24o\JALv2.exe -clear -no-variable-reuse
-s c:\picdev\jal\libraries "C:\PICcolino\tmp\demo.jal"
Errors :0
Warnings :0
Code :156/8192 Data:17/368 Hardware Stack : 2/8 Software Stack :96
No errors doesn't necessarily mean that the program will do what you want it to do, it just
means that you didn't make any syntax errors. If for example you accidentally spelled
delay as deley then this will cause an error:
[Error] (demo.jal) [Line 5] {FOREVER starts at C:\PICcolino\tmp\demo.jal:3}
[Error] (demo.jal) [Line 5] "deley_1s" not defined
[Error] (demo.jal) [Line 5] unexpected token: deley_1s
Compiler CommandLine: C:\PICdev\JAL\Compiler\jalV24o\JALv2.exe -clear -no-variable-reuse
-s c:\picdev\jal\libraries "C:\PICcolino\tmp\demo.jal"
Errors :5
Warnings :0
15
When you use a different JAL version you will get another number, and another compile date,
and perhaps even a slightly different lay-out. The content however will always be the same.
19
The error was made in line 5, and is duly reported: "deley_1s" not defined. Because of
this mistake the compiler is now confused, and doesn't understand anything beyond line
5. As a result it looks like you made a whole bunch of errors! The best way to handle this
is to solve the very first mistake, and then recompile. Then again you just solve the very
first mistake, and repeat this process until no mistakes are left.
Assuming your program doesn't contain any mistakes (if you did make a typo make sure
to correct it) we can now send the program to the Piccolino. Connect the Piccolino to the
power supply and to your PC.
Figure 12. Piccolino connect to a power supply and PC.
Switch the power supply on. The red Power LED will immediately come on. If not the
power is not connected correctly, or it is not suitable.16
16
The power supply doesn't have to be stabilized, but it must be decent current (DC) with the plus
on the center pin. The voltage should be between 7 and 10 volts. A higher voltage is possible (no
more than 30 volts), but then the voltage regulator on the Piccolino will warm up quickly.
Optionally you can power the Piccolino with a 9 volt battery.
Figure 13. The auto-reset jumper (in the circle) has to be in place.
Please note: to enable automatic programming the auto-reset jumper must be placed, see
the previous Figure. In fact you never need to remove this jumper, unless you prefer to
reset the Piccolino manually.17
Once the red LED is on and the Piccolino is thus powered you can click on the button
with the chip on it (compile + program) or press Ctrl-F9.
Figure 14. Compile + Program
The compile window appears again but this time it is followed by a black window. This
window is part of the software that will actually program the Piccolino.
17
If you use Hyper Terminal or pySerial then read the section "Use the Piccolino with Hyper
Terminal or pySerial" later in this document.
21
Figure 15. Programmer window.
If all goes well the window will disappear before you get a chance to read what it says.
For your convenience the text is shown in the previous Figure.
Once the black window has closed the red LED on the Piccolino will start flashing.
Congratulations, you have just made your first microcontroller program!
If all went well you can skip the the rest of this section and proceed to the next section. If
something went wrong consult the next table, take the appropriate action and try
compile+program again.
symptom
The block window didn't
open.
solution
1. There are errors in your program. Check the area at
the bottom of the JALedit screen (compile results)
for error messages.
2. You didn't install the downloaded software
according to the instructions. Read the instructions
carefully and redo the installation exactly(!) as
described.
3. You installed/moved the software in/to another
location than mentioned in the instructions. In
JALedit go to "environment options" in the
"compile" menu, select the "programmer" tab and
enter the correct data. Do note that the path names
may not contain spaces!
The black window shows an 1. Read the error message and solve the reported
error message and doesn't
problem. Usually the message is quite clear. If not
close. You can close the
one of the following issues may be the cause.
window by pressing the
2. The serial port is incorrect (the Piccolino is
enter key.
connected to another port). Adjust the port number
following the instructions earlier in this section.
3. The Piccolino is not powered. Switch the power on.
4. Another program is using the same serial port as the
Piccolino, and is keeping it occupied. For example
a terminal program. Close the connection, or the
program, and try again. Restart the PC if the other
program doesn't release the port correctly.
5. Your power supply isn't stable enough, has a too
low voltage, or doesn't supply enough power. Use
an appropriate power supply (7 to 10 volts DC, plus
on the center pin)
6. The auto-reset jumper is not in position. Place the
jumper.
The program has
1. The LED does flash, but very fast. Check to see if
downloaded but the LED
you left out one of the delay statements.
doesn't flash.
2. In your program you either switched the LED on
twice, or off twice, rather than on and off
alternatingly.
In the black window the text A Piccolino program that sends data to the PC can
"Establishing contact etc"
prevent the PC from programming the Piccolino. The
will not go away. You have text "Establishing contact..." will remain visible in the
previously been able to
black screen until the program finally gives up. The
successfully program the
solution is simple: while this text is shown press the
Piccolino.
reset button on the Piccolino, count to three, and then
release the button. At that moment program will
commence.
If that fails switch the Piccolino off. Click on "Program
+ Compile" again. Wait for the text "Establishing
contact etc" to appear, and then switch the power back
on. This will force the Piccolino to accept the program
(this method always works).18
Table 4. Possible issues and ways to solve them.
If, despite the solutions provided in the table, it still doesn't work, check everything again.
Perhaps sleep on it: everything looks different the next day.
18
See also the next section: "Programming fails".
23
If you are convinced that you have followed the instructions to the letter, but it still
doesn't work, then come to the JAL user group on Yahoo (see the support page for an upto-date address) and ask your question. The mandatory language in this group is English
(the users are located all over the world). They will be happy to help you.
Programming fails
A Piccolino program that sends unsolicited data to the PC can prevent the PC from
reprogramming the Piccolino. In the black window the text "Establishing contact etc" is
displayed until the program eventually gives up. This problem occurs when the Piccolino
program sends unsolicited data to the PC. For example with a loop like this:
forever loop
serial_hw_write(data)
end loop
If the PC is not listening, for example because you're writing a program, the data fills up
the buffer on the PC. When you want to program the Piccolino, the programmer software
gets the old data that is still in the buffer, instead of the Piccolino reply it is expecting.
Hence it gets confused and programming fails.
The solution is simple: while this text is shown press the reset button on the Piccolino,
count to three, and then release the button. At that moment programming will commence.
If that fails switch the Piccolino off. Click on "Program + Compile" again. Wait for the
text "Establishing contact etc" to appear, and then switch the power back on. This will
force the Piccolino to accept the program (this method always works).
You can prevent this problem by making sure that the Piccolino only sends data on
request of the PC. Like this for example:
forever loop
serial_hw_wait(something)
serial_hw_write(data)
end loop
Use the Piccolino with Small Basic
The combination of Piccolino and Small Basic allows you to very rapidly make
applications where the PC is controlling the Piccolino, or the Piccolino is collecting data
that the PC displays.
Piccolino
include 16f887_bert_piccolino
forever loop
if pButton then
serial_hw_write(adc_read_low_res(pAN))
delay_10ms(1)
end if
end loop
Please note: the Piccolino program assumes port 5. If you use another COM port make
sure to change that in the program (the relevant command is printed bold)
PC
GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenColor = "LightGreen"
GraphicsWindow.Width = 505
GraphicsWindow.Height = 280
GraphicsWindow.BrushColor = "LightGreen"
GraphicsWindow.FontBold = "no"
GraphicsWindow.DrawBoundText(30, 0, GraphicsWindow.Width, "Piccolino Variable
resistor")
GraphicsWindow.BrushColor = "Black"
if ComPort.OpenPort("com5",115200)= "true" then
While("True")
lasty=y
y=255-ComPort.RXbyte()
'TextWindow.Write(y)
x=x+1
If x>500 Then
'GraphicsWindow.Clear()
x=0
endif
'delete part of the graph
GraphicsWindow.FillRectangle(x,20,4,GraphicsWindow.Height-20)
If x>1 then
'move the data +20 to avoid the text
GraphicsWindow.DrawLine(x-1, lasty+20, x, y+20)
25
EndIf
Endwhile
comport.ClosePort()
else
TextWindow.WriteLine("This comport doesn't exist")
endif
This set of programs measures the position of the variable resistor on the Piccolino, sends
the data to the PC and displays it in a graph.19 This only happens while the button on the
Piccolino is pressed. If the screen is full the graph restarts and slowly erases the old
graph.
Figure 16. Result of the programs.
All you need for this project is the Piccolino, obviously connected to a PC.
Use the Piccolino with JAL books
You can use the Piccolino with the JAL books.20 All you need to do is to convert the
programs to the 16f887, the microcontroller that the Piccolino uses. Normally that would
be rather difficult, but the use of the _bert libraries makes this easy. It is possible that the
microcontroller has properties that the 16f887 doesn't have (for example CAN bus) in
which case conversion is of course impossible. In a few cases you may need to change
19
Do you want to know more about Small Basic consult the book "PC Programming - A Practical
Approach" (expected 2013) or the book "Supersnel PC Interfacen" (expected in English: 2014).
20
See the section "Done! What's next" for the names of these JAL books.
port or pin number because they Piccolino doesn't have these, or uses them for another
purposes. And of course you can make good use of the components already on-board the
Piccolino.
Component
Red LED
Name
pLED
Button
pButton
Variable resistor
pPot
On analog channel pAN
Table 5.Standard components of the Piccolino.
As an example we will convert the tutorial project from the "PIC Microcontrollers". In
this project two LEDs are connected to a microcontroller, and flash alternatingly. The
hardware looks like this:
LED
330
c4
330
d3
LED
GND
Figure 17. Connecting LEDs to the Piccolino.
Figure 18. The complete project.
27
The pins that the tutorial project uses for the 16f877A also exist on the Piccolino. So the
only thing we need to do is to change the include from "16f877A_bert" to
"16f887_bert_piccolino" and the program works. Perhaps a bit feeble, but in most cases it
really is that simple with the _bert libraries!
Tutorial program 16f877A
Tutorial program Piccolino
include 16f877A_bert
include 16f887_bert_piccolino
-- definitions
pin_c4_direction = Output
pin_d3_direction = Output
-- definitions
pin_c4_direction = Output
pin_d3_direction = Output
forever loop
forever loop
-- LEDs in state 1
pin_d3 = high
pin_c4 = low
delay_1s(1)
-- LEDs in state 1
pin_d3 = high
pin_c4 = low
delay_1s(1)
-- LEDs flipped
pin_d3 = low
pin_c4 = high
delay_1s(1)
-- LEDs flipped
pin_d3 = low
pin_c4 = high
delay_1s(1)
end loop
end loop
Table 6. Tutorial program in the Piccolino.
Use the Piccolino with Hyper Terminal or pySerial
Please note: Some communication software insists on using flow control, which cannot
be disabled, for example Hyper Terminal. This software flow control uses the exact same
pin that the Piccolino uses for the auto-reset.
If you want to use this kind of software you must remove the jumper. The same applies to
the Python pySerial module.
If you do not remove the jumper during communication the Piccolino appears to be
unresponsive. This is because it is continually reset and never gets the chance to do
anything at all.
Done! What’s next?
Would you like to know more about programming in JAL, or about connecting all sorts
of hardware components? There are four more books on JAL by the same author, filled to
the brim with fun, exciting and sometimes rather weird projects. The books do not use the
Piccolino, but conversion is usually quit simple.
Figure 19. PIC Microcontroller Programming in 10 captivating lessons
Using the lessons in the book "PIC Microcontroller Programming in 10 captivating
lessons" you learn how to program a microcontroller. You’ll be using JAL, a free but
extremely powerful programming language for PIC microcontrollers, which enjoys great
popularity in the hobby world. Starting out from scratch virtually, you slowly build up the
knowledge. No previous knowledge is needed: anyone can get started with this book.
Assuming you have absorbed all lessons — meaning you have actually completed all the
exercises — you should be confident to write PIC microcontroller programs, as well as
read and understand programs written by other people.
You learn the function of JAL commands such as include, pin, delay, forever loop, while
loop, case, exit loop, repeat until, if then, as well as the use of functions, procedures and
timer- and port interrupts.
29
You make an LED blink, build a time switch, measure a potentiometer’s wiper position,
produce sounds, suppress contact bounce, and control the brightness of an LED. And of
course you learn to debug, meaning: how to spot and fix errors in your programs.
You learn to recognize various components including the PIC microcontroller,
potentiometer and quartz crystal, and how to wire up a PIC microcontroller and
effectively link it to your PC. A breadboard is used for the purpose, allowing you to
easily modify the component arrangement for further experimenting.
Figure 20. PIC Microcontrollers - 50 projects for beginners and experts.
The book "PIC Microcontrollers" covers 50 exciting and fun projects using PIC
microcontrollers, such as a time switch, dice, secret doorbell, dark activated switch, silent
alarm, electric motor control, people sensor, ultrasonic sensor, pictures of light, night
buzzer and electric candle.
You can use this book to build any of the projects for your own use. The clear
explanations, circuit diagrams and photos of the setup on breadboards make this a fun
activity.
You can also use this book as a study guide. For each project an explanation is given why
it was designed in a certain way. Datasheet are used whenever appropriate. This way
you'll learn a lot about the project and the microcontroller used. You can adapt the
programs to suit your own needs, which makes it ideal for schools.
The book also makes an ideal reference guide. You'll find an explanation for all JAL
programming statements. Even when you've built all the projects it's highly likely that you
would want to keep this book next to your PC.
Figure 21. 50 PIC microcontroller projects - for beginners and experts.
The book "50 PIC microcontroller projects" also contains 50 fun and exciting projects
using PIC microcontrollers, such as a laser alarm, USB teasing mouse, egg timer, youth
deterrent, sound switch, capacitive liquid level measurement, "finger in the water" sensor,
camera-based alarm system, 220 volt light dimmer, talking microcontroller and much
more.
Numerous techniques are discussed such as relays, alternating current control including
mains, I2C, SPI, RS232, USB, pulse width modulation, rotary encoder, interrupts,
infrared, analogue to digital conversion (and vice versa), 7-segment display and even
CAN bus
You can use this book to build any of the projects for your own use. The clear
explanations, circuit diagrams and photos of the setup on breadboards make this a fun
activity. For each project an explanation is given why it was designed in a certain way.
You can use this book as a study book as the basis for larger and more complex projects.
All circuits are built on a breadboard, which makes it easy for you to adapt and expand
them.
The three PIC microcontrollers used in this book are the 16f877A, 18f4455 and 18f4685.
It is also discussed how you can migrate your project from one microcontroller to another
(15 types are supported) including two example projects.
This book is not a sequel but rather an expansion of the "PIC Microcontrollers" book by
the same author. If you own both books you have a massive 100 projects for 6 different
microcontrollers!
31
Figure 22. Artificial Intelligence, 23 projects to bring life to your microcontroller!
The book "Artificial Intelligence" contains 23 special and exciting projects about artificial
intelligence and machine learning, for the PIC microcontroller and PC.
You will learn how to set up a neural network in a microcontroller, and how to make the
network self-learning. Discover how you can breed robots, and how changing a fitness
function results in very different behavior. Find out how a PC program exposes your
weak spots in a game, and ruthlessly exploits them
Many artificial intelligence techniques are discussed and used in projects such as an
expert system, neural network, subsumption, emerging behavior, genetic algorithm,
cellular automaton and roulette brains. Every project has clear instructions with circuit
diagrams and photos so you can start immediately. There are many suggestions and
literature links to inspire you to make many more projects. Even after you have built all
the projects in this book, it will remain a valuable reference guide to keep next to your
PC.
This unique book is full of information that you won't find anywhere else, clearly written
and easy to understand. This is a brilliant book for anyone with an interest in artificial
intelligence and machine learning.
Piccolino (JAL) language instructions
JAL (Just Another Language) is a free-format language for programming PIC
microcontrollers. The commands can be spread out over the lines as you please. Tabs,
spaces, and newlines are all considered whitespace. There is no delimiter between
different commands. In theory you could put all commands on one long line. In practice,
however, appropriate whitespace results in easier-to-read programs because any
convention you choose is possible.
JAL is the only advanced free language, and has a large and active international user
base. It is configurable and extensible by use of libraries and can even be combined with
assembler.
A typical JAL program will start with a call to a library that contains relevant details for
the microcontroller for which the program is written. If for example you use the 16f877A
microcontroller you will use the 16f877A_bert library. Note that the _bert libraries are
available only for the microcontrollers that are discussed in this book.
include 16f877A_bert
Then the variables and constants will be declared:
var byte a
Next are the commands:
forever loop
a=a+1
end loop
It is a good practice to add comments to your program to indicate what it does and for
which JAL version it is written. So a simple program might look like this:
--JAL 2.4j
include 16f877A_bert
var byte a
--demo program
forever loop
a=a+1
end loop
33
Variables
Here the power of JAL is immediately clear. Both unsigned (positive) as well as signed
(positive and negative) variables can be used, containing up to 32 bits21.
name
bit
byte
sbyte
word
sword
dword
sdword
description
1 bit unsigned boolean
8 bit unsigned
8 bit signed
16 bit unsigned
16 bit signed
32 bit unsigned
32 bit signed
range
0 or 1
0 to22 255
-128 to127
0 to 65,535
-32,768 to 32,767
0 to 4,294,967,296
-2,147,483,648 to 2,147,483,647
Table 7. Different types of variables and their ranges.
You can even define variables with any bit length you want, such as:
var bit*2 demo
Variable demo is now 2 bits long (and can thus contain the values 0, 1, 2 or 3). When a
value is assigned to demo it doesn’t necessarily have to be two bits, but only the lowest
two bits will be put into the variable (the others do not fit).23
So the statement:
demo = 99
will result in a value of 3 for demo, because the number 99 in binary is 0b_0110_0011,
and the lowest two bits are set to 1, which equals 3 in decimal.
Besides decimal values you can use other number bases as well. In that case you need to
add a prefix to the number. Possible bases are:
21
Variables with a size of more than 16 bits can only be used in microcontrollers that have so
called shared memory. Of the microcontrollers in this book the 10f200 and the 12f675 do not have
such memory locations, so they are limited to 16 bits or less.
22
"To" in this context means "up to and including".
23
The compiler will notice and give you a warning. If you did it intentionally, you can ignore the
warning. If you use the settings in the download package you will not see the warnings, because
they are switched off by default.
23
0x1F
0q07
0b01
decimal
hexadecimal
octal
binary
And of course you can use letters:
"Hello"
string
For readability purposes underscores can be added to numbers in any position you want.
To the compiler the number 10_0 is identical to 100. Binary value assignments almost
always use underscores to make them easier to read:
a = 0b_1001_1110
Declaring variables must be done before they can be used.
35
Here are a few possibilities to do this:
command
var byte a
description
a is declared as a byte
var byte a = 0x1F
a is declared as a byte and assigned a value at the same time
var byte a at 0x06.
a is declared as a byte at memory location 0x06 24
var byte volatile a
a is declared as a byte and will not be optimized away by the
compiler25
var byte a is b
a is a synonym or alias for b (b must be declared first)
Table 8. How to declare a variable.
The final declaration can be used to give pins names that are easier to remember. Lets
suppose that a red LED is connected to pin c1. If you use this command you can refer to
pin c1 using the redled alias:
var bit redled is pin_c1
For example, redled = 1 would make pin c1 high and thus turn on the LED. This will
make the program easier to read. But it is also easier if you want to migrate your program
to another PIC. If this microcontroller doesn’t have a pin c1 all you have to do is change
the declaration to a pin that the microcontroller does have, such as:
var bit redled is pin_a1
24
It is important to understand what you are doing when you force variables to a certain memory
location. It can also be used as a convenient way to split variables. This example declares an array
in exactly the same memory location as the word variable demo:
var word demo
var byte dem[2] at demo
Demo is a word (two bytes). Since the array dem is also two bytes long, and in the same memory
location, this means that the first item in the array, dem[0], is the low byte and the second item,
dem[1], is the high byte.
25
The compiler optimizes the program during compiling, and will remove all unused parts.
References to a pin will also be removed since the compiler doesn’t know that pins can change
"from the outside". So, to the compiler the command seems useless. The volatile keyword is used
to prevent this. The compiler will leave a volatile variable alone. The average user will almost
never need the volatile keyword.
The rest of the program can remain unchanged.
Mixing different variable types in a calculation is possible, but care should be taken that
intermediate results will be stored in the variable type that the calculation started with. If
you multiply a byte with a byte and put the result in a word, like this:
var byte num1, num2
var word num3
num3 = num1 * num2
the result will never be larger than 255. This is because num1 and num2 are multiplied
using bytes and the result is transferred into num3.
You can force the compiler to use a word for intermediate calculations by changing the
multiplication to num3 = word(num1) * word(num2).
Constants
When you know in advance that a variable will be assigned a value once and will never
change it is not a variable but a constant. This can be declared using const, like this:
const byte demo = 5
The advantage of using constants is that a variable uses RAM memory and a constant
doesn’t (the compiler uses the value rather than the constant name). So it is a good idea to
use constants whenever you can. In small programs such as in this book it is not really an
advantage, because we are never even close to running out of memory.
Forever loop
This command ensures that a particular part of the program is executed forever. Many
microcontroller programs use this command since it is a convenient way to make sure the
program never stops.
forever loop
[ commands ]
end loop
You can use the command Exit Loop to leave this loop.
37
While loop
The while loop executes a series of commands as long as a certain condition is met (true).
This loop is executed as long as the condition a is smaller than b is true. If the condition
is not met (false) the program will exit the loop.
while a < b loop
[ commands ]
end loop
You can use the command Exit Loop to leave this loop even if the condition is still true.
For loop
The for loop executes a series of commands a fixed number of times.
This loop is executed ten times:
for 10 loop
[ commands ]
end loop
Normally the compiler will count "internally", but you can force it to use a certain
variable to count the loops, with the using command. This is very convenient if you want
to know inside your loop how many times it has already been executed. For example,
when you want to determine the position in an array.
for 10 using counter loop
[ commands ]
value = demo[counter]
end loop
Because microcontrollers start counting at zero the variable counter will get values from 0
to 9. You can use the command Exit Loop to leave this loop even if the loop hasn't been
executed enough times.
Repeat until
With this command you can repeat a series of commands until a certain conditions is met
(true).
repeat
[ commands ]
until condition
The next example will repeat the addition until variable a has value 8:
repeat
a=a+1
until a == 8
Note the double = sign for this is a condition and not a calculation. You can use the
command Exit Loop to leave this loop even if the condition is still false.
Procedure
A procedure is a part of program that is needed more than once. Rather than typing it
several times it is put aside and given a name. This particular part of the program is only
executed when it is "called" by that name. Using procedures usually makes a program
easier to read and maintain.
This is an example procedure demo
procedure demo is
[ commands ]
end procedure
You can call this procedure simply by using the name as a command. For the procedure
shown above the call would be
demo
In procedures you can use variables just as in any other part of the program. Any variable
declared outside the procedure is also valid inside a procedure. If you define a variable
inside a procedure you can only use it inside that particular procedure. This is called a
"local variable".
39
procedure demo is
var byte a
[ commands ]
end procedure
In this example variable a is declared inside the procedure so it is not known to the rest of
the program.
If you want to give a value to a variable from outside the procedure you need to "pass" it
to the procedure. The procedure would look like this:
procedure demo (byte in a) is
[ commands that use variable a ]
end procedure
Note that this declares variable a. You must not declare it in the main program or in the
procedure. Inside the procedure you can now use variable a, which gets a value when the
procedure is called. If for example you want to call this procedure and give a value 6 you
use this command:
demo (6)
In this same way you can pass variables out of the procedure, but then you need to
declare them as "byte out" instead of "byte in" in the procedure name. This is an example
of a real procedure:
procedure Serial_HW_Write ( byte in Data ) is
-- wait until TXREG is empty
while ! TXIF loop end loop
-- then put new byte in TXREG
TXREG = Data
end procedure
If this procedure is for example called using Serial_HW_Write(6) then Data gets value 6,
and is used in the line TXREG = DATA.
This is a good way to make new commands that you can add to JAL. If you have made
new procedures, for example to control a certain component or function, you can put
them in a separate file. This file is then called a library and you can use it by "including"
it in your program using the include command.
40
The advantage is that your program becomes much easier to read, and the next time you
need that particular component or function you can simply load your library, and off you
go.
Interrupt
An interrupt routine is a procedure that is called under certain special conditions. For
example when a pin changes value of a timer overruns. The normal program is
interrupted (hence the name) and this special procedure is executed. Once the procedure
is terminated the normal program will continue where it left off.
procedure demo is
pragma interrupt
[ command ]
end procedure
The first line of an interrupt procedure is the pragma interrupt command. You can have as
many interrupt procedures as you want, the compiler will combine them for you. In
general however you should try to avoid using interrupts unless it is absolutely necessary.
Interrupt routines may not be called by the main program or other procedures or
functions.
Function
A function is basically the same as a procedure. The main difference is that a function
always returns a value. The returned value needs to be declared using the return
statement.
In this function variable a is incremented by one:
function demo (byte in a) return byte is
var byte b
b=a+1
return b
end function
In the declaration of the function it is indicated that an input is expected (byte in) and that
the answer the function will return is a byte (return byte). Inside the function the "return
b" statement indicates that b will be the value that is returned.
41
This is an example of a function call:
x = demo(4)
where x will get the value 5 (4 + 1).
Functions are often used to return a status rather than a number, such as true or false, or a
number and a status indication. This is a good example of such a function:
function Serial_HW_read ( byte out Data ) return bit is
-- test if byte available, and if so get byte
if RCIF then
Data = RCREG
else
return false
end if
return true
end function
This function first checks to see if RCIF is true, which means that something has arrived
in the buffer of the serial port. If true the content of the buffer is copied into variable
Data. The function itself gets status true.
If the buffer is empty the function itself gets status false. In that case you know that no
information has arrived, and that Data contains no relevant information. If you want your
program to wait until information has arrived you can use this line:
while !serial_hw_read(data) loop end loop
As long as the function is false (not true) meaning the buffer was empty, the loop is
repeated. As soon as the function is true the loop is exited and Data contains the newly
received information.
In-line assembly
You can use assembler inside your JAL programs. I don’t think this will ever be really
necessary, but perhaps you found a nice snippet on the Internet or want to use an example
from the datasheet.
You can use individual assembler statements using the asm prefix, like this:
asm movlw 0xFF
If you need multiple statements it is easier to use an assembler block, like this:
assembler
movlw 0xFF
bsf 5
movwf pr2
bcf 5
end assembler26
When your program is compiled the JAL compiler generates a HEX file for the
microcontroller as well as an assembler file. You can use this to exchange programs with
assembler users or to use Microchip tools.
All assembler commands ("mnemonics") that you find in the datasheet of the
microcontroller in your project can be used. Additionally, a few assembler macros that
are often used on the Internet can be used too, for example:
command
description
OPTION k
Copy literal k to the OPTION register 27
TRIS {5,6,7}
Copy W to the TRIS {5,6,7} register
MOVFW f
Copy f to W (a synonym for MOVF f, W)28
SKPC
A synonym for BTFSS _status, _c
SKPNC
A synonym for BTFSC _status, _c
SKPZ
A synonym for BTFSS _status, _z
SKPNZ
A synonym for BTFSC _status, _z
26
A very complicated way to assign value 0xFF to pr2 in assembler, because the program is in
memory bank 0 and pr2 is in memory bank 1. In JAL you don't have to worry about things like
that, in fact you don't even need to know what memory banks are. So you can just use pr2 = 0xFF.
27
The official explanation is move. However since the original version is retained it is in fact copy.
28
W is the working register, f is the register file address. MOVFW myvar puts the value of myvar
in W, MOVLW myvar puts the address of myvar in W.
43
If you use variables from the JAL program in your assembler block you cannot be sure
where they are because JAL may store them in any available memory bank. This also
means that at the moment the JAL program is interrupted for your assembler block, you
have no clue where the bank pointers are pointing to. That means you don’t have access
to the registers, even though you do know where they are.
The solution is to ask the compiler to fix the bank pointers for you. You can do this by
adding bank to every line in which you use JAL variables or registers.
procedure register_write( byte in Address, byte in Data ) is
assembler
bank movf address,w ; move address to working variable w
movwf FSR
; move address to file select register
bcf irp
; make irp zero so we use bank 0 and 1
; (indirect register bank select), 0 = bank 0/1
bank movf data,w
; move data to w
movwf indf
; move data to indirect file register as
; referenced by fsr
end assembler
end procedure
I wrote this fragment for the regedit29 library and you see that the bank statement is used
in all lines containing address and data, because the location of these JAL variables is
unknown. The line referring to the registers FSR, IRP and INDF do not have the bank
keyword, because they are accessible from all banks.
Task
Even though a microcontroller can only do one thing at a time you can still do
multitasking, just like on a PC. The different tasks can be defined using the task
command. Every time the program encounters a suspend command the current task is
interrupted. The scheduler then checks to see which task has been waiting for the longest
time and hands control over to that task. This system only works if the tasks are "honest"
and use the suspend command regularly.
29
This is a library to read and change registers while the program is running. This is not discussed
in this book. The latest version of the library (contained in the download package) is written in
JAL, so the fragment is no longer used.
Just like in a procedure, tasks can have their own local variables.
task name1 (parameters) is
[ commands ]
suspend
[ commands ]
end task
task name2 (parameters) is
[ commands ]
suspend
[ commands ]
end task
start task1(parameter values)
start task2(parameter values)
forever loop
suspend
end loop
Before a task can be run you need to start it using start. The task can be stopped using
end task.
IMPORTANT: If you want to compile a program containing tasks you need to tell the
compiler how many tasks there are. Note that the main program is also counted as a task.
So for the previous example the program should contain this command:
Pragma task 3
In older JAL versions the number of tasks must be submitted in the command line, so in
this example using -task 330.
If then else
This command is used to make a choice between two possibilities. If one condition occurs
then something is done, else something else is done.
In this example b gets the value of 2 when a is equal to 1. In all other cases b gets 3.
30
If you use JALedit you can enter this at Compile - Environment Option - Additional
Commandline Parameters.
45
if a == 1 then
b=2
else
b=3
end if
This command can be nested, like in this example.
if a == 1 then
b=2
else if a == 2 then
b=3
else
b=4
end if
end if
Note that else if are two words. The above program yields the following results:
if
then
a=1
b=2
a=2
b=3
a = something else
b=4
Instead of else if you can also use the command elsif. The syntax is as follows:
if a == 1 then
b=2
elsif a == 2 then
b=3
elsif a == 3 then
b=4
else
b=5
end if
The elsif statement is not used very often.
Case
If you need to make multiple decisions based on the value of a single variable the case
statement is a good alternative to a row of if then else statements. In the following
example b is given a value based on the value of a. If a is 3 than b will be 4. If the value
of a is not listed then b will get the value 6.
case a of
1: b = 20
2: b = 13
3: b = 4
4: b = 59
otherwise b = 6
end case
Contrary to the if then else statement the case block can only have one statement per
choice. So if you want to give b the value 13 and aa the value 188 when a is 2 then this is
not possible. You will have to use a procedure, or the block statement.
Block
The block command can be used to group statements together. Variables defined inside a
block can only be used in this block. A block is a program section and will be executed
when it is encountered. In cannot be called form another location.
block
[ commands ]
end block
This is particularly useful in combination with the case command, or if you want to keep
variables local to just a small portion of your program. This could happen if you want to
use parts of different programs combined into one, that all use the same variable names.
case a of
1: b = 20
2: block
b = 13
c = 188
end block
3: b = 4
otherwise b = 6
end case
47
Array
Normally speaking a variable has only one value. With an array a variable can be given a
whole range of variables. In the following example the demo array is assigned a row of
five values.
var byte demo[5] = {11,12,13,14,15}
To get a value out of the array the number between square brackets indicates the position
that you want. Remember that computers start counting at 0, so the first position in the
array is 0. In our example demo[0] contains the value 11.
This command selects the fourth number in the array (the value 14):
a = demo[3]
Adding a value to an array (or modifying one) is done in a similar way. In this command
the fourth position in the array is assigned the value in a:
demo[3] = a
Your program can use the count statement to determine the length of an array. For
example:
a = count(demo)
That means that count is a reserved word and cannot be used as a variable name. Be
careful with your array size, since an array has to fit within a single RAM memory bank.
A bit array is not possible.
Long table (also known as: Lookup table or LUT)
If you need more space that you can fit into an array you can use the long table. This is
an array with just constants. That means you define it once, and afterward you cannot
make changes to its content. This is because even though it is an array, it is not contained
in RAM, but in program (flash) memory.
The long table can be very long indeed; in the 16F877A a whopping 8000 bytes! Of
course the length can never be more than the available program memory. So in a small
PIC, or with a large program, long tables must be shorter. You can check this by
compiling the program with a long table with a length of 1. Based on the resulting
program size the available length for the long table can then be calculated. See section 6.3
for more information on how to determine the size of a program.
If the long table doesn’t fit the compiler will generate an error, and your program will not
be downloaded into the 16F877A.
The syntax of the long table is:
const byte long_table[2049] = { 1, 2, 3, 4, 5, ...}
var word x
var byte y
y = long_table[x]
where the above values are just for demonstration purposes.
Operators
JAL has a wide variety of operators. The most important ones are shown below, with a
simple example if appropriate.
Operator
!!
Explanation
Logical. Indicates whether a variable is zero or not.
For example !!5 = 1, and !!0 = 0
!
Not, or Complement. Flips 0 to 1 and 1 to 0 at the bit level.
So !5 = 250 because !0b_0000_0101 = 0b_1111_1010
*
Multiply.
/
Divide without remainder.
So 9/2 = 4
%
The remainder of the division, also known as MOD
So 9%2 = 1
+
Add.
-
Subtract.
Left shift. Move all bits one position to the left. Note that the newly
created bit is set to 0. When a signed variable is shifted the sign is
retained.
<<
49
Operator
>>
<
Explanation
Right shift. Same as left shift, but in the other direction.
Less than.
<=
Less than or equal to.
==
Equal to. Note that these are two equal signs == in a row. Accidentally
using only one = is a very common mistake (feel free to view this as an
understatement). The single = sign is a calculation not a condition.
!=
Not equal to.
>=
Greater than or equal to.
>
Greater than.
AND comparison at the bit level. The truth table is:
&
1&1=1
1&0=0
0&1=0
0&0=0
OR comparison at the bit level. The truth table is:
|
1|1=1
1|0=1
0|1=1
0|0=0
XOR (eXclusive OR) comparison at the bit level. The truth table is:
^
1^1=0
1^0=1
0^1=1
0^0=0
Table 9. Operators.
Pragma
Pragma is a command for the compiler. It is a very powerful but complex command. A
detailed explanation would be out of scope for this book, particularly since you will never
(or rarely) use most of the commands.
This table lists the most important pragma's:
command
description
pragma eedata
Stores data in the EEPROM of the microcontroller.
For example pragma eedata "O","K" will store the letters
O and K.
pragma interrupt
This can only be used inside a procedure. The result is that
the procedure is added to the interrupt chain, a series of
procedures that is executed when an interrupt occurs.
There is no limit to the number of procedures in the chain,
but the execution order is not defined. Interrupt procedures
may not be called from the program itself of from other
proecdures or functions.
Pragma clear yes
Give all variables value zero at the beginning of the
program. This does not apply to volatile variables or
variables that were given an explicit value by the
programmer.
Pragma task
Indicates the number of tasks in the program. If you use
tasks then remember that the main program counts as a
task too. You can either use this command or enter the
number of tasks on the commandline. This last option is
mainly for older versions of JAL that don't support this
pragma.
Table 10. A few of the most relevant pragma's.
Comments
Lines containing comments are preceded by two dashes or a semicolon. You need to do
this for each line, as there is no block comment statement.
; this is a comment
-- and this too
Comment lines are used to clarify what a program is for, or why things are done the way
they are done. This is very handy for future reference, or when you want to share
programs over the Internet.
51
It is good practice to indicate the JAL version on the very first line of your program. That
eliminates a lot of questions!
Good comments are not about what a certain statement does (unless you are writing a
book), because the reader may be expected to know this. They are about why you use the
statement. If you make a library you should use comment lines to explain in detail what
the library is for and how it should be used. Libraries without these comments are
completely useless.
The _bert_piccolino library
Libraries are used for custom settings required for different microcontroller types, and to
define additional commands. Since everybody can write and publish libraries, many do
not work together.
The Piccolino comes complete with a large set of libraries, combined into one
comprehensive set: the 18f4455_bert_piccolino.31 This library adds a large series of
commands to JAL. And it solves all compatibility issues.
The library adds commands that are used and discussed in my JAL books, such as analog
conversion, serial communication, read and write from memory and much more. See the
section "Done! What's next" for more information on these JAL books.
31
This _bert_piccolino library is made specifically for the Piccolino. Combination libraries for
"separate" PIC microcontrollers also exist. These libraries all contain the text "_bert" in their
name, and are available for a limited selection of PIC microcontrollers. So if you want to buy a
PIC first make sure it is supported. You will find a list of supported types on www.vandam.tk,
follow the link to the Starterpack.
Index
! ........................................................ 49
!! ........................................................ 49
!= ........................................................ 50
% ........................................................ 49
& ........................................................ 50
^ ........................................................ 50
| ........................................................ 50
<<........................................................ 49
==........................................................ 50
16f877A .............................................. 27
16f887 ................................................... 3
16f887_bert_piccolino ........................ 17
18f4455 ............................................... 27
ANx ...................................................... 5
array .................................................... 48
Array ................................................... 48
assembler ............................................ 43
auto-reset jumper ................................ 21
Baud rate ............................................. 15
bit ........................................................ 34
bit at .................................................... 36
bitlength .............................................. 34
Block................................................... 47
byte ..................................................... 34
byte is.................................................. 36
byte out ............................................... 40
Case .................................................... 47
CCP1..................................................... 6
CCp2 ..................................................... 6
Code Explorer ..................................... 13
comment ............................................. 51
compile ............................................... 19
compileren .......................................... 14
const .................................................... 37
Constants ............................................ 37
Data bits .............................................. 15
delay_100ms ......................................... 8
delay_1s(1) ......................................... 17
dword...................................................34
elsif ......................................................46
end loop .................................................8
end task................................................45
Errors. JALedit ....................................19
Establishing contact.............................24
Establishing contact.............................23
Exit loop ..............................................37
Flowcontrol .........................................15
For loop ...............................................38
forever loop ...........................................8
Forever loop ........................................37
Function...............................................41
GND ......................................................5
headers...................................................5
iclude ...................................................17
If then else ...........................................45
include .............................................8, 33
In-line assembler .................................42
interrupt ...............................................41
jumper, Piccolino ................................21
Left shift ..............................................49
Long table............................................48
Lookup table ........................................48
LUT .....................................................48
MOD....................................................49
NC .........................................................5
Nieuw ..................................................14
Openen ................................................14
Operators .............................................49
Opslaan ................................................14
pAN ...............................................17, 27
Parity ...................................................15
pButton ..........................................17, 27
Piccolino ................................................1
pLED .............................................17, 27
pPot................................................17, 27
Pragma .................................................50
53
pragma clear ....................................... 51
pragma eedata ..................................... 51
pragma interrupt ................................. 51
pragma task ................................... 45, 51
pragme interrupt ................................. 41
Procedure ............................................ 39
Repeat until ......................................... 39
Right shift ........................................... 50
sbyte .................................................... 34
SCL ....................................................... 6
SDA ...................................................... 6
SDO ...................................................... 6
sdword .................................................34
start task...............................................45
stop bits ...............................................15
suspend ................................................45
sword ...................................................34
Task .....................................................44
Vin .........................................................5
virtuele seriële poort ............................12
volatile .................................................36
werkblad ..............................................13
While loop ...........................................38
word.....................................................34
55