Following on from and using the theory we've already covered. I thought it might be a good idea to try and make the RANDOM time pause generated from the previous program slightly more RANDOM.
I noticed with the last program that RANDOM always 'seeded' with the same start value at power up a (2522_Mili_secs) pause and then followed the same number sequence on the display. So if you powered up and powered down the program in fairly quick succession you'd soon notice the start sequence wasn't that RANDOM at all, it was exactly the same!
So I got to thinking what if, when the program 'powered down' I saved the last RANDOM value in EEPROM memory and then re-loaded that value as the start 'seed' on 'power up' that would then continue the 'RANDOM' sequence (ok, RANDOM from 1-65535) so pretty RANDOM unless you're a memory man.
Here's the code I came up with (I changed from the Serial Communicator to LCD (why not, all good practice)). It seems to work too
.
The program continually WRITES to EEPROM whilst the program runs but only READS from EEPROM on power_up. What do you think?
CODE:
Code:
X VAR WORD
Y VAR WORD
Z VAR WORD 'STORED IN EEPROM LOCATIONS 0 AND 1
Start:
goto Power_up: 'Load STORED last RAND_VAL
Power_up: 'Power_up subroutine
Read 0, Z.HIGHBYTE 'Read VAL_Z_high byte
Read 1, Z.LOWBYTE 'Read VAL_Z_low byte
LET X = z 'SEED X with the VAL Z
GOSUB RAN:
RAN:
Random X 'RANDOM program cycle continues
Y = X * 1000
Z = DIV32 21845
LCDOUT $FE,2,"MSECS=",DEC4 Z 'LCD displays RAND_VAL
PAUSE Z 'Program PAUSES for 'Z_Mili_secs'
HIGH PORTA.2 'LED PORTA.2 lights up
PAUSE 500
low PORTA.2 'LED off
WRITE 0, Z.HIGHBYTE
WRITE 1, Z.LOWBYTE
GOTO RAN
Dave
Bookmarks