Microprocessors In Engineering
Fall 2006
Introduction
There are microprocessors in use everywhere. This document was created using one, but the
following computers are also carried daily by the author: Cell Phone, Palm
Pilot, Wrist Watch, and a
I would ask that you begin your instruction in the digital world by reading another document that goes through how computers are built, and how they interact with the outside analog world. You will need to know the difference between an analog and a digital signal, and what the fundamental elements of computers are constructed from. This document is found at the URL:
http://www.engr.uidaho.edu/thompson/courses/ME330/lecture/DigitalInteraction.html
The following sections assume that you are familiar with the concepts outlined in that document.
Hardware
In this course, we will be working with the simplest and easiest to use microprocessors currently available: the PICaxe microprocessors[1]. The basis for this series of microprocessors (PIC) is manufactured by Microchip[2], but enhanced by a firmware software interpreter that turns them into the PICaxe processors. A program running on a PC compiles your application code that is written in a high level Basic language and then downloads the resulting meta-instructions over a serial communications line (SIO) into the PICaxe processor for execution. This is shown schematically in Figure 1.

Figure 1. Programming concept for the PICaxe microprocessor.
This powerful programming approach allows one to quickly become educated in the operation of the microprocessor, and move from simple to more complex measurement and control tasks. It is possible to read switch contacts and other digital input data, turn indicator lights on and off, measure analog voltages, make calculations, and send and receive serial communications data, using the simplest $3, 8-pin microprocessor to the more complex $10 part with 100 pins. To avoid the seeming overwhelming complexity of the larger microprocessors, we will use those with only a few pins. But, once you have mastered the basics, more complex chips simply add more capabilities.
What we will delve into here will be a simple approach to hardware measurements. The first step will be to assemble an 8-pin microprocessor to turn LED’s on/off and to read a switch to start and stop the program. We will be using a PICaxe Experimenter Board (Model AXE090), so the following instructions apply to this specific setup. The datasheet for this board may be found at:
http://www.rev-ed.co.uk/docs/AXE090.pdf
Take some time to study the layout of this board as it applies to the PICaxe 08M processor. There is a socket for that processor on the AXE090 board, and it is pre-wired for power, ground and serial communications.
The first experiments will use the PICaxe 08M. A detailed datasheet and manual (73pp) may be found at:
http://www.rev-ed.co.uk/docs/picaxe_manual1.pdf
Pay special attention to Appendix C on configuring the processors pins as inputs or outputs. A complete programming manual for all of the PICaxe processors may be found at:
http://www.rev-ed.co.uk/docs/picaxe_manual2.pdf
The hardware pinouts for the PICaxe 08M are as shown in Figure 2. The
difference between “legs” on the integrated chip and its logical “pins” is
explained in Section 1, Appendix C.

Figure 2. Pinouts
for the PICaxe 08M Microprocessor.
A typical configuration with two LED digital outputs,
a potentiometer analog input, and a push button digital input is shown in Figure
3.
As a practical issue, observe that the input from the switch is held to logic level “0” by the 10kΩ resistor as is the serial input line. This assures that the voltages on those pins do not “float” when no voltage is imposed on them. When the switch is depressed, the pin is immediately pulled to logic level “1”. The 330Ω resistors on the LED lines prevent too high a current from flowing through the LEDs. The potentiometer does nothing more than provide an analog voltage between 0 and 5v to the A/D converter ADC1 on pin 6.
As another practical matter, many of the processor’s pins are soft-programmable, and may take on one of several functions. All pins are assumed to be inputs when the microprocessor is powered up with the exception of the leg 7 (Out0) on the PICaxe08M. There are two ways to reconfigure the pinouts. The first is to explicitly state the directionality using the input and output commands. The second is to implicitly define it by the first use of a programming statement to read in a bit or write out a value. The use of a readadc command similarly defines a pin as an analog input.
Figure 3. AXE090
with 08M processor and test circuits.
This circuit will be the fundamental starting point for our first experiment with the microprocessor. It provides digital input and output signals and indicators, and an analog input. You should make, check, and test all connections before powering up the system for the first time. Please note that the connection lines in black are already pre-wired as part of the board, and you only have to add those shown in Blue.
Software
The PICaxe microprocessors use an extended form of BASIC, with special commands to read data in or write it out, to control timing, and more. Some of the basic language features are explained in the following sections.
First we will discuss how RAM memory is used to store temporary data in variables. All RAM data is lost when power is removed. We will categorize data into three types of variables- general purpose, storage, and special function. See the “let” command for more details about variable mathematics.
General Purpose Variables
There are 14 of these byte variables labeled b0 to b13. Each byte can have an integer value between 0 and 155. For variables requiring greater precision, one can use a 2-byte word variable which combines the byte variables as shown below:
w0 = b1:b0
w1 = b3:b2
w3 = b5:b4
:
w6 = b13:b12
The most significant byte of w3 is b5 and the least significant is b4. Similarly, the first two byte variables, b0 and b1, can be broken down as bit variables for use when you just need to store a single bit (0 or 1).
b0 = bit7:bit6:bit5:bit4:bit3:bit2:bit1
b1 = bit15:bit14:bit13:bit12:bit11:bit10:bit9:bit8
Storage Variables
For the PICaxe08M, it is possible to retain up to 48 variables in a temporary storage area (addresses 80 to 127) that can be accessed using the peek and poke instructions. This data cannot be used in calculations unless it is extracted into a general purpose variable.
Special Function Variables
For the PICaxe08M, these variables are
pins = the input or output port, broken down into bit variables pin0, pin1, etc.
dirs = data direction registers
infra = used by infrain instruction and is equal to b13.
Pins is a pseudo name for the input and output ports. When used on the left of the assignment,
let pins = %00001001
Will set outputs 4 and 0 high and the others low. When used on the right side, however,
let b3 = pins
will place the bit pattern on the input pins into variable b1.
Sometimes
it can be hard to remember which pins are connected to which devices. The ‘symbol’
command can then be used at the start of a program to rename the inputs and outputs.
Note this program assumes connection of an external buzzer to output pin 1.
symbol red = 7 ;
rename output7 ‘red’
symbol buzzer = 1 ;
rename output1 ‘buzzer’
main: ;
make a label called .main.
high red ;
LED on
low buzzer ;
buzzer off
wait 1 ;
wait 1 second
low red ;
LED off
high buzzer ;
buzzer on
pause 1000 ;
wait 1 second using pause command
goto main ; jump back to
the start
Remember that comments (an explanation after the semicolon “;” symbol) can make each line of a program much easier to understand. These comments are ignored by the computer when it downloads a program to the PICAXE.
A
label (e.g. main: in the program above) can be any word (apart from keywords
such as ‘switch’), but must begin with a letter. When the label is first
defined it must end with a colon (:). The colon ‘tells’ the computer that the
word is a new label.
This
program uses the wait command. The commands wait and pause both create time delays.
However wait can only be used with whole seconds, while pause can be used for
shorter time delays (measured in milliseconds (1000th of a second)). Wait can be followed by a number between 1
and 65. Pause can be followed by a number
between 1 and 65535.
It is
also a good programming technique to use tabs (or spaces) at the start of lines
without labels so that all the commands are neatly aligned. The term “white-space” is
used
by programmers to define tabs, spaces and blank lines, and the correct use of
white-space can make the program listing much easier to read and understand.
This
program below shows how to react to digital (on or off) signals such as switch Closures.
In this program output pin 7 flashes
every time the push switch on input pin 6 is pushed.
main: ;
make a label called .main.
if input6 =1 then flash ; jump if the input is on
goto main ; else loop
back around
flash: ;
make a label called .flash.
high 7 ;
switch output 7 on
pause 2000 ;
wait 2 seconds
low 7 ;
switch output 7 off
goto main ; jump back to
start
In
this program the first three lines make up a continuous loop. If the input is
off the program just loops around time and time again. If the switch is then pushed the program
jumps to the label called “flash”. The program then flashes output 7 on for two
seconds before returning to the main loop.
Note
carefully the spelling in the if…then line – input6 is all one word (without a space).
Note also that only the label is placed
after the command then – no other words apart from a label are allowed.
The
value of an analogue input can be easily copied into a variable by use of the ‘readadc’ command. The variable value (0 to 255) can then be
tested. The following program switches on one LED if the value is greater than
120 and a different LED if the value is less than 70. If the value is between
70 and 120 both LEDS are switched off.
main: ;
make a label called .main
readadc 2,b0 ; read channel 2
into variable b0
if b0 > 120 then top ; if b0 > 120 then do top
if b0 < 70 then bot ; if b0 < 70 then do bot
low 1 ;
else switch off 1
low 2 ;
and switch off 2
goto main ; jump back to
the start
top: ;
make a label
high 1 ;
switch on 1
low 2 ;
switch off 2
goto main ; jump back to
start
bot: ; make a label
high 2 ;
switch on 2
low 1 ;
switch off 1
goto main ; jump back to
start
When
using analogue sensors it is often necessary to calculate the ‘threshold’ value
necessary for the program (ie the values 120 and 70
in the program above). The debug command provides an easy way to see the
‘real-time’ value of a sensor, so that the threshold value can be calculated by
experimentation.
main: ;
make a label called main
readadc 2,b0 ; read channel 2
into variable b0
debug b0 ;
transmit value to computer screen
pause 100 ;
short delay
goto main ; jump back to
the start
After
this program is run a ‘debug’ window showing the value of variable b0 will appear
on the computer screen. As the sensor is experimented with the variable value will
show the current sensor reading.
A
sub-procedure is a separate ‘mini-program’ that can be called from the main
program. Once the sub-procedure has been
carried out the main program continues. Sub-procedures
are often used to separate the program into small sections to make it easier to
understand. Sub-procedures that complete
common tasks can also be copied from program to program to save time. The following program uses a sub-procedure to
do a recurring task, passing information along in variable b2.
symbol dp = 7 ;
rename output7 .dp.
symbol counter = b0 ;
define a counter using variable b0
main: ; make a label called .main.
let
b2 = 5 ;
first flash count
gosub flash ; call the sub-procedure flash
pause 500 ; wait a while
let b2=15 ;set
second flash count
goto main ; loop back
end ;
end of the main program
flash: ;
make a sub-procedure called flash
for counter = 1 to b2 ;
start a for.next loop
high dp ; LED on
pause 250 ;
wait 0.25 second
low dp ; LED off
pause 250 ;
wait 0.25 second
next counter ;
next loop
return ;
return from the sub-procedure
When transmitting information back to the terminal program, you must use the serout command which uses communication settings of 2400 baud, no parity, 8 data bits, and 1 stop bit and pin0 (leg 7) to match the default PICaxe08M serial data configuration. An example program to take 10-bit data and transfer the results back to the terminal window is shown. The symbol declarations will help you to understand this program.
‘ ADC Test Program by D.E. Thompson 10/06/2006
‘
symbol Tab = 9 ; ASCII line feed
symbol lf = 10 ; ASCII line feed
symbol cr = 13 ; ASCII carriage return
symbol sw1 = pin3 ;
define leg 4 (pin 3) as switch
main:
if sw1 = 0 then main ; loop until started with switch
serout 0,N4800,(”Data”,13,10) ; output header
for b0 = 0 to 20 ; start a loop counter
readadc10 1, w4 ; read word into b9:b8
pause 500 ; 0.5 seconds between samples
serout 0,n2400,(#b9, 9, #b8, 13,10) ; write out data
next b0 ; end of loop
end ; end of main program
After this program is started, one must activate the terminal window using the menu
PICAXE -> Terminal…
(or F8) to catch the data. After the program is completed, get the data using the terminal window menus:
EDIT->Copy Input Buffer
Then you may open Excel and
paste this data into the worksheet.
Testing and Running
the Software and Hardware
Now that you have an understanding of the elements of programming, the next step is to test the board to assure that the microprocessor is functioning properly. Using the Programming Editor, enter the test program shown in Table 1 and save it as test1.bas. Note that the program has two labels, “main” and “done” which are defined by the “:” that follows them.
Table 1. Simple test program 1.
main:
high 2
pause 2000
low 2
high 4
pause 2000
low 4
if pin3 = 1 then done
goto main
done:
end
Then do the following:
The program should start running automatically on the PICaxe 08M.
Table 2. Annotated version of test program 1.
symbol red = 2 ; define output 4 (pin 3) as red
symbol green = 4 ; define output 5 (pin x) as green
main: ; label for start of main loop
high red ; set red LED to high (5v)
; Also defines 4 as an output
pause 2000 ; wait for 2000 milliseconds
low red ; turn off red LED (0v)
if pin3 = 1 then done ; if switch depressed jump to done
high green ; turn green LED on
pause 2000 ; wait for 2000 milliseconds
low green ; turn green LED off
if pin3 = 1 then done ; if switch depressed jump to done
goto main ; otherwise, jump to main
done: ; label for done
end ; stop program
At this time, the LED’s should begin to alternately flash on and off every two seconds. This will continue until the power is removed, or Switch 1 is depressed and held for up to 2 seconds. To help understand the program, it is possible to annotate the program and add helpful information as shown in Table 2. Because the BASIC compiler only downloads the final code into the microprocessor, these comments do not cost precious program space on the PICaxe08M. The total 256 bytes memory space for storing programs and data is shared on the little PICaxe08M. Longer programs result in smaller data space.
You may at any time modify the program using the editor and download the new program to test it.
You are now able to do anything within the capabilities of the PICaxe08M. This will include taking data using the on-board 8 or 10-bit ADC and transmitting data to the serial port. You can run timing programs, control events, turn motors on and off, record temperatures, and more.
[1] PICAXE® products are developed and
distributed by Revolution Education Ltd, 4 Old Dairy Business Centre,
[2] Microchip Technology Inc.,