Andrew Warren [fastfwd at ix.netcom.com] of Fast Forward Engineering - San Diego, California says
Load a register called "RANDOM" with any non-zero value, then call this routine each time you'd like a new pseudo-random value:LFSR: RLF RANDOM,W RLF RANDOM,W BTFSC RANDOM,4 XORLW 1 BTFSC RANDOM,5 XORLW 1 BTFSC RANDOM,3 XORLW 1 MOVWF RANDOM RETLW 0Scott Dattalo says:
[with the double RLF at the start of this routine,] Andy is implementing 'roll left' where the most significant bit of RANDOM will get copied to least significant position. This is how it works. The first RLF will copy the most significant bit of RANDOM into the carry. What ever was in the carry prior to the first RLF will get copied into the least significant bit position - but we don't care. Also, since the destination is the W register, the variable RANDOM is unaffected. The second RLF repeats the same rotate operation, but this time the carry has been initialized to the MS bit of random. So this second rotate will copy the MS bit into the least significant bit. All of the other bits are of course shifted left one bit position. See?Or, if you prefer, you can use this routine (written by Marv Isaacman) instead:
MARV: MOVLW 01DH CLRC RLF RANDOM SKPNC XORWF RANDOM RETLW 0
, design by dr. Imre Bartfai, 8/30/1999
+--------------------------------------------------------------------- Vdd | 220k 220k 5V | +---\/\/\---+ +---\/\/\---+------+ | | | | | | | | |\ | | +------+ \ | EFT317 PNP +-----+----|- \ | | | / 220k | C | | | >-+----| |----+ | |\ \ | |/ \ +-------|+ / 47n | +-----|- \ | +--B| 220k / | | |/ | | | >--+ |\ \ | | 1/2 LM358 +----------|+ / | |E | | | | | |/ \ +----| |----+--+ + + + / 47k | 47n | | | | 1/2 LM358 \ \ \ \ \ \ | 4k7 / 1M / / 2k2 1M / / 2k2 + out \ \ \ \ \ | | | | | +-----------+-----+-----------------------+----+---------------- Vss Standard TTL can not be driven, but CMOS (and maybe LS) are o.k. The power supply must be filtered thoroughly due to the high gain!
See also: