Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/15/06 11:50
Read: times


 
#112218 - Fibonacci/Galios Generator
Responding to: ???'s previous message
I take it that what you want to achieve is to have 16 LEDs that at least appear to flash at random. Is that right?

The algorithm you have seems to be some sort of 16-bit Fibonacci-type LFSR that you are only clocking once for each display cycle. In a Fibonacci-type generator, most of the bits simply shift one place with a new bit introduced based on some function of the states of the existing bits. If you clock such a generator only once, the resulting bit pattern will not look very random - you are only generating one pseudo-random bit.

In general, an LFSR should be clocked a number of times equal to or greater than the number of new pseudo-random bits that you need - 16 times in this case. Also, you should take care that the number of clocks is not a factor of the run-length of the LFSR.

The "randomness" of the LFSR is dependent upon the number of stages (bits) in the LFSR. More is better, but it depends upon what you are trying to achieve.

The "apparent" pseudo-randomness of an LFSR is generally increased by having a large number of taps, up to half the number of stages in the LFSR.

A Galois-type generator is simpler to implement in software and you can easily have as many taps as you like with little or no software overhead. In a Galois-type generator, all the bits are shifted and then if the bit that was shifted out is a '1' complement all the bits at the tap positions - a simple XOR with a mask value.

If you want apparently flashing LEDs, I would recommend:

* Use a Galois-type LFSR generator
* You can probably make do with a 16-bit generator
* Have at least half as many taps as stages (that is, 8 taps for a 16-bit generator.
* You may be able to get away with just one clock per cycle, but more would be better and 16 clocks would be ideal because you are diplaying 16-bits at a time.

If you want an off-the-shelf solution, use one of the 32-bit generators found at:

http://www.programmersheaven.com/zone5/cat.../32219.htm

Or just examine them for inspiration.

e.g.:
        MOV     R7,#16              ;Number of iterations.
                                    ;
?random_loop:                       ;
                                    ;
        CLR     C                   ;
        MOV     A,lfsr+0            ;
        RRC     A                   ;
        MOV     lfsr+0,A            ;
        MOV     A,lfsr+1            ;
        RRC     A                   ;
        MOV     lfsr+1,A            ;
        MOV     A,lfsr+2            ;
        RRC     A                   ;
        MOV     lfsr+2,A            ;
        MOV     A,lfsr+3            ;
        RRC     A                   ;
        MOV     lfsr+3,A            ;
                                    ;
        JNC     ?random_x           ;
                                    ;
        XRL     lfsr+0,#XMASK3      ;
        XRL     lfsr+1,#XMASK2      ;
        XRL     lfsr+2,#XMASK1      ;
        XRL     lfsr+3,#XMASK0      ;
                                    ;
?random_x:                          ;
                                    ;
        DJNZ    R7,?random_loop     ;
                                    ;

Where lfsr defines 4 bytes of immediate memory initialised to a non-zero value and the XMASKx constants define the taps, for example use the following values:
XMASK3 0xCC
XMASK2 0x4C
XMASK1 0x4E
XMASK0 0xCE

Then all you have to do, is to copy 16-bits of lfsr to your ports - it does not matter which 16-bits you choose, so you could do:
    MOV   P3,lfsr+0
    MOV   P1,lfsr+2

If performance is an issue, reduce the number of iterations, you may get acceptable results from a single iteration because the large number of taps makes the shifted relationship of the LEDs less obvious. Or, you could adapt the code to a simpler 24 or 16-bit generator - in which case you will have to work out your own values for the XMASKx constants.

List of 48 messages in thread
TopicAuthorDate
LFSR The unknown            01/01/70 00:00      
   As in a Random number generator?            01/01/70 00:00      
      Fine Tune            01/01/70 00:00      
         Did you Google?            01/01/70 00:00      
            Yes I did Google            01/01/70 00:00      
               OK            01/01/70 00:00      
               The structure of an lfsr            01/01/70 00:00      
   Random numbers            01/01/70 00:00      
   Look on Keil            01/01/70 00:00      
   LFSR testbench            01/01/70 00:00      
      trouble with LFSR's            01/01/70 00:00      
         randomness            01/01/70 00:00      
         Not Random            01/01/70 00:00      
            not pseudorandom            01/01/70 00:00      
   LFSRs            01/01/70 00:00      
   Flowchart            01/01/70 00:00      
      flowchart of implementing a pRNG???            01/01/70 00:00      
         the whole issue            01/01/70 00:00      
            what is a good pRNG            01/01/70 00:00      
               a fun story about pseudorandom            01/01/70 00:00      
                  seed            01/01/70 00:00      
                     you are forghiven, it's bedtime in slova            01/01/70 00:00      
                        you are right, time to go sleep            01/01/70 00:00      
                           Code change            01/01/70 00:00      
                              Read the App Note            01/01/70 00:00      
                  Been Done            01/01/70 00:00      
                     I don't understand            01/01/70 00:00      
                        Do it....            01/01/70 00:00      
                        It's there for you to use!            01/01/70 00:00      
               the best pseudo random number generator            01/01/70 00:00      
                  pseudo?            01/01/70 00:00      
                     well..            01/01/70 00:00      
   Pattern Generator            01/01/70 00:00      
      as has been said many times            01/01/70 00:00      
      Posting Code - Make it 3            01/01/70 00:00      
      homework            01/01/70 00:00      
         NOOB            01/01/70 00:00      
            thanks Andy...            01/01/70 00:00      
               Quite so            01/01/70 00:00      
                  "hosed"            01/01/70 00:00      
                     Gone tits up, or gone Pete Tong            01/01/70 00:00      
         Not my Homework            01/01/70 00:00      
            whoever said that the ability to write e            01/01/70 00:00      
            No problemo            01/01/70 00:00      
            What does "working" mean?            01/01/70 00:00      
      Fibonacci/Galios Generator            01/01/70 00:00      
         Thank you            01/01/70 00:00      
   the hunt for pseudo not being pseudo            01/01/70 00:00      

Back to Subject List