Hi,

I wasn't quite sure where to post this, so here seemed the logical choice.

I'm using a PIC12F675 processor and am trying to generate random 'numbers'. In fact, all I want is a procedure to generate a single random bit (1 or 0) to light up random LEDs.

Here's the idea in pseudo code...


Call a procedure that generates a random bit and places it into w (using movlw)
Put the value of w into a named variable (movwf RAND)
Attempt to decrement the value (using decfsz). It's a bit of a hack, but it works. (pun not intended, hehe)
If it succeeds, RAND was 1, so call the procedure to turn on the LED. (call LED1on)
If it fails, RAND was 1, so it jumps the 'call LED1On' line and continues here. A 'nop' will suffice.
Repeat the procedure for LEDs 2 to 8.


Here's my LED1On code:

Led1On ; Turns on the LED
bsf STATUS, RP0 ; Bank 1
movlw TRIS_D0_D1 ; move predefined value to TRISIO
movwf TRISIO
bcf STATUS, RP0 ; Bank 0
movlw D1_ON ; move predefined value to GPIO
movwf GPIO
return


I've sorted out turning the LEDs on and the decfsz part, but I need to write a random bit generator.
How can I do this in ASM?
Posted on 2005-10-22 06:50:22 by Burningmace
Absolutely no guarantees, but this is what I've used in the past.
It's a 33 bit PRBS sequence generator (repeats after 2^33 interations)
and returns 32 bit random numbers in RNDN)1-RNDNO4

To generate the next number in the sequence, call RND
Then read as many bits as required from the 4 RNDNOx bytes.
Never write to the 5 RNDNOx bytes except to initially seed
the generator with a non-zero number.



'5 bytes of workspace needed to hold 33 bit random number
'must never contain all zeroes so make sure it's seeded
'with something non-zero before you start

        RNDNO1        EQU  2BH  ;RANDOM NUMBER
        RNDNO2        EQU  2CH  ;RANDOM NUMBER
        RNDNO3        EQU  2DH  ;RANDOM NUMBER
        RNDNO4        EQU  2EH  ;RANDOM NUMBER
        RNDNO5        EQU  2FH  ;RANDOM NUMBER




;GENERATE A RANDOM NO. 33BIT SEQUENCE AS PER ARC.
RND     MOVLW      .32           ;32 BITS NEEDED
        MOVWF       TEMP
RND1    MOVLW       08H          ;GET READY TO EOR BIT 20
        RRF         RNDNO5       ;GET BIT33
        BTFSS       STATUS,CARRY
        CLRW
        XORWF       RNDNO3,W     ;GET BIT33 EOR BIT20
        MOVWF       RNDNO5
        SWAPF       RNDNO5       ;GET THE NEW BIT INTO THE CARRY
        RLF         RNDNO5
        RLF         RNDNO1       ;ROTATE LEFT THE 33 BITS AND PUT NEW BIT AT BOTTOM
        RLF         RNDNO2
        RLF         RNDNO3
        RLF         RNDNO4
        RLF         RNDNO5
        DECFSZ      TEMP         ;DO NEXT BIT
        GOTO        RND1
        MOVLW       01H          ;CLEAR UNUSED BITS IN RNDNO5
        ANDWF       RNDNO5
        RETURN                   ;FINISHED


Editted:
a) to sort out the terrible formatting caused when posting to theis board
b) to suggest that, if you really only need 1 random bit, why not read the timer and take the LSB from that?

Paul.
Posted on 2005-10-22 09:46:54 by pdixon