Lets set this piece by piece.
1. RandomSeed in my FINAL example is a WORD (16 bits) because the RANDOM command works on a Word variable, so we need a WORD.
2. RandomSeed.Lowbyte only acts on the Lower 8 Bits (the LOW BYTE of the Word - yes it's in the manual). Just as RandomSeed.HighByte would act on the upper Byte (upper 8 bits of the word).
3. EEPROM locations are BYTE sized (8 Bits).
4. When the PIC powers up, RandomSeed=$0000.
5. 1st time around we go to EEPROM addess Zero, and load the LOWBYTE of the RandomSeed Word with the contents. The initial Contents will be zero.
6. In my example we then add 23 to it. RandomSeed is now 23.
actually where I wrote...
RandomSeed.Lowbyte=RandomSeed.Lowbyte+23
I could just have left it as...
RandomSeed=RandomSeed+23
because it makes no difference in the scheme of things as adding 23 WILL act on the Lowbyte anyway!
7. We save the lower 8 bits (LowByte) back to EEPROM. EEPROM location zero now has 23 in it.
8. We now use 23 to itterate our first RANDOM function.
9. We only need the bottom four bits which will give us a 0-15 range.
---
Next Evening... we get to step 5 above and EEPROM address had 23 in it. We add another 23 and save the value 46 to EEPROM. We now have the value 46 to seed our RANDOM instruction.
---
On the third evening the seed will be 69... and so forth.
---
However, eventually we will spill out of a BYTE (8 bits, but we will only save the lower 8 bits of the RandomSeed word (we don't need any more than that) to be our starting point for the next day.
----
Your example is erroneous because RandomSeed is a WORD and you cannot READ or WRITE a WORD to EPROM in a single operation... you have to split it into two bytes and save each byte into separate EEPROM addresses. But like I said above, in your application saving the just lower 8 bits suffices.
-----
Once you have a variable holding a value 0-15, you can easily convert it to a 16-bit variable for shifting out of your chosen I/O to your peripheral driver.
Bookmarks