PIC Random Lookup Table

Discussion in 'Embedded Systems and Microcontrollers' started by mark squared, Oct 20, 2010.

  1. mark squared

    Thread Starter New Member

    Oct 20, 2010

    Hello, I am new to PIC programming and I could really use your assistance in my code. I would like to make a random output on Port D using only 5 bits. I attempted different ways to make the lookup table pick a different line in the lookup table but it would not work at all. I’m using a PIC 18F4550 microcontroller that outputs to Port D to turn on a LED's. I'm only using bits 0 to 4, 5 bits from the lookup table below. I would like to randomly choose a line in the lookup table and output it to Port D.
    Can someone help me in by modifying the code to randomly pick a line in the lookup table? I would greatly appreciate it. Thanks Mark Squared.

    GOTO Start

    Start clrf count ;set counter register to zero
    Read movf count, w ;put counter value in W
    call Table
    movwf PORTD
    call Delay
    incf count, w
    xorlw d'14' ;check for last (14th) entry
    btfsc STATUS, Z
    goto Start ;if start from beginning
    incf count, f ;else do next
    goto Read

    Table ADDWF PCL, f ;data table for bit pattern
    retlw b'10000000'
    retlw b'01000001'
    retlw b'00100010'
    retlw b'00100100'
    retlw b'00000000'
    retlw b'00000100'
    retlw b'00000010'
    retlw b'00000001'
    retlw b'00000010'
    retlw b'00000100'
    retlw b'00001000'
    retlw b'00010000'
    retlw b'00100000'
    retlw b'00000100'

    ;return; added for testing
    movlw d'250' ;delay 250 ms (4 MHz clock)
    movwf count1
    d1 movlw 0xC7
    movwf counta
    movlw 0x01
    movwf countb
    decfsz counta, f
    goto $+2
    decfsz countb, f
    goto Delay_0
    decfsz count1 ,f
    goto d1
    retlw 0x00

  2. t06afre

    AAC Fanatic!

    May 11, 2009
    Try Google and search for something like random number microchip
  3. wannaBinventor

    Active Member

    Apr 8, 2010
    Just a thought -- though you could probably look up and find a more efficient method.

    Make an astable multivibrator.

    Take an ADC reading across one of the capacitors -- 8 bit should be fine. Set the reference voltage so it's pretty much at the peak of the voltage on the capacitor.

    Do some math on that 8 bit reading, like dividing by 19 --- this way the whole number answer won't jump beyond the 14 listings you show in your LU table currently.

    If I'm thinking right, the RC time would let you theoretically figure the voltage on that pin at any point in time, but I think with fast enough oscillations, error of imperfect components, and sure to be out of sink timing with the PIC, the output would look very random.

    Triple check my thinking, but it should work.
  4. Markd77

    Senior Member

    Sep 7, 2009
  5. DonQ

    Active Member

    May 6, 2009
    If there is some operator intervention, say like hitting a keyboard, or a switch, just read the value off of a free-running counter running at warp-10 or so. There is absolutely no telling what the value will be when you happen to hit the switch.

    Even if you use a random number generator program, this is a good way to pick a seed value.