HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite Tutorial Table of Contents

Pascalite Programming: Let There Be Light!

This version of this tutorial is fine tuned for a Pascalite, or the free software Pascalite emulation. Please send me an email with "complaints" if something doesn't work!

If you have the open source FPC, aka Free Pascal or Borland's Turbo Pascal (TP hereafter), then there's a separate tutorial for you, covering much of the ground as presented here for the Pascalite crowd.

One view of programming says that it is all about three simple things:

One view of programming says that it is all about three simple things:

In our programs to date our input has been via the switches b0 and b1. We, the human users, change the switch settings to send messages IN to the computer.

Following the dictates of the instructions provided by the programmer, the computer "digests" the inputs. This is the "processing" element, and it leads to....

Some kind of message being PUT OUT from the computer. This is the output. To date, we've been using the free LCD simulator. Unfortunately, if you want to start using the Pascalite hardware, you'll find that the LCD panel is an extra, and not cheap. (Not overpriced for what it is... but still serious money.)

Happily, there's a simple alternative, albeit one that is a lot less capable: LEDs. The free, virtual, Pascalite hardware simulation that we have been using has LEDs already wired up. If you have a real Pascalite, you only need an LED and a resistor, and only have to make three electrical connections.

Sticking (mostly) with the familiar, enter the following program:
program fifth;
var boItWas,boItIs: boolean;
if b0 then set(d0){no ; here}
   else reset(d0);
until boItIs xor boItWas;
When you run this program, you should see the LED on d0 going on and off in response to the setting that you put on switch b0. Note that when using the simulator, there is a noticeable delay in the system's response. This would be imperceptible if you were using a real Pascalite device.

To achieve this program only a few new elements are involved:

Near the beginning, we have def_out(d0). This statement DEFines as OUTput pin d0. Broadly speaking, all of the pins (a0-a7, b0-b7 and d0-d7) CAN be used as input or output.... however they will normally be used for one or the other in any given application. The device sets them up to be used for input when it is switched on. This is because most external connections you might make will cause no problems when an output circuit, e.g. an LED and it's resistor, is connected to a pin which is set up for input. On the other hand, if an external circuit for input is connected to a pin that is running as an output problems can arise.

ANYWAY!: def_out changes the way the specified pin or pins is working. Once you have done def_out, some other words will have effects:

Set(d0) turns pin d0 "on", i.e. makes pin d0 high. See my guide to digital electronics if you're in a hurry to start making actual circuits, otherwise continue on with the simulation, which takes care of the messy bits for you!

Reset(d0) doesn't mean "set it again", which any reasonable person would think. Instead, it is used in an obscure manner, and means "turn d0 off", e.g. make the pin low.

Once you're sure you understand the program above, change the part that says "set(d0)" to "pulse(d0,80)". In this form, the program will repeatedly "wink" the LED on d0 briefly as long as b0 is switched to the "on" setting. In a real Pascalite, you wouldn't notice the "off" period of the LED... adding "delay(1000)" after "boItIs:=b1;" would cause the program to wait for a second between the 80ms winks of the LED. (Notice you just learned another of the built in words of the Pascalite package!!)

The following isn't very useful, but it is kind of pretty. We'll look at why it works in a minute.
program fifth3;
var bDisplay: byte;
{put delay(20); in here if you have a real Pascalite}
until 4=5;
On the def_out line, we've merely added more pins to the list of pins to be operating as outputs.

"Random" is a word which is built into Pascal. When we say bDisplay:=random we are telling the computer to store a number in the variable called bDisplay. It will be a number from 0 to 255, apparently drawn from a hat, though in fact if you drew many, many such numbers, eventually you would find the same sequence of numbers beginning again. Such sequences are called pseudo-random, but for most purposes they are as good as random numbers.

Write(portd,dDisplay) tells the computer to write whatever is in dDisplay onto pins d0 to d7. ("portd"= the pins p0,p1,p2...p7). How can you show a number on pins??? This goes back to the simple code mentioned back in the second tutorial. The number, lets say it's 131, gets imagined as if it were written out in binary. 131 would be 10000011. If it is a smaller number, say 12, then you still write 8 digits, even if that means writing some leading zeros thus: 00001100 (12, written in binary). I said "the number gets imagined written in binary". The idea that 10000011 and 131 are somehow different is wrong. "Two", "2", and "a pair" are not different, are they? there's just alternative ways of writing one thing. So when people say "What is 36 in binary?", I suppose they mean "How do you show 36, if you are writing it out as a binary number?" (The answer: 100110)

Once the number has been imagined written in binary form, you have only to imagine the pins (p0, p1, p2...) being set high when the corresponding 1 or 0 in the number is a 1, and set low when the 1 or 0 is a zero.

To be a full fledged programmer, you'll have to know more about ways of showing numbers, but that will do for now.

The program we just wrote gives a pretty result... but a pretty pointless one. We're going to write a better program, and learn another of Pascal's built in words at the same time. By the way... people usually call the "built in" words the reserved words. They are "reserved" in the sense that the people who put Pascal together said "We're reserving these words for the uses we are setting up for them, and you shouldn't use them for your own purposes."
program fifth4;
var bDisplay: byte;
for bDisplay:=0 to 15 do write(portd,bDisplay);
{put delay(20); in here if you have a real Pascalite}
until 4=5;
When you run the program, be sure to set the simulator to run fast.

We've got a simple instance of the very important
for <variable>:=<number>to<bigger number> do <some Pascal thing>
statement. The reserved words "for" and "to" and "do" are part of it.

The "for" statement, at it's simplest, as used here, is for counting. the pattern of lights you're seeing on the LED's show the binary for 0,1,2,3.... (If you don't mind waiting through the longer cycle, change the 15 to 255)

When we said "for bDisplay:=0 to 15 do write(portd,bDisplay);" what we caused was the equivalent of...


Some of the advantages of "for.. := .. to .. do" should already be obvious, and there are others. You will use "for" a lot.

Although this tutorial started with flashing LEDs, we're going to finish by going back to the LCD and seeing some more output.

In the current program change the numbers to so that the line says "for bDisplay:=65 to 75", and change the "portd", and change it to "lcd".

Run the program. You should see ABCD... on the LCD. (65 is the code for "A", 66 is the code for "B", etc)

Now... how can we clear the screen before each letter is displayed?

I hope you thought of....
for bDisplay:=65 to 75 do

That's nearly the answer. Problem is, the screen will clear 11 times, and then the letter 75 stands for will appear on the screen. "For" will only "do" the ONE THING that comes after it. Happily, there's a way to fool it. The following will work:
for bDisplay:=65 to 75 do
Adding "begin" and "end" either side of the two lines we had before makes them look like the "one" thing that should be DOne. This is another trick that you will use frequently. Remember "if... then... else..." Previously, we have always had just one thing after the then, one thing after the else. With "begin" and "end" we can make several things look like "one" thing, so the following would be okay....
if bDisplay=65
  then begin
         write(lcd,'bDisplay is holding 65');
         end  {no ; here}
  else begin
         write(lcd,'bDisplay is not holding 65');
Note that you DO need the semicolons BETWEEN the things "glued" together by the begin/ end pairs... it is just after the "else" that you must not put one in. I've written elsewhere an analysis of the theory behind this... just be happy with the rule, if you want the easy life!

Enough for now, I trust?

Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers. The Google advertisers pay me. I know nothing about them or their services, of course.)

Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!

Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....