2003_04_supp(2).pdf

(912 KB) Pobierz
T
N
EE ME
FR LE
PP
SU
EPE PIC
TUTORIAL V2
JOHN BECKER
PART ONE
WHAT IT’S ALL ABOUT
T
the time that the original
EPE PIC Tutorial
was published in March to
May 1998, letters and phone calls to
EPE
had been showing that interest
in Microchip’s PIC microcontrollers had become intense.
Many readers were asking for more information on how to use
these devices in designs of their own invention.
In the words of one reader, “I find the PIC data sheets
too skimping on everyday detail, and the published
software too complex. Please show me how to get to
grips with the
essence
of PICs. Tell me, step-by-
step, how to get started with writing simple
programs, how to just turn on a single light
emitting diode, for example. Then take me
forward from there.”
It was to meet this demand that the original
EPE PIC Tutorial
was published. Its success
resulted in a CD-ROM version being produced
commercially as
PICtutor,
complete with its own
ready-built development board. Recently that
version was upgraded to become
Assembly for
PICmicro V2
along with its Version 2 PICmicro MCU
board (see elsewhere in
EPE
for details). Many thousands
of people have learned to program PICs through these
several versions.
However, we frequently receive requests from readers for PIC
programming education that is available at minimal cost. The
aim of this three-part
EPE PIC Tutorial V2
series is to
do just that, as detailed more fully on the next
two pages. In a nutshell, though, it gets
you inexpensively started with
PICs, and does so
“step-by-step”.
Quite
simply
the easiest
low-cost
way to learn
about using
A
PIC
Microcontrollers!
EPE PIC
TUTORIAL V2
01202 873872
PIC TOOLKIT TK3
AND ITS VERSATILE
BOARD SHOW YOU HOW!
Everyday Practical Electronics, April 2003
PIC Tutorial V2 Supplement
1
THIS REVISION
Over five years on from the publication
of the original
EPE PIC Tutorial,
a number
of things have changed, yet at the same
time the basics of programming PIC
microcontrollers have not.
This revision is thus a mixture of the old
and the new. The old aspect is that the com-
mands used to program PICs remain the
same. The new aspects, though, are several:
The original
EPE PIC Tutorial
illustrat-
ed its example programs in a programming
dialect known as TASM. This dialect is
usable with a variety of tables whose con-
tents can be modified to suit many types of
microcontroller and microprocessor. It had
been modified to suit PICs by reader
Darren Crome.
This revision now has its programming
examples written in Microchip’s own PIC
programming dialect, MPASM. This dialect
is the “industry standard” and thus has far
wider appeal than TASM, although the basic
differences between the two are slight.
Secondly, the original
EPE PIC Tutorial
concentrated on the now-obsolete
PIC16C84 as being the target device. This
microcontroller effectively became replaced
in 1997 by the pin-for-pin compatible
PIC16F84, which is an equally excellent
device to use to illustrate PIC programming
techniques. More recently the PIC16F84A
has arrived on the scene. The PIC16F84 and
PIC16F84A (two of the devices in the
PIC16F8x family) can be used interchange-
ably in this
EPE PIC Tutorial V2
(referred
to from now on as the
Tutorial).
RESOURCES
A special composite
EPE PIC
Resources CD-ROM
has been produced
to accompany this series. It includes the
Tutorial
software for the entire series,
EPE Toolkit TK3
software, complete
reproductions of the
TK3
texts of
Oct/Nov ’01,
Using TK3 with Windows
XP and 2000
(Oct ’02) and a broad selec-
tion of PIC-related articles published in
EPE
over the last several years and
which illustrate practical examples of
various advanced programming func-
tions and techniques. Some unpublished
articles are included as well. The full list
is given elsewhere in
EPE.
Alternatively, software for
TK3
and
this
Tutorial
can be downloaded
free
from
the
EPE
FTP Site. Disks for both sets of
software (CD-ROM for
TK3,
and 3.5in
disk for this
Tutorial)
are also available
are included on the
EPE PIC Resources
CD-ROM.
From hereon the software and
p.c.b. are jointly referred to simply as
TK3.
It should be noted that
TK3
was written
to run under Windows 95, 98 and ME. To
run it under Windows NT, XP and 2000 the
software must be used as described in
Mark Jones’ article
Using TK3 with
Windows XP and 2000
of Oct ’02. This
article is also carried on the
EPE PIC
Resources CD-ROM.
In keeping with the original
Tutorial,
we
assume in this series that you have no pre-
vious knowledge of PICs and their pro-
gramming. We thus start as we did before,
by explaining the basic nature of a PIC
microcontroller.
from the
EPE
Editorial Office. Back
issues (or photocopies) or Back Issue
CD-ROMs of published texts can also be
purchased (see the
Back Issues
page).
The
EPE
FTP Site is most easily
accessed via the main page at
www.epemag.wimborne.co.uk.
Click
on the
FTP Site (Downloads)
option at
the top, then click down the paths
pub/PICS
then select
PIC Tutorial V2
or
Toolkit TK3.
The printed circuit board for
TK3
is
available from the
EPE PCB Service,
code 319. Note, you will require the
relevant back issues or the
EPE PIC
Resources CD-ROM
to be able too build
this.
See the
EPE PCB Service
page for the
price and ordering details of the disks
and p.c.b.
RA2
RA3
RA4/TOCKI
MCLR
GND
RB0/INT
RB1
RB2
RB3
1
2
3
4
5
6
7
8
9
18
17
16
15
14
13
12
11
10
RA1
RA0
OSC1/CLK IN
OSC2/CLK OUT
+
VE
RB7
RB6
RB5
RB4
FAMILY MATTERS
Once you know how to program a
PIC16F84 you are well equipped to write
programs for other PICs, although there are
some minor differences in the way that the
various PIC families handle some of the
same functions.
Apart from the PIC16F8x family, two
other PIC families are eminently suited to
hobbyist constructors, notably the
PIC16F87x and PIC16F62x families
(although they are not immediately suited
to this
Tutorial).
However, in the final part
of this three part series, basic differences
between the way that the PIC16F8x,
PIC16F87x and PIC16F62x families do the
same thing are highlighted and the Tutorial
programs can be readily modified to run on
these devices. Examples of some useful
routines specific to the PIC16F87x family
are included.
It is stressed, though that this
Tutorial
does not attempt to be a full tutorial on
every aspect of the three families. Nor does
it examine specific aspects of some other
PIC families whose functions are more
advanced than most readers probably
require.
Also, the
Tutorial
does not teach the use
of Microchip’s MPASM and MPLAB pro-
gramming software, and it does not cover
any PIC variant or dialect that is pro-
grammed in versions of BASIC.
An important aspect of this revision is
that it has been designed for use with the
EPE PIC Toolkit TK3
printed circuit board
and software (published Oct/Nov 2001),
both of which, plus their two texts, you
need in order to get full benefit from this
Tutorial.
See the Resources panel for
details of obtaining them. Note that these
WHAT IS A PIC?
Fig.1. Pinouts for the PIC16F8x family.
by Microchip. When asked about the name’s
origin, Microchip’s Technical Department
A PIC chip, in this context, is a micro-
controller integrated circuit manufactured
BASIC PIC16F84 SPECIFICATIONS
35 single-word commands (see
Table 1)
1K × 14-bit EEPROM program
memory
68 × 8-bit general purpose SRAM
registers
15 special function hardware registers
(see Table 2 later)
64 × 8-bit EEPROM data memory
1000 program memory erase/write
cycles (typical)
10,000,000 data memory erase/write
cycles (typical)
Data retention >40 years
5 data input/output pins, Port A
8 data input/output pins, Port B
25mA current sink maximum per pin
20mA current source maximum per
pin
80mA maximum current sunk by
Port A
50mA maximum current sourced by
Port A
150mA maximum current sunk by
Port B
100mA maximum current sourced by
Port B
Total power dissipation 800mW
8-bit timer/counter with 8-bit
prescaler
Power-on reset (POR)
Power-up timer (PWRT)
Oscillator start-up timer (OST)
Watchdog timer (WDT) with own
on-chip RC oscillator
Power saving Sleep function
Code protection
Serial in-system programming
Selectable oscillator options:
RC: low cost RC oscillator
XT: standard crystal/resonator
(100kHz to 4MHz)
HS: high speed crystal/resonator
(4MHz to 10MHz) (to 20MHz for
’F84A)
LP: power-saving low frequency
crystal (32kHz to 200kHz)
Interrupts:
External, RB0/INT pin
TMR0 timer overflow
Port B RB4 to RB7 interrupt on
change
Data EEPROM write complete
Operating voltage range: 2·0V to
6·0V (to 5·5V for ’F84A)
Power consumption:
<2mA @ 5V, 4MHz
15mA typical @ 2V, 32kHz
<1mA typical standby @ 2V
2
– PIC Tutorial V2 Supplement
Everyday Practical Electronics, April 2003
TUTORIAL CONCEPTS EXAMINED
TUTORIAL 1:
Minimum commands needed
Port default values
Instruction ORG
Instruction END
Command GOTO
Program TK3TUT1.ASM
TUTORIAL 2:
Clock cycles
File registers
Bits
Bytes
Set
Clear
Command CLRF
Command CLRW
Command BSF
Command BCF
Ports and Port directions
Register STATUS
STATUS register bit 5
Banks 0 and 1
Program TK3TUT2.ASM
TUTORIAL 3:
Names in place of numbers
Case sensitivity
Labels
Repetitive loop
Instruction EQU
Program TK3TUT3.ASM
TUTORIAL 4:
Command MOVLW
Command MOVWF
Command RLF
Command RRF
Command BTFSS
Command BTFSC
Instruction #DEFINE
Instruction BANK0
Instruction BANK1
Register PORTA
Register TRISA
Register PORTB
Register TRISB
Register PCL
Naming numbers
Bit naming
Program counter
STATUS register bit 0
Carry flag
Bit codes C, F, W
Bit testing
Conditional loop
Pin protection
Program TK3TUT4.ASM
Program TK3TUT5.ASM
TUTORIAL 5:
STATUS bit 2
Zero flag
Bit code Z
Command MOVF
Program TK3TUT6.ASM
TUTORIAL 6:
Command INCF
Command DECF
Command INCFSZ
Command DECFSZ
Counting upwards
(incrementing)
Counting downwards
(decrementing)
Use of a file as a counter
Program TK3TUT7.ASM
TUTORIAL 7:
Switch monitoring
Command ANDLW
Command ANDWF
Command ADDWF
Command ADDLW
Nibbles
STATUS bit 1
Digit Carry flag
Bit code DC
Program TK3TUT8.ASM
TUTORIAL 8:
Increasing speed of
TK3TUT8
Bit testing for switch status
Program TK3TUT9.ASM
TUTORIAL 9:
Responding to a switch press
only at the moment of
pressing
Program TK3TUT10.ASM
TUTORIAL 10:
Performing functions
dependent upon which switch
is pressed
Use of a common routine
serving two functions
Program TK3TUT11.ASM
TUTORIAL 11:
Reflecting PORTA’s switches
on PORTB’s l.e.d.s
Command COMF
Command SWAPF
Inverting a byte’s bit logic
Swapping a byte’s nibbles
Program TK3TUT12.ASM
Program TK3TUT13.ASM
Program TK3TUT14.ASM
TUTORIAL 12:
Generating an output
frequency in response to a
switch press
The use of two port bits set
to different input/output
modes
Command NOP
Program TK3TUT15.ASM
TUTORIAL 13:
Command CALL
Command RETURN
Command RETLW
Program TK3TUT16.ASM
TUTORIAL 14:
Tables
Register PCL (again)
Register PCLATH
Program TK3TUT17.ASM
TUTORIAL 15:
Using four switches to create
four different notes
Use of a table to selectively
route program flow
Program TK3TUT18.ASM
TUTORIAL 16:
Indirect addressing
Using unnamed file
locations
Register FSR
Register INDF
Program TK3TUT19.ASM
TUTORIAL 17:
Tone modulation
Command XORLW
Command XORWF
Command IORLW
Command IORWF
Program TK3TUT20.ASM
Program TK3TUT21.ASM
Program TK3TUT22.ASM
TUTORIAL 18:
Register OPTION
Register INTCON
Register TMR0
Use of internal timer
Program TK3TUT23.ASM
TUTORIAL 19:
BCD (Binary Coded
Decimal) counting
Program TK3TUT24.ASM
TUTORIAL 20:
Real-time timing at 1/25th
second
Counting seconds 0 to 60
Program TK3TUT25.ASM
TUTORIAL 21:
Using 7-segment l.e.d.
displays
Showing hours, minutes and
seconds
Command IORLW (usage)
Program TK3TUT26.ASM
Program TK3TUT27.ASM
Program TK3TUT28.ASM
TUTORIAL 22:
Using intelligent l.c.d.s
Setting l.c.d. contrast
Initialising the l.c.d.
Sending a message to the
l.c.d.
Program TK3TUT29.ASM
TUTORIAL 23:
Coding hours, minutes and
seconds for an l.c.d.
Shortened clock monitoring
code
Command SUBLW
Command SUBWF
Program TK3TUT30.ASM
TUTORIAL 24:
Adding time-setting switches
Program TK3TUT31.ASM
TUTORIAL 25:
Writing and reading
EEPROM file data
Register EECON1
Register EECON2
Register EEDATA
Register EEADR
Program TK3TUT32.ASM
TUTORIAL 26:
Illustrating use of EEPROM
data read/write
Converting binary value to
hexadecimal
Program TK3TUT33.ASM
TUTORIAL 27:
Interrupts
Command RETFIE
Program TK3TUT34.ASM
Program TK3TUT35.ASM
TUTORIAL 28:
Command SLEEP
Program TK3TUT36.ASM
TUTORIAL 29:
Watchdog timer (WDT)
Command CLRWDT
Program TK3TUT37.ASM
TUTORIAL 30:
Misc Special Register bits
TUTORIAL 31:
INCLUDE files command
Embedded configuration data
Embedded Data EEPROM values
Embedded PIC type data
Embedded Radix
Program TK3TUT38.ASM
TUTORIAL 32:
PIC16F8x, PIC16F87x,
PIC16F62x family coding
differences
PIC16F87x PORTA
PIC16F87x Data EEPROM use
PIC16F62x PORTA
PIC16F62x Data EEPROM use
Program TK3TUT39.ASM
Program TK3TUT40.ASM
TUTORIAL 33:
Converting binary values to
decimal
Program TK3TUT41.ASM
TUTORIAL 34:
Multiplication routine
Program TK3TUT42.ASM
TUTORIAL 35:
Division routine
Program TK3TUT43.ASM
TUTORIAL 36:
ADC conversion routine for
PIC16F87x family
Program TK3TUT44.ASM
TUTORIAL 37:
CBLOCK command
Interfacing to external serial
EEPROM chips, for PIC16F87x
family
Program TK3TUT45.ASM
TUTORIAL 38:
Outputting serial data at a
specified BAUD rate, for
PIC16F87x family
Program TK3TUT46.ASM
TUTORIAL 39:
Practical example recording
analogue data to serial
EEPROM and subsequent
outputting as RS-232 serial data
Program TK3TUT47.ASM
TUTORIAL 40:
Programming
PICs vs. hardware
Summing-up
APPENDIX A:
Bugged Teaser!
APPENDIX B:
Useful PIC information
Further reading
Everyday Practical Electronics, April 2003
PIC Tutorial V2 Supplement
3
replied,
“PIC is not an acronym; it is just a
trademarked
name
that
General
Instruments came up with a long time ago”.
(GI were the originators of PICs.)
A microcontroller is similar to a micro-
processor but it additionally contains its
own program command code memory, data
storage memory, bi-directional (input/out-
put) ports and a clock oscillator. Many
microprocessors require the use of addi-
tional chips to provide these requirements;
microcontrollers are totally self-contained.
The great advantage of microcontrollers is
that they can be programmed to perform
many functions for which many other chips
would normally be required. This not only
makes for simplicity in electronic designs,
but also allows some functions to be per-
formed which could not be done using nor-
mal digital logic chips – i.e. circuits for
which, previously, a microprocessor and
peripheral devices would have been required.
PICs are manufactured and supplied
“empty”. That is, they are without program
codes (commands) and cannot control a
circuit until they have been provided with a
program that tells them what to do. It is the
task of the program writer (you) to tell
them what that is.
The commands are written in a spe-
cialised form of English, largely consisting
of mnemonics, known as the “source
code”. An assembly program (such as
TK3)
then translates (assembles) the source code
commands into a numerical form that the
PIC can understand, the “program code”.
This code, which is normally in hexadeci-
mal, is then sent (loaded) in binary format
to the PIC by electronic hardware, such as
TK3’s
p.c.b.
TABLE 1. PIC COMMAND CODES FOR PIC16F8x, PIC16F87x
AND PIC16F62x
Command/Syntax
Flags
affected
Cycles
Description
Tutorial
discussed
7
7
2
2
11
6
6
6
6
17
5
4
12
4
4
23
11
17
2
2
4
4
7
7
13
29
1
17, 21
4
27
13
13
28
23
17
BYTE-ORIENTATED FILE REGISTER OPERATIONS
ADDWF
f,d
C, DC, Z
1
Add W and f
ANDWF
f,d
Z
1
AND W with f
CLRF
f
Z
1
Clear f
CLRW
Z
1
Clear W
COMF
f,d
Z
1
Complement f
DECF
f,d
Z
1
Decrement f
DECFSZ
f,d
1 (2)
Decrement f, skip if 0
INCF
f,d
Z
1
Increment f
INCFSZ
f,d
1 (2)
Increment f, skip if 0
IORWF
f,d
Z
1
Inclusive OR W with f
MOVF
f,d
Z
1
Move f
MOVWF
f
1
Move W to f
NOP
1
No operation
RLF
f,d
C
1
Rotate left f through Carry
RRF
f,d
C
1
Rotate right f through Carry
SUBWF
f,d
C, DC, Z
1
Subtract W from f
SWAPF
f,d
1
Swap nibbles in f
XORWF
f,d
Z
1
Exclusive OR W with f
BIT-ORIENTATED REGISTER OPERATIONS
BCF
f,b
1
BSF
f,b
1
BTFSC
f,b
1 (2)
BTFSS
f,b
1 (2)
LITERAL AND
ADDLW
ANDLW
CALL
CLRWDT
GOTO
IORLW
MOVLW
RETFIE
RETLW
RETURN
SLEEP
SUBLW
XORLW
CONTROL OPERATIONS
k
C, DC, Z
1
k
Z
1
k
2
TO, PD
1
k
2
k
Z
1
k
1
2
k
2
2
TO, PD
1
k
C, DC, Z
1
k
Z
1
Bit
Bit
Bit
Bit
clear f
set f
test f, skip if 0
test f, skip if 1
PIC VARIETIES
There are many families of PIC micro-
controller available, ranging from those
which can only be programmed once, to
those that can be repeatedly repro-
grammed. The former are typically known
as One Time Programmable (OTP)
devices, and because of this characteristic
are not well-suited to hobbyist use since
they cannot have their software code
changed once they have been programmed.
There are two basic families of repro-
grammable PICs: those that require an
ultra-violet light unit to erase their previous
data before being reprogrammed, but
which are now essentially obsolete, and
those which are electrically erasable.
In the latter category fall the three device
families already mentioned, of which it is
the PIC16F84 device we use here. It has
been chosen because of its ease of repro-
gramming and because it does not have
additional features that can prove difficult
to understand for beginners. Its pinouts are
shown in Fig.1, and its basic attributes
given in the Specifications panel.
It is an EEPROM (electrically erasable
programmable read only memory) device,
also known as a “Flash” device, hence the
“F” infix in its type number. This means
that it can be rapidly reprogrammed as
often as you wish, without the need for
ultra-violet erasing.
Add literal and W
AND literal with W
Call subroutine
Clear Watchdog Timer
Go to address
Inclusive OR literal with W
Move literal to W
Return from interrupt
Return with literal in W
Return from subroutine
Go into standby mode
Subtract W from literal
Exclusive OR literal with W
chip can be used: 4MHz, 10MHz and
20MHz respectively. You may use any
device speed rating for this
Tutorial,
although the -04 is likely to be stocked by
more component suppliers.
The PIC16F84 used here has two
input/output (I/O) ports, Port A and Port B.
Port A has five pins (RA0 to RA4), and
Port B has eight pins (RB0 to RB7). We
shall be using the PIC in two of its four
oscillator modes, RC (resistor/capacitor)
and XT (standard crystal up to 4MHz), the
former being variable, the latter using a
3·2768MHz crystal.
To re-emphasise an earlier point, much
of the information about the commands
which we present here is, in most
instances, applicable to other members of
the PIC family. Once you understand a
PIC16F84 you should have no difficulty
applying your knowledge to other PICs.
*
suitable power supply (TK3 runs from
9V d.c., which can be supplied via a
plug-in mains adaptor)
*
word-processing program (text editor)
*
assembly and send (download) software
program (e.g.
TK3)
It is worth noting that this
Tutorial
and
the
TK3
software can be used with
Magenta’s version of the
TK3
board, and
with the commercial Version 2 PICmicro
MCU Development Board. However, these
two boards do not have the numbered CP
connection points referred to in this text
regarding the
EPE TK3
board, but they do
have pin function notations and the con-
nections should be obvious.
Data is output from the computer to the
PIC via the parallel printer port (addresses
378h, 278h and 3BCh are supported by
TK3).
It is output serially, data on port line
D0, and a clock signal on line D1.
Additional computer printer port lines are
used by
TK3
to enable such functions as
reading back program code from a PIC.
You must be able to use a word-process-
ing program. This must produce a text file
that is totally without formatting and printer
commands. That is, it must be able to gen-
erate a pure ASCII text file (and to input
one). It is stressed that the source code
(.ASM) files
must
be in pure ASCII text
formats without printer or display format
WHAT YOU NEED
There are six things that you need in
order to program a PIC:
*
PC-compatible computer having a stan-
dard (Centronics-compatible) parallel
printer port (USB ports are not suitable)
*
purpose built programming hardware
board (e.g.
TK3)
*
standard (Centronics) parallel printer
port connecting cable
SUB-VERSIONS
Note that there are several sub-versions
of individual PIC types, having suffixes
such as -04, -10 and -20. The suffix indi-
cates the maximum clock rate at which the
4
– PIC Tutorial V2 Supplement
Everyday Practical Electronics, April 2003
commands embedded in them. (TK3 offers a
choice of editors, including DOS Edit,
Windows Notepad and Windows Wordpad.)
ADDITIONAL
COMPONENTS
To use this
Tutorial
with the basic
TK3
p.c.b. you need the following additional
components:
330W resistor, 0·25W 5% carbon film (8
off)
1mF capacitor, axial elect. 10V
2-line, 16-characters per line alphanu-
meric liquid crystal display module and
datasheet
Optional: 4-digit multiplexed common
cathode 7-segment l.e.d. display module
and datasheet
Personal (high-impedance) headphones
Jack socket to suit headphones
Extra push-to-make switch for connec-
tion via flying leads
Stranded connecting wire
Solder
ANOTHER NEED
Throughout this
Tutorial
we shall exam-
ine in a fair amount of detail the 35 basic
PIC commands. It is hoped that this will
give you all the necessary information that
will enable you to conceive a design in
which you can use a PIC16F84 to control
whatever situation you wish, and to write
the code that will let it do so.
There is, though, much more to writing
programs than you may at this moment
fully appreciate. Knowledge about individ-
ual commands and the way in which they
can be used is not enough in itself.
Programming is a way of looking at the
world that other people may not recognise.
You must have the mental ability to see
each programming situation as a step by
step function, visualising and analysing in
your mind exactly how it is that you need
to specify the complete program flow.
You have to write the sequence of events
with the correct grammar, with the correct
spelling and in the correct order.
Undoubtedly you will make mistakes
while you are writing the code, failing to
see the correct sequence of events and
using incorrect command structures.
You require the ability to analyse what
you have done wrong and to correct it. You
are likely to be confronted with an overall
task that may, on occasion, take you into
several days or even weeks of dedicated
concentration.
Readers have occasionally asked how
they can be taught to think like a program-
mer. There is no easy way in which this can
be taught. Some people have the ability,
some do not. The best way to learn is by
actually writing snippets of code and get
those to work, giving you the experience
and confidence to progress to more com-
plex situations. Throughout this
Tutorial
we try to encourage you in this approach.
Programming, to those who have the abil-
ity to see things “as they are” and not “how
they seem to be”, can become extremely
addictive. You could find yourself com-
pelled to get back to the keyboard and PIC
programmer at any conceivable hour. You
had better have an understanding family!
board mentioned earlier plus the few extra
components just listed.
TK3
already has
eight light emitting diodes (l.e.d.s), four
pushbutton switches and four uncommitted
(open-collector)
npn
transistors.
These facilities help to illustrate the pro-
gram examples discussed in the text and
encourage you to build up your experience
of how the PIC16F84 can be made to
respond to different practical situations.
In this text, we start at the very begin-
ning of programming. The first lesson is
about the very minimum of information
that needs to be written into a program list-
ing before the PIC can do anything else.
We then, “step by step”, take a specific
very simple task, such as turning on an
l.e.d., and describe in detail each of the
commands that are required to do the task.
Having described one task, we then take
that idea a stage further, adding a few more
commands that will enhance the capabilities
of the program. Each of these commands is
similarly discussed in detail. Thus we
progress, taking simple ideas, and illustrating
how they can be achieved and then enhanced.
We feel that this approach is far more
useful than describing each and every one
of the commands in turn before we ever get
to use them. Most people learn by doing,
reading about things in short sections and
applying the knowledge in practical bite-
sized chunks. A complete chapter on all the
commands in sequence would be too much
to remember and understand in one go.
The complete list of Microchip’s com-
mands for the PIC16F8x, PIC16F87x and
PIC16F62x families is shown in Table 1. All
are discussed and demonstrated. In the early
years of PICs there used to be two others,
OPTION and TRIS, but Microchip have
dropped them from their recent PIC families.
As you will see, there are a lot of “bit-
orientated” sub-commands available as
well as the main commands. Some of these
are similar in operation and close examina-
tion will be given only to the principal ones
– once you understand these, use of the
similar bit-setting commands available will
become obvious.
types of microprocessor or microcon-
troller, will find that once a few commands
have had their functions explained, using
them will rapidly become instinctive. The
author, having many years of such experi-
ence, effectively learned about PICs and
how to use them over a single weekend,
just by doing a bit of experimenting.
Other readers without such experience
will, it has to be said, have to become
accustomed to understanding program-
ming itself as a step by step process. An
analytical mind is required and, as said ear-
lier, there is no easy way in which pro-
gramming can be taught to those who lack
this ability.
TUTORIAL EXERCISES
TUTORIAL SECTIONS
FIRST THINGS FIRST
To get you started programming PICs
through this
Tutorial,
you need the
TK3
The
Tutorial
is split into numbered sec-
tions. Each deals with specific coding top-
ics but, in most cases, is a direct follow-on
from the previous one, and is nearly always
visually illustrated by the displays on the
TK3
p.c.b. The exception to the latter is
when sound is used as the output medium,
when the personal headphones are needed.
At the end of most sections there are a
few simple exercises which allow you to
experiment with the program presented in
that section. You will only be expected to
use the commands that have already been
introduced to you. None of them should tax
your brain too much, but they will, hope-
fully, encourage you to think of alternative
ways in which the same basic task can be
tackled, or to consider other tasks that can
be achieved using similar techniques.
By the end of the complete
Tutorial,
you
will know how to get the PIC16F84 to
respond to switches and other external sig-
nal sources, send data to various types of
display, to create sound, to be the heart of a
24-hour clock and to store data in its non-
volatile EEPROM memory.
Readers who have had experience of
programming in BASIC, or with other
Throughout this
Tutorial
we present var-
ious programming exercises at which to try
your hand. In most cases, you are request-
ed to modify an existing tutorial program.
This requires it to be saved, assembled and
sent to the PIC.
It is important that when you make these
changes, you do not save the variant under
the same name as the original. It must have
a different name. If you
do
save under the
same name, the original file will be
replaced by the new one. You can save each
program variant by any name of your
choosing, but use the same .ASM exten-
sion as the examples use. You might con-
sider using TRY1.ASM, TRY2.ASM, etc.
It is recommended that you save the
original file under the new name before
you make any changes, to avoid curse-wor-
thy errors! But if you do make a mistake
and overwrite something you should not,
the original can be recopied from its disk or
FTP site download.
Having saved your changed file, you
now assemble and send it to the PIC.
We shall
not
be giving possible solutions
(of which there could be several) to any of
the exercise questions. It is expected that
you will persevere until you find a work-
able solution. Only in this way will you get
into the habit of being presented with a
computing problem, which you have to
solve on your own, and then solving it.
This last statement was made in the orig-
inal
Tutorial
text but we still periodically
get asked what the solutions are. We are
hard-hearted on this point and don’t offer
solutions! If you want to become a pro-
grammer, you’ve got to get your brain
thinking like one. The exercises are all sim-
ple and have simple solutions, we would
not be doing you any favour by telling you
some answers.
We are now almost in a position to start
telling you about writing programs for a
PIC16F84 and for you to start getting your
brain into gear – it’s really all very logical!
From hereon you need the
TK3
software
running on your PC. It should be loaded
and run as explained in its published text.
You also need the
TK3
p.c.b. connected to
the PC via its parallel printer port cable,
and a PIC16F84 in the allocated socket.
You do not need the l.c.d. or 7-segment
l.e.d. modules connected at this time.
However, if the l.c.d. module is already
connected you do not need to remove it.
All the software for this
Tutorial
should
be in the same folder. This may be the same
folder as used for
TK3’s
software, or in
PREPARATION
Everyday Practical Electronics, April 2003
PIC Tutorial V2 Supplement
5
Zgłoś jeśli naruszono regulamin