2003_05_supp(2).pdf
(
609 KB
)
Pobierz
T
N
EE ME
FR LE
PP
SU
EPE PIC
TUTORIAL V2
JOHN BECKER
PART TWO
Quite simply the easiest
low-cost way to learn
about using PIC
Microcontrollers!
In this part we play with switches, make
noises, count times, and generally have
fun with some more PIC16F84 commands!
TUTORIAL 7
CONCEPTS EXAMINED
Switch monitoring
Command ANDLW
Command ANDWF
Command ADDWF
Command ADDLW
Nibbles
STATUS bit 1
Digit Carry flag
Bit code DC
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-
SW3 (via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Capacitor C7 as 1mF
Preset VR1 set to minimum resistance
(fully clockwise)
From hereon we shall usually omit the
program initialisation commands that have
up to now been shown at the top of each
listing. Some will be included where they
help to clarify the program. Otherwise,
assume that any name used in the listing
extracts shown will have been defined or
equated in the headings. The commands
are included in full on the disk file pro-
gram listings (source code).
We now turn to looking at how data is
input via switches and shall continue to
show the results on individual l.e.d.s. In
Tutorials 21 and 22 we shall look at 7-seg-
ment l.e.d.s and alphanumeric l.c.d.s as the
output displays.
First connect TK3’s pushbutton switch-
es SW0, SW1, SW2, SW3 (via CP19,
CP18, CP17, CP16) to PORTA pins RA0,
RA1, RA2, RA3 respectively. Connect the
switch power pin CP21 to the +5V OUT
pin, and switch power pin CP20 to the 0V
OUT pin. Port pins RA0 to RA3 are now
EPE PIC
TUTORIAL
LISTING 8 PROGRAM
TK3TUT8
BEGIN
LOOP
CLRF COUNT
MOVF PORTA,W
ANDLW B’00000001’
ADDWF COUNT,F
MOVF COUNT,W
MOVWF PORTB
GOTO LOOP
connected so that they are normally biased
low (to 0V) but will go high (+5V) when
their respective switches are pressed.
Run TK3TUT8.HEX. Pushing switch
SW0 on and off, PORTB’s l.e.d.s will be
seen to go on and off in a binary sequence
when the switch is on (pressed), but will
remain in the last condition when the
switch is off (released). In this example,
the program tests whether the status of
switch SW0, which is connected to
PORTA RA0 (bit 0), is on or off. If the
switch is on then the counter variable,
COUNT, is repeatedly added to (by 1 in
this example). A value of zero is added to
the count if the switch is off. The count
value is output to PORTB.
First let’s look at two of the commands
introduced here, ANDLW and ADDWF.
Their counterparts ANDWF and ADDLW
will also be examined.
This technique is widely used in elec-
tronics and computing, the final answer
determining the subsequent action to be
taken by a circuit or software routine.
There are two ANDing commands
available with PICs, ANDLW (AND
Literal to W), and ANDWF (AND W with
File value). Suppose that the first number
in the foregoing examples (01110010) is
already contained within W, we then wish
to AND it with a fixed number as stated in
a program command. Assuming that the
fixed number is the second number quoted,
the command is:
ANDLW B’01011001’
The PIC ANDs the second (literal) num-
ber with that already held in W. The
answer (01010000) is retained by W and is
available to be further manipulated or
copied into any file as specified by the
command which follows ANDLW. You
could, for example, use the command
MOVWF PORTB which will turn on
l.e.d.s LD6 and LD4 (01010000).
Any of the three numerical formats may
be used with ANDLW, e.g. B’00011111’
(binary), H’1F’ (hexadecimal), 31 (deci-
mal), are all legitimate and equal. It is also
legitimate to use a name that has been
equated with a value, e.g. ANDLW
PORTB (which would AND 6 with W
since we have previously specified that the
name PORTB represents the value 6).
The command ANDWF is used to AND
an existing value within W to a value with-
in a named file, either retaining the answer
in W (ANDWF FILENAME,W) or putting
back in the named file (ANDWF
FILENAME,F).
It is not possible to directly AND the
contents of two files together, the value of
one or other file must have already been
moved into W before the ANDing can take
place. With both commands ANDLW and
ANDWF, if the answer is zero, the Zero
As no doubt most of you are aware, if
one binary number is ANDed with anoth-
er, then only if the same bits of both num-
bers are set (1) will the answer also have a
1 in that position. Any zeros on either or
both sides for any bit will automatically
produce a result of 0, e.g.:
First number:
Second number:
ANDed answer:
01110010
01011001
01010000
COMMANDS ANDLW
AND ANDWF
Everyday Practical Electronics, May 2003
PIC Tutorial V2 Supplement
–
17
flag of STATUS is set. If the answer is
greater than zero, the Zero flag is cleared.
Zero is the only flag affected by an AND
command.
COMMANDS ADDLW
AND ADDWF
There are two ADDing commands avail-
able with PICs, ADDLW (ADD Literal to
W), and ADDWF (ADD W to a File value).
Command ADDLW is used where a fixed
number (literal) within a program is to be
added to an existing value within W and
which has been obtained by a previous
operation. Suppose that W holds the
answer produced in the previous ANDing
example, 01010000 (decimal 80), and you
wish to add a fixed value to it, 53 decimal
(00110101), for instance. The command
would be:
ADDLW 53 (or ADDLW H’35’ hex-
adecimal, or ADDLW B’00110101’ binary).
The answer in this instance is 10000101
(decimal 133) and is retained in W for fur-
ther use or copying into a file, e.g.
MOVWF PORTB.
Command ADDWF adds the contents of
W to the value within a stated file. The
answer can be held in W (ADDWF
PORTB,W) or put back into the named file
(ADDWF PORTB,F).
Three flags within STATUS are affected
by any ADD command, Carry, Zero and
Digit Carry. If the answer to an addition is
greater than 255, the Carry flag is set, oth-
erwise it is cleared. If the answer equals
zero, the Zero flag is set, otherwise it is
cleared. The third flag, Digit Carry, you
have not encountered yet. Although the
concept is not illustrated until later
(Tutorial 19), it is appropriate to describe it
now.
If you imagine that an 8-bit binary num-
ber (e.g. 10110110) is split into two halves
(known as “nibbles”), 1011 and 0110, the
righthand nibble is monitored by the PIC
as a separate digit and it is served by its
own flag, the Digit Carry flag. If an addi-
tion takes place which produces a result
greater than 15 (binary 1111) for that nib-
ble, the Digit Carry flag is set, otherwise it
is cleared.
count value. With the command GOTO
LOOP, the sequence is repeated.
It will be seen that there is only an
increase in the count value if PORTA bit 0
holds a 1, therefore the count will only
change if the switch is on (pressed).
Pressing any other switch connected to
PORTA has no effect. When the counter
passes 255, its value rolls over to zero and
starts counting upwards again.
8.2. Could one of the Zero flag testing
commands be used instead of BTFSS? If
so, how, and would an AND command be
useful? (Remember that PORTA has more
bits than just bit 0).
TUTORIAL 9
CONCEPT EXAMINED
Responding to a switch press only at the
moment of pressing
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Capacitor C7 as 1mF
Preset VR1 set to minimum resistance
(fully clockwise)
EXERCISE 7
7.1. Can you see another way of writing
the first two lines using MOVLW and
ANDWF?
7.2. Can you see how the BTFSS or
BTFSC commands might be used to
achieve the same output result; the use of
MOVLW 1 or ADDLW 1 could be useful
here.
7.3. There is also the opportunity to use
INCF in this type of situation. Try rewrit-
ing to include this command.
TUTORIAL 8
LISTING 10
PROGRAM TK3TUT10
BEGIN
TESTIT
CLRF COUNT
CLRF SWITCH
BTFSC PORTA,0
GOTO TSTPRV
BCF SWITCH,0
GOTO TESTIT
BTFSC SWITCH,0
GOTO TESTIT
INCF COUNT,F
MOVF COUNT,W
MOVWF PORTB
BSF SWITCH,0
GOTO TESTIT
CONCEPTS EXAMINED
Increasing speed of TK3TUT8
Bit testing for switch status
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Capacitor C7 as 1mF
Preset VR1 set to minimum resistance
(fully clockwise)
TSTPRV
LISTING 9 PROGRAM
TK3TUT9
LOOP
BTFSS PORTA,0
GOTO LOOP
INCF COUNT,F
MOVF COUNT,W
MOVWF PORTB
GOTO LOOP
LISTING 8 FLOW
Having described the new terms, we
shall now detail what happens in Listing 8.
As said at the start of Tutorial 7, switches
SW0 to SW3 are biassed so that their
respective PORTA pins are normally at 0V
(low) but go high when pressed. In this
example program, at the label LOOP the
contents of PORTA are copied into W
(MOVF PORTA,W), which then holds the
status of all five usable bits of that port. We
are only interested, though, in the status of
the switch on PORTA bit 0, switch SW0.
Therefore, in the next command (ANDLW
B’00000001’) bit 0 is ANDed with 1 to
isolate its value, the other seven bits in W
being cleared by the respective zeros of the
ANDed value.
The answer in W is then added to the
contents of the counter (ADDWF
COUNT,F). Next, the contents of the
counter are brought back into W (MOVF
COUNT,W) and then copied into PORTB
(MOVWF PORTB), whose l.e.d.s are
turned on or off depending on the binary
In TK3TUT8 we saw that the count
adding commands etc. were performed
even if the count value was zero. This is a
waste of processing speed, why bother to
add zero to a count? The program in
Listing 9 shows a faster alternative. Run
TK3TUT9.HEX.
By using the command BTFSS to check
the status of a switch (in this case still SW0
on PORTA bit 0), if the switch is not
pressed we can avoid the count increment-
ing procedure, jumping immediately to a
further switch status test. Alternatively, in
another program, by substituting another
destination instead of LOOP, we could
jump to a totally different routine and per-
form some other procedure.
Another choice is to use the command
RETURN instead of GOTO LOOP to
return to another routine which had called
this one. Commands CALL and RETURN
will be covered in Tutorial 13.
It is expected that you will recognise
from Listing 9 what the program does and
how it does it. If you don’t, re-read Tutorial
4 and the section on BTFSS.
EXERCISE 8
8.1. What happens if you use BTFSC
instead of BTFSS?
In the switch press examples of
Listings 8 and 9, we saw that the counter
was incremented for the entire duration
of the switch being on. Often, only a sin-
gle response to a change of switch status
might be required. This entails testing
the switch status and comparing it with a
previous test. Only if the switch is on and
if that on condition has not yet been
responded to will the next action be
performed.
Load TK3TUT10.HEX. You are still
monitoring PORTA bit 0 for the switch
press (SW0), responding to it via the l.e.d.s
on PORTB. Observe the l.e.d.s while press-
ing SW0 on and off. For each pressing,
only one change of the l.e.d. count will
occur (but note that low-cost switches may
cause switch-bounce, resulting in the count
increasing for each bounce – a matter cov-
ered later).
Study Listing 10: the entry to the routine
is at BEGIN where two variables, COUNT
and SWITCH are cleared. At the label
TESTIT, the command is BTFSC
PORTA,0, testing the status of PORTA bit
0 (is it clear?). Remember that we are only
interested in the bit being set. If it is false
that bit 0 is clear (i.e. that it is set – the
switch is pressed) the command GOTO
TSTPRV is performed and then the status
of SWITCH bit 0 is tested, BTFSC
SWITCH,0. This bit serves as the flag to
keep track of the previous status of the
switch. At this moment, the bit will be
clear because the whole byte was cleared
on entry to the routine. Consequently, the
GOTO TESTIT command is skipped, the
count is incremented and its value output to
PORTB.
18
– PIC Tutorial V2 Supplement
Everyday Practical Electronics, May 2003
Now SWITCH bit 0 is set (BSF
SWITCH,0) to indicate that the count has
been incremented for this switch press (i.e.
the flag is set), and the program jumps
back to TESTIT. If the switch is still
pressed, then at TSTPRV the BTFSC
SWITCH,0 command will produce a false
answer and the command GOTO TESTIT
will be performed, thus preventing the
counter from being further incremented at
this time.
What is now needed is for the switch to
be released so that the two commands BCF
SWITCH,0 (clear the flag) and GOTO
TESTIT can occur. The stage is then once
again set for the next switch press to be
responded to by the counter.
LISTING 11
PROGRAM TK3TUT11
BEGIN
TEST1
CLRF COUNT
CLRF SWITCH
BTFSC PORTA,0
GOTO TSTPR1
BCF SWITCH,0
GOTO TEST2
BTFSC SWITCH,0
GOTO TEST2
BSF SWITCH,0
INCF COUNT,F
GOTO OUTPUT
BTFSC PORTA,2
GOTO TSTPR2
BCF SWITCH,2
GOTO TEST1
BTFSC SWITCH,2
GOTO TEST1
BSF SWITCH,2
DECF COUNT,F
MOVF COUNT,W
MOVWF PORTB
GOTO TEST1
switches on PORTA (SW0 to SW3) while
observing the l.e.d.s on PORTB. This rou-
tine should need no further comment.
Another way of expressing the first two
commands is:
LOOP
MOVLW B’00001111’
ANDWF PORTA,W
TSTPR1
Now load TUT13.HEX and run it, again
experimenting with pressing any combina-
tion of the switches on PORTA (SW0 to
SW3) and observing the l.e.d.s on PORTB.
TEST2
LISTING 13
PROGRAM TK3TUT13
LOOP
SWAPF PORTA,W
ANDLW B’11110000’
MOVWF PORTB
EXERCISE 9
9.1. In Listing 10, AND and MOV com-
mands could have been used instead of
BTFSC and BCF. How, and with what
other command?
9.2. Would using BTFSS instead of
BTFSC involve more commands and
labels having to be used as well?
9.3. Because low cost switches have
probably been used, there is the danger that
mechanical switch bounce might occur,
causing the count to be incremented unde-
sirably. Another counter could be used to
cause a delay in the rate of switch testing to
eliminate the effects of switch bounce.
How would you implement the delay, and
where would you put the commands
required. Hint, another label will be need-
ed as well.
TSTPR2
OUTPUT
EXERCISE 10
TUTORIAL 10
CONCEPTS EXAMINED
Performing different functions depend-
ing upon which of two switches is
pressed
The use of a common sub-routine serv-
ing two other routines
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Capacitor C7 as 1mF
Preset VR1 set to minimum resistance
(fully clockwise)
Run TK3TUT11.HEX and experiment
with the switches on PORTA bits 0 and 2
(SW0 and SW2). You will discover that
switch SW0 causes the count displayed on
the l.e.d.s to be increased, and that switch
SW2 decreases the count. The basic logic
flow is the same as that in Listing 10,
except that two switches are used and each
switch is responsible for a different
routine.
Note that whilst each switch could have
had its own routine to output to PORTB,
the two routines would be the same.
Consequently, each switch routine is rout-
ed into a common output sub-routine
(OUTPUT). At the end of SW0’s routine,
the command GOTO OUTPUT needs to be
given, but at the end of SW2’s routine, no
GOTO OUTPUT command is needed
because OUTPUT follows immediately
after it. It is said to reach OUTPUT by
default
because it does not need to be told
to go there.
10.1. How do you think a single test for
neither
of the switches being pressed could
be introduced, shortening the testing time?
Could an AND be used with a STATUS
check, or can a STATUS check be used on
its own without an AND? (Think carefully
about the latter.)
10.2. How would you increase the count
by more than one, say two, at each press of
switch SW0? With the knowledge you’ve
gained so far, three ways should come to
mind, one of them including the use of a
new named variable.
10.3. If you want to add 255 each time a
switch SW0 press occurs, do you need an
ADD command, or is there another com-
mand which will do the same job? (Think
rollover.)
TUTORIAL 11
CONCEPTS EXAMINED
The ease of 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
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Capacitor C7 as 1mF
Preset VR1 set to minimum resistance
(fully clockwise)
Load TUT12.HEX. Experiment with
pressing any combination of the four
You will see while you press PORTA’s
four switches, that they are having their
status displayed on PORTB’s four lefthand
l.e.d.s (LD7 to LD4), even though you have
not changed the wiring to PORTB and the
l.e.d.s. Had there been a fifth switch, on
PORTA RA4, it would be affecting the first
l.e.d. on the right (LD0) – if a different
AND value were used (what value?).
What is happening is that the software
has been told to swap and move into W
(SWAPF PORTA,W) the left and righthand
four bits of PORTA (its nibbles, as intro-
duced in Tutorial 7). The answer is then
ANDed with bits that reflect the swapped
status in order to remove any possibility of
influence by the unused bits of PORTA’s
register.
The SWAPF command is especially use-
ful if the values of the two nibbles are
required separately as values of up to 15
(00001111). A good example of its use will
be seen in Tutorial 21. It is illustrated now
because of its programming similarity to
TK3TUT12 and TK3TUT14.
The F suffix can be used with SWAPF
instead of W, as with other files discussed.
There is no command which allows nibbles
to be swapped once the byte is in W. If a
byte within W needs swapping, it must be
put out to a file, and then the SWAPF
(FILENAME),W command given to bring
it back into W.
Let’s look now at another command
which uses a similar demonstration routine
to TK3TUT12 and TK3TUT13. Run
TK3TUT14.HEX. Once more, experiment
with pressing any combination of switches
SW0 to SW3 while watching PORTB’s
l.e.d.s.
LISTING 14
PROGRAM TK3TUT14
LOOP
COMF PORTA,W
ANDLW B’00001111’
MOVWF PORTB
GOTO LOOP
LISTING 12
PROGRAM TK3TUT12
LOOP
MOVF PORTA,W
ANDLW B’00001111’
MOVWF PORTB
GOTO LOOP
You will now discover that instead of
l.e.d.s being turned on when a switch is
pressed, they are turned off, and vice versa.
This is due to the command COMF, which
automatically inverts each bit of a byte, 1s
becoming 0s, 0s becoming 1s, i.e. it per-
forms a task known as
complementing,
hence COMF, which means COMplement
File.
Everyday Practical Electronics, May 2003
PIC Tutorial V2 Supplement
–
19
There are several uses for this command,
one of which is the situation when all the
switches are biased to the +5V line instead
of 0V. In that instance, and using the switch
testing techniques shown earlier, pressing
the switches would produce the wrong bit
levels for the commands shown: switches
would need to be held pressed for off,
releasing them for on. Not an easy thing to
do with push-switches!
Swap over the 0V and +5V connections
to pins CP21 and CP20 so that PORTA
pins RA0 to RA3 are biassed to +VE,
going low when switches SW0 to SW3
are pressed. Run the program again. You
will find that the l.e.d.s respond as they
did for TK3TUT12. Now run TK3TUT12
again and confirm that the l.e.d. results
are the inverse of that previously seen
with it.
Another use for COMF is in subtraction.
This is a concept for experienced program-
mers and will not be demonstrated here. In
a nutshell, the use of COMF allows addi-
tion to be used instead of subtraction while
still achieving the desired objective. This
technique can be easier in some instances
than using the available subtraction
commands.
The F suffix can be used with COMF
instead of W, as with other files discussed.
There is no command which allows the
inversion of a byte once it is in W. If a byte
within W needs inversion, it must be put
out to a file, and then the COMF (FILE-
NAME),W command given to bring it back
into W.
LISTING 15
PROGRAM TK3TUT15
SOUND
GETKEY
MOVLW 80
MOVWF NOTE
MOVWF FREQ
BTFSS PORTA,0
GOTO GETKEY
DECFSZ NOTE,F
GOTO GETKEY
MOVF FREQ,W
MOVWF NOTE
MOVLW B’00010000’
ADDWF PORTA,F
GOTO GETKEY
EXERCISE 11
So far we have been outputting data to
l.e.d.s, and at a comparatively slow rate.
We have also been using one port as a
switch input and the other port as the out-
put. Here we examine how the same port
can be used simultaneously for input and
output via different bits. In doing so, we
use sound as the medium by which we
indicate the status of a switch, generating
an audible frequency when it is pressed.
The 1mF capacitor used up till now for
C7 should be omitted from hereon.
Connect a 330W resistor between RA4
and the +5V connection at CP21. Pin RA4
is an open-collector pin and this resistor
biasses it so that an output can be generat-
ed on it. Connect a 1mF capacitor with its
positive lead on the junction of RA4 and
the resistor. Connect the negative lead of
the capacitor to the signal terminal of a
jack socket that suits your personal (high-
impedance) headphones (see Fig.3).
+
5V OUT
3.5mm STEREO
JACK SOCKET
(SEE TEXT)
With these exercises, reconnect the +5V
connection to CP21 and 0V to CP20.
11.1 If SWAPF was not available as a
command, how would you write a routine
which produced the same result (would
RLF or RRF be suitable commands)?
11.2
Rewrite
TK3TUT13
and
TK3TUT14, putting the contents of W out
to a file of any name (which you must
equate at the beginning of the program),
performing another COMF or SWAPF
action, and then bringing it back into W
for output to PORTB. Can PORTB be used
as the temporary file store in these
rewrites?
11.3. Write a routine that allows the nib-
bles of a byte to be put into separate files
and each having a value no greater than
H’0F’ (decimal 15); there are several ways
of doing it.
330Ω
1µ
HEADPHONES
When that occurs, file NOTE is decre-
mented and its zero status tested. If it is
not yet zero, the routine jumps back to the
switch test.
When the switch has been pressed for
long enough (mere thousandths of a sec-
ond), NOTE will eventually reach zero, at
which point the command MOVF
FREQ,W is reached, followed by the fixed
value of FREQ being reset into NOTE.
Next, the value in PORTA has 16 (binary
00010000) added to it to increment the
count at bit 4 (so alternating the bit
between 0 and 1), and then there is a jump
back to further switch testing.
For as long as switch SW0 is pressed,
PORTA bit 4 will be periodically incre-
mented. The speed at which the routine
runs causes this bit to change at the audio
frequency rate to which you are listening.
If you adjust the rate setting preset, VR1,
you will hear the change in the resulting
frequency.
In a real-life situation, of course, the
operating frequency of the system would
normally be fixed. One frequency correc-
tion choice then is to change the value of
FREQ.
There is, though, another factor that
will affect the resulting audio frequency:
the number of commands within the con-
trolling loops. To illustrate the point, let’s
change the number of commands
involved.
You may think that to add more com-
mands would be difficult, what would they
do which would not interfere in the com-
pletion of the loop? Well, there are several
options, such as repeating some of the
existing commands, MOVF FREQ,W for
example, or MOVWF NOTE. Neither of
these commands would actually change
anything, except for the rate of operation.
However, a tailor-made command is
already available in the PIC’s command
codes which is intended for use where
minor delay tactics are needed, command
NOP.
+
TO RA4
0V OUT
COMMAND NOP
Fig.3. Audio output connections.
Do not connect a loudspeaker directly to
this circuit as there is insufficient power to
drive it.
Load TK3TUT15.HEX and press switch
SW0 on and off. A frequency tone will be
heard when the switch is pressed.
In the initialising statements at the head
of the full TK3TUT15.ASM program,
PORTA has been set with bits 0 to 3 as
inputs and bit 4 as an output (MOVLW
B’00001111’, MOVWF TRISA). You
should now recognise all the commands
given in the heart of the program shown.
Only a general commentary on what hap-
pens is now given.
On entry into the routine headed
SOUND, a value of 80 is loaded into the
files named NOTE and FREQ. The value is
arbitrary as far as this demonstration is
concerned. You may choose any other from
1 up to 255; the lower the value, the higher
the frequency generated.
PORTA’s status is monitored at
GETKEY and the setting (logic 1) of
PORTA bit 0 by switch SW0 is being
looked for. Switch testing is repeated
until SW0 is pressed, setting RA0 high.
TUTORIAL 12
Command NOP simply stands for No
OPeration. Responding to this command
takes the PIC just as long as responding to
any other single-cycle command but its
response is to just do nothing!
This command, then, can be used here to
slow down the resulting note frequency.
Insert it immediately before DECFSZ
NOTE,F. When running the amended pro-
gram you will notice that a change in the
output frequency has occurred.
CONCEPTS EXAMINED
Generating an output frequency in
response to a switch press
The use of two port bits set to different
input/output modes
Command NOP
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
Port A RA4 connected as in Fig.3 (audio
connection)
CP21 to +5V OUT
CP20 to 0V OUT
1mF capacitor C7 omitted (from hereon)
Preset VR1 set to minimum resistance
(fully clockwise)
EXERCISE 12
12.1. Experiment with different values for
FREQ. What happens if you set FREQ to
zero – does it stop a note being generated?
Explain the result.
12.2. Experiment with more than one
NOP command in the loop.
12.3. At which other places can you
alternatively insert NOP, and is the fre-
quency change still noticeable?
12.4. Are there any places where you
cannot use NOP?
12.5. When the audio frequency is not
being generated there is the likelihood that
RA4 will be set low, so sinking current
through the 330W resistor. Can the pro-
gram be modified so that this cannot
occur.
20
– PIC Tutorial V2 Supplement
Everyday Practical Electronics, May 2003
TUTORIAL 13
CONCEPTS EXAMINED
Command CALL
Command RETURN
Command RETLW
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Preset VR1 set to minimum resistance
(fully clockwise)
results could occur. For example, the fol-
lowing is “illegal”:
PROG1 CALL PROG2
GOTO PROG1
PROG2 GOTO PROG1
This is “legal”, though:
PROG1 CALL PROG2
GOTO PROG1
PROG2 RETURN
When the program returns from a CALL
following a RETURN command, the con-
tents of W are those which were put there
by the last command which used W.
Consequently, you can perform a complex
sub-routine, end up with an answer in W
and, using the RETURN command, return
to the main program with that result still
retained in W.
Command RETLW, though, returns to
the main program with W holding the
value which RETLW has acquired as part
of that command. A literal value is always
specified as part of the RETLW command,
e.g. RETLW 127 or RETLW 0. That value
replaces any other value within W and is
the one which is held in W on the return to
the calling point. The value may be
expressed in decimal, hexadecimal, binary
or as a “named” value equated during pro-
gram initialisation.
To explain Listing 16 then, at LOOP the
sub-routine at PROG1 is CALLed from
where the value held in PORTA is moved
into W. A return is made to the loop where
the next command to be performed is
MOVWF PORTB, after which the GOTO
LOOP command again takes us back to
CALL PROG1 again.
It is important to be aware that PICs
have a limit to the number of calls that can
be nested (calls being made from within
calls). This is due to the PIC’s Stack (the
LISTING 16
PROGRAM TK3TUT16
LOOP
CALL PROG1
MOVWF PORTB
GOTO LOOP
PROG1 MOVF PORTA,W
RETURN
Before looking further into sound gener-
ation, there are several commands that we
should examine. Three of those are associ-
ated with calling sub-routines: CALL,
RETURN and RETLW.
Load TK3TUT16.HEX and experiment
with pressing different combinations of
switches SW0 to SW3 while observing
PORTB’s l.e.d.s.
area that monitors the return addresses
when calls are completed) being limited to
only eight address values. If the Stack
receives more than eight addresses it will
over-write the earlier ones, causing a pro-
gram crash.
There is no way to read or write to the
Stack or to determine how full it is. It is
therefore imperative that if you are using
nested calls then you must keep very care-
ful track of how many you are using. In
such cases consider whether you could
achieve the same result by using GOTO
commands for some of the calls, or by
returning to the previous calling routine
before making the next call.
Run program TK3TUT16 and confirm
that l.e.d.s LD0 to LD3 respond as expect-
ed to the pressing of the four switches.
EXERCISE 13
13.1. Rearrange TK3TUT16 so that
reading PORTA is in the main loop and
outputting data to PORTB is in the called
routine.
13.2. Try adding other commands in the
subroutine, such as AND or ADD.
13.3. Use RETLW as the final statement
in the subroutine, using any literal value of
your choice, verifying its operation!
TUTORIAL 14
Tables
Register PCL (again)
Register PCLATH
CONCEPTS EXAMINED
COMMANDS CALL,
RETURN AND RETLW
Programs can be written as a series of
sub-routines which can be reached in one
of three ways, directly by default (without
being told to go there), via a GOTO com-
mand, or by a CALL command. (Routing
following automatic detection of an inter-
rupt event is another matter and is dis-
cussed later.)
We have shown several examples of the
first two. Program TK3TUT4 (Tutorial 4)
uses them both: the sub-routine LOOP1 is
entered directly following the initialisation
routine. LOOP2 is also entered directly
from the end of LOOP1. Both LOOP1 and
LOOP2 are then further accessed by
GOTO commands.
However, a CALL command can be
used if one routine needs to make use of
another and then once that has been com-
pleted, for the program to jump back to
continue from the command that follows
the call. The use of sub-routines allows the
same routine to be accessed from many
other areas within the overall program, so
saving on program space.
A second command always has to be
used before the program returns to the call-
ing origin. That command takes one of two
forms, RETURN (which is an obvious
command – return to where you came
from) or RETLW (RETurn to where you
came from with a Literal value held in W).
There is a third return command, RETFIE,
which we shall meet later in connection
with interrupts.
A GOTO command can never be used to
end a sub-routine call – the PIC will con-
tinue to expect a return command and, if
repeated calls to a sub-routine are made
without a RETURN or RETLW command,
it will become confused and unpredictable
CONNECTIONS NEEDED
All Port B to all l.e.d.s.
Port A RA0-RA3 to switches SW0-SW3
(via CP19-CP16)
CP21 to +5V OUT
CP20 to 0V OUT
Preset VR1 set to minimum resistance
(fully clockwise)
LISTING 17 PROGRAM TK3TUT17
TABLE
BANK0
GOTO LOOP
ANDLW B’00001111’
ADDWF PCL,F
RETLW 255
RETLW 1
RETLW ’5’
RETLW 0
RETLW 31
RETLW 193
GOTO OTHER
RETURN
RETLW B’10101010’
RETLW H’C7’
RETLW ’A’
RETLW 65
RETLW ’B’
RETLW ’x’
GOTO OTHER1
MOVF STORE,W
RETURN
MOVF PORTA,W
CALL TABLE
MOVWF PORTB
GOTO LOOP
RETLW STORE
MOVLW 128
ADDWF PORTA,W
RETURN
; AND W with 15
; ADD to PCL
; 0 11111111
; 1 00000001
; 2 00110101
; 3 00000000
; 4 00011111
; 5 11000001
; 6 00100000
; 7 00000111
; 8 10101010
; 9 11000111
; 10 01000001
; 11 01000001
; 12 01000010
; 13 01111000
; 14 10001110
; or 10011110
; 15 00000000
LOOP
OTHER
OTHER1
Everyday Practical Electronics, May 2003
PIC Tutorial V2 Supplement
–
21
Plik z chomika:
fred1144
Inne pliki z tego folderu:
2001_11_supp(2).pdf
(2950 KB)
2000_xmas_supp(2).pdf
(1853 KB)
2001_02_supp(2).pdf
(1735 KB)
2003_04_supp(2).pdf
(912 KB)
2001_04_supp(2).pdf
(935 KB)
Inne foldery tego chomika:
1998
1999
2000
2001
2002
Zgłoś jeśli
naruszono regulamin