Back to PSI Science Lamp | Back to PSI Science Lamp Technology
Thanks to Helmut Schmidt & Robert Davies of Statistical Research Associates Limited for their support in the development of the Random Number Generator source code below.
'*********************************************************************************
'* Name : ESP.BAS *
'* Author : John Iovine *
'* Notice : Copyright (c) 2010 Images Scientific Instruments Inc. *
'* : All Rights Reserved *
'* Date : 7/8/2010 *
'* Version : 1.0 *
'* Notes :ESP Lamp RNG *
'*********************************************************************************
'*
DEFINE OSC 16 | 'Set oscillator to 16 MHz |
b0 var byte | 'counting variable |
x var byte | 'general variable |
w2 var word | 'random number variable |
TRISB = 1 | 'initialize ports |
b0 = 0 | 'counting variable |
PORTB = 2 | 'LED runover on startup |
Pause 550 | |
PORTB = 4 | |
Pause 550 | |
PORTB = 8 | |
Pause 550 | |
PORTB = 16 | |
Pause 550 | |
PORTB = 0 | |
Loop: | 'main counting loop |
if intcon.1 = 1 then display | 'interrupt polling, if positive edge, jump to display routine |
b0 = b0 + 1 | |
GoTo Loop | |
display: | 'main display routine - interrupt subroutine |
x = b0 / 4 | 'divide by four to find out integer quotient |
x = x // 2 | 'quotient modulo 2 to find out whether its odd or even |
b0 = b0 // 4 | 'modulo 4 on original counting variable to get random number from range 0 - 3 |
if x = 1 then b0 = 3 - b0 | 'reverse modulo in case of odd quotient |
w2 = b0 + 1 | 'range shift from 0 - 3 to 1 - 4 |
SerOut PORTB.5,6, [w2.byte0, w2.byte1] | 'serial out random number |
PORTB = 0 | 'turn off any LED to let user see new number generated |
Pause 150 | 'add pause to allow user to see new random number generated, if new and old 'number are same, user will see LED blink. |
IF w2 = 1 Then PORTB = 2 | 'Light LED corresponding to new random number |
IF w2 = 2 Then PORTB = 4 | |
IF w2 = 3 Then PORTB = 8 | |
IF w2 = 4 Then PORTB = 16 | |
b0 = 0 | 'reset counting variable |
INTCON.1 = 0 | 'reset interrupt flag |
goto loop | 'back to counting loop |