Random:

    For a change, we'll not be exploring any new peripheral with this routine. This time we'll simply write an App routine to generate a 32-bit Prng number.    

This routine was "lifted" from the already mentioned application note "AN10913 DSP library for LPC1700 and LPC1300"  from NXP ( formerly Philips ) and it

makes a very  interesting use of the MLA (Multiply Accumulate) instruction, which allows it to execute in 5 machine cycles!  In comparison, the fastest AVR 

Prng routine, which I've ever used; takes on average 9 machine cycles and the 4 * 8-bit Prng registers must reside in the High register set to take account of

a quirk with the Atmel EOR instruction! The equivalent PIC routine is even slower!

clc                                                

          ror     RegD

ror     RegC

          ror     RegB

          ror     RegA

          brcc   PC + 6

          ldi     Temp,    $a6

eor    RegD,     Temp

          eor    RegC,     Temp

          eor    RegB,     Temp

          eor    RegA,     Temp

    On power up, the Prng routine needs firstly to be fed with a Seed generated by a relatively random source. The new STMF4 chip provides an interesting

way of generating such a number, with what is claimed to be an on-chip "True Random Number Generator". While this provides an elegant solution, it's

of no use to us, because we are instead using the Stm32 Mini development board, which is powered by the STMF103RB.

Other sources of useful randomness include radioactive decay, thermal noise, shot noise, avalanche noise in Zener diodes, clock drift, the timing of actual

movements of a disk read/write head, and radio noise. Fine if you like hardware circuits and you're just itching to solder and populate pcbs with Smd-parts,

but that's not the way to do it IMHO :-) There are 16 * 12-bit readily accessible channels inside the Adc1 peripheral and if we fill a 32-bit register bit by bit

with the least significant bits obtained by twice reading channels 0 to 15, then we should have a usable source of white noise and we could fill the 

32-bit Seed purely in software!  So here goes then ;-)

    We initialise the peripherals, as usual, by enabling the clocks for Usart1, Adc1, PortC, PortB, PortA and AF (12). Then we set the port pins corresponding

to Adc1 channels 0 -> 15 to Analog inputs (16, 22, 26). We then finally enable the Usart1 Tx output on PortA_9 to function as an alternate push-pull output (18).

The TruRnd routine is basically a modified version of the GetA2D routine we are already familiar with, the major difference being the bit stuffing of the Seed

register R12 (69,70,71). The noisy least significant bit of each A2D read is moved into the seed, R12 register by the LSL and OR instructions ( 69-71).

The 16 * A2D channels must be read Twice to give us the required 32-bits for the Prng. Using the Usart, we can send out the contents of R12 to a TTY terminal 

and we can observe the results at our leisure (85). The two second delay is appended after each output to allow us to clearly see what's happening (120).

A small caveat must be added here. The TruRnd routine is incredibly basic and further whitening of the Seed, by the usual methods, wouldn't do any harm ;-)

"32-bits and what do you get? Another day older and deeper in debt."

Download