Documente Academic
Documente Profesional
Documente Cultură
JOHN BECKER
Demo circuit
Simple
randomising
48
Listing 11.1
MAIN
;
;
;
;
;
;
call BYTECOUNT7
call BYTECOUNT15
call BYTECOUNT255
call BYTE2COUNT
call BYTECOUNT50
call BYTECOUNT7A
call RANDOMISE
goto MAIN
Listing 11.2
BYTECOUNT255
incf COUNTLSB,F
call PAUSIT
Value limitation
In a games situation, you may not want
the random value to be displayed, in which
case simply omit the display commands and
make use of the derived value in any way
you want.
If you require the random value to be
limited to a maximum value below 255, this
can be done in several ways. An example of
limiting it to a maximum of 15 is shown in
Listing 11.3.
Here, the limitation is done by the
insertion of the command andlw 15 (binary
00001111). Other AND values could be
used instead, limiting the value to the
maximum that can be held in the resulting
active bits of COUNTLSB. A command of
andlw 7 (binary 00000111) would result in
a maximum value of 7.
Of course, for a maximum value of 7,
it is not necessary to use the BIN2DEC
routine to convert the value for LCD
display, as it is only necessary to OR the
value with 48 to convert it for the LCD, as
in Listing 11.4.
In this instance, it is also possible to keep
the value below 8 by the inclusion of the
command bcf COUNTLSB,3 after incf
COUNTLSB,F, as in Listing 11.5.
There are situations where you may
want to limit the range to a value which
cannot be obtained by ANDing or
clearing bits. Such would be the case if
you wished to create a value predictor for
Lotto, where the maximum value is 49.
An example of such limitation is shown
in Listing 11.6.
Here the count value is constantly checked
to see if it has gone beyond 49, ie to 50. If
it has, the count value is reset to 0 at this
point. Other values could be used instead,
as appropriate to the situation.
There may be occasions when you want
to arrive at values within a range greater
than 255. Here the counter could use two
or more bytes in the counting routine, as
shown in Listing 11.7, which uses two bytes,
COUNTLSB and COUNTMSB. Using two
bytes produces a potential maximum of
btfss PORTA,0
goto MAIN
call CLRLINE1
call LCDMSG2
btfsc PORTA,0
goto BYTECOUNT255
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
movwf REGA0
clrf REGA1
clrf REGA2
clrf REGA3
call BIN2DEC
call SHOWDIGIT7
return
Listing 11.3
BYTECOUNT15
incf COUNTLSB,F
btfsc PORTA,0
goto BYTECOUNT15
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
andlw 15
movwf REGA0
clrf REGA1
lrf REGA2
clrf REGA3
call BIN2DEC
call SHOWDIGIT9
return
Automatic randomisation
There are situations, though, in which
random numbers may need to be generated
automatically throughout a program, without
the intervention of user-pressed switches.
49
Seeding
What may not be obvious is the fact that
the value within the RANDOM(x) bytes at
power-on could in fact be zero. This is a
situation which can never generate a value
other than zero. It is essential to ensure that
this can never happen.
It is possible, of course, to check the
RANDOM(x) bytes for all zeros following
switch-on and setting at least one of them
to a non-zero value. In a simple program,
however, just setting a specified value into
the bytes would itself be a non-random
event. What is needed is a value which
itself changes each time the power is
switched on. Such a value is known as a
seed.
One way to do this is to have a value
held within the PICs non-volatile memory
(EEPROM) which is retrieved at switch on,
and then is immediately updated and stored
back to the EEPROM as well as being set
into RANDOM(x) bytes.
In the long term, there will indeed
be repetition of seed values, but this
technique does help to enhance the apparent
randomisation of overall value generation.
Remember that in software, we can only
ever use pseudo-random value generation,
but extending the repetition range is highly
beneficial, reducing the chances of obvious
predictability.
In Part 9 we illustrated how the PICs
EEPROM could be read and written to. That
technique can be used in connection with
acquiring and updating the seed value.
Any 8-bit value of the users choosing
could be set into the basic ASM program,
with it automaticaly being set into the PIC
via the HEX file. Each time the program
is run, that value would be retrieved, used
and updated, say incrementing it. It would
be necessary, of course, to ensure that
the updated value itself was never zero.
But thats easy enough if it is zero then
increment it to 1.
That concludes this section looking at
obtaining random values. Lets now look at
a practical way in which randomisation is
used.
50
Listing 11.4
BYTECOUNT7
incf COUNTLSB,F
btfsc PORTA,0
goto BYTECOUNT7
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
andlw 7
iorlw 48
call LCDOUT
return
Listing 11.5
BYTECOUNT7A
incf COUNTLSB,F
bcf COUNTLSB,3
btfsc PORTA,0
goto BYTECOUNT7
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
iorlw 48
call LCDOUT
return
Listing 11.6
BYTECOUNT50
incf COUNTLSB,F
movf COUNTLSB,W
xorlw 50
btfsc STATUS,Z
clrf COUNTLSB
btfsc PORTA,0
goto BYTECOUNT50
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
movwf REGA0
clrf REGA1
clrf REGA2
clrf REGA3
call BIN2DEC
call SHOWDIGIT9
return
Nudge, nudge
If the jackpot has not been won the simple
message NO LUCK THIS TIME! is shown
and the jackpot and totals values remain
unchanged.
Jackpot total scoring is affected not only
by the value within the jackpot, but also by
the value of the winning number, 1 for 1, 2
for 2, 3 for 3, and 10 for 0.
The Cheat switch allows you to always
win the jackpot when the program provides
the Nudge option. Pressing the switch always
ensures that the wrong number becomes the
same as the other two, so ensuring a win. If
you were to build this circuit on stripboard
and house it in a box, you could have the
Cheat switch on a lead external to the box
and hidden somewhere. You could then press
this switch unseen while pretending to press
the Nudge switch, disguising from your
friends that you are in fact cheating!
Listing 11.7
BYTE2COUNT
incfsz COUNTLSB,F
goto BYTE2
incf COUNTMSB,F
BYTE2
btfsc PORTA,0
goto BYTE2COUNT
call CLRLINE1
call LCD1
bsf RSLINE,4
movf COUNTLSB,W
movwf REGA0
movf COUNTMSB,W
movwf REGA1
clrf REGA2
clrf REGA3
call BIN2DEC
call SHOWDIGIT6
return
Listing 11.8
SETRANDOM
Possible changes
There are changes to the program that
you might like to make. You could increase
the number of values that are cycled through
the counters, as described above. This will
make it harder for a winning combination
to occur (if you are not cheating!). The
scoring values at any win could also be
increased quite readily. The incremental
value of the jackpot can also be changed to
a higher value with a spot of minor program
modification.
You could also allocate symbols other
than the counter values themselves to be
shown. For example, via a look-up table,
you could allocate alphabet letters to be
shown instead of the numerals. In Part 10
last month we also showed how you can
create your own LCD symbols for display
in place of the actual numerals.
Be adventurous, have a go at changing
this program to suit your own needs. What
you do and how you achieve it is entirely
over to you good luck!
clrf XORGATE1
clrf XORGATE2
btfsc RANDOM1,2
bsf XORGATE1,0
btfsc RANDOM4,6
bsf XORGATE2,0
bcf STATUS,C
movf XORGATE1,W
xorwf XORGATE2,W
btfsc STATUS,Z
bsf STATUS,C
rrf RANDOM1,F
rrf RANDOM2,F
rrf RANDOM3,F
rrf RANDOM4,F
return
Listing 11.9
RANDOMISE
call SETRANDOM
call LCD1
bsf RSLINE,4
movf RANDOM1,W
movwf REGA0
movf RANDOM2,W
movwf REGA1
movf RANDOM3,W
movwf REGA2
movf RANDOM4,W
movwf REGA3
call BIN2DEC
call SHOWDIGIT1
return
Further reference
So, now over to you enjoy using your
PICs and the programs you write for them.
And dont forget to read PIC n Mix, the
regular column in EPE, which covers many
aspects relating to PICs.
51