Formalizing the
Variable List
To finish off the first round of design, it is helpful
to make some concrete decisions about how data will
be stored. After the most basic features are
prototyped, you should have a fairly good idea of how
well your assumptions have played out, and whether
or not you need to add any major variables. I like to lay
out specifically where each variable should be placed in
data memory. This is especially important in a
microcontroller with banked memory, because
switching banks takes extra time and memory. In some
microcontrollers, you may not be able to copy data
from one bank to another without using temporary
variables in the non-banked (or shadowed) memory —
unless you switch banks between each item.
I then lay out the memory in a spreadsheet in
the same format as the data sheet’s register map
(Figure 2). I use one cell for each memory location,
and type in the variable names that I want in each
place. This makes them easy to rearrange. When
writing your program later, you can easily see which
bank holds each variable. To minimize bank switching,
the shadowed memory should hold most variables that
are used frequently or used by multiple parts of the
program. It is also useful to place temporary variables
there, so they can be accessed no matter which bank
you are using at the time.
00
00h INDF
01h TMR0
02h PCL
03h STATUS
04h FSR
05h OSCCAL
06h PORTB
07h PORTC
08h Delay0
09h Delay1
0Ah Scratch0
0Bh Scratch1
0Ch Scratch2
0Dh Scratch3
0Eh Scratch4
0Fh Scratch5
10h Display0
11h Display1
12h Display2
13h Display3
14h Display4
15h Display5
16h Display6
17h Display7
18h Display8
19h AppState
1Ah SwState0Prev
1Bh SwState0Curr
1Ch SwState1Prev
1Dh SwState1Curr
1Eh GameOptions
1Fh OptionCounter FIGURE 2. Data memory diagram
with variable names.
Bank bits
01 10
||
||
||
||
||
||
||
Shadowed Shadowed
||
||
||
||
||
||
||
||
MasksL0 MasksH0
MasksL1 MasksH1
MasksL2 MasksH2
MasksL3 MasksH3
MasksL4 MasksH4
MasksL5 MasksH5
MasksL6 MasksH6
MasksL7 MasksH7
MasksL8 MasksH8
11
|
|
|
|
|
|
|
Shadowed
|
|
|
|
|
|
|
|
Random0
Random1
Random2
Random3
Random4
Random5
Random6
Random7
Random8
Random9
PUZZLE BOX PARTS LIST
Specific Parts, Supplier & Part
Microcontroller
Microchip – PIC16F505
Pushbutton switches ( 9)
RadioShack 275-1547 or 275-1571
LEDs, D0-D8
RadioShack 276-1622 (assorted)
Common Parts
Power switch, SPST
0.1 µF capacitor, any type
Resistors R1-R3, 220 ohms
Diodes D9-D11, general-purpose such
as 1N4001
Single-row six-pin header for ICSP
(optional)
Download files that accompany
this article are available at
www.servomagazine.com.
Additional Analysis
Analysis and design tend to blend together, and that is okay. It would be
hard to avoid making any decisions on the design while analyzing the
requirements or the results of testing your design. Forcing yourself to stop and
analyze where you stand is a good way to check if you are really designing the
software the best way you can.
In the prototype, I had stored the entire value I read from PORTB as a
variable to use each time I ran through the main loop. This wastes some memory,
but more importantly it makes it harder to work with the switch values. I noticed
at this point that it would be easier to store the previous and current values of
each switch as a single bit in two separate variables. If these variables are XORed
together, a bit is set for each switch that changes states.
The display and switch reading code in the prototype worked well, so I
determined there was nothing major to change there. I tweaked the variable list
(adding the new previous/current variables for the switch states) and after
reviewing the variable list again, everything looked good, so no additional
analysis was needed.
Matt Rusnak can be reached at mrusnak@roadrunner.com.
SERVO 06.2010 51