AN544 Random Number Generator ASM Macro (for small pics) - Joe's Cat Website


This AN544 based Pseudo Random Number Generator (RNG) is here for anyone wishing to make use of it for their own projects.

This RNG uses less code and works on all PIC chips including the smaller pic10, pic12, pic16 versions.


GET THE FILE

You can download RandomM Macro, or you can copy and paste the code shown below into your ASM code editor.

;****************************************************************
;Random16 - 16bit Pseudo Sequence Random Number Generator (RNG).
;Based on Microchip's MATH routine found in AN544 and modified to
;fit all picchips including the tiny 12bit versions. This version
;uses 10 program words, making it slightly faster, but it needs a
;temp scratch pad to work.
;This RNG is based on linear shift register feedback. Sequence is
;generated by (Q15 xor Q14 xor Q12 xor Q3). Make sure the initial
;RandomV is not ZERO (AN544 suggests 3045h as a good choice).
;enter:	RandomV=16bit, temp=8bit
;exit:	w and temp are modified. RandomV updated to new value.
RandomM	macro	RandomV,temp
	rrf	RandomV,w	;Wreg = Q12
	xorwf	RandomV+1,w	;Wreg = xor(Q12,Q3)
	movwf	temp		;temp(bit3) = xor(Q12,Q3)
	swapf	temp,f		;temp(bit7) = xor(Q12,Q3)
	rlf	RandomV,f	;Wreg = Q14
	xorwf	RandomV,f	;Wreg = xor(Q15,Q14)
	xorwf	temp,f		;temp(bit7) = xor(Q15,Q14,Q12,Q3)
	rlf	temp,w		;cflag = xor(Q15,Q14,Q12,Q3)
	rlf	RandomV+1,f	;move bit7 to new bit0 and then..
	rlf	RandomV,f	;..rotate RNG value to the left
	endm
;****************************************************************

To use the code above, you need to provide 2 bytes of space for your RandomV value, and you need to have a temporary scratch pad file register available.

;variable definitions and file locations
w	equ	0
f	equ	1

	org	8		;start of ram area
RNGval	res	2		;RandHI:RandLO sequential value
temp	res	4		;4 temp scratch pad locations

;----------------------------------------------------------------
;routine example if you call RandomM many times in your code
RandomC	RandomM	RNGval,temp+3
	retlw	0

;----------------------------------------------------------------
;main code area showing how you can call routine, or use macro
	org	0
Start	movlw	030h		;initialize random routine....
	movwf	RNGval
	movlw	045h
	movwf	RNGval+1
;----------------------------------------------------------------

	call	RandomC		;if you use code often, do a call
	call	RandomC
	call	RandomC

	RandomM	RNGval,temp+1	;do macro if you use it only once
 

The code example above shows temp+1 and temp+3 just to show that you can build your macro with pointers to other specific locations if you want (temp was fine too).

If you use this improved RNG, I will appreciate a little credit for building this macro/routine and providing it here for your use/project (for example, in your source code comments, or even a web link back to this web page is great if you have a project that uses it that you want to show/demonstrate). Thanks.

Other PIC Related Pages On This Web Site
Home Page A/B Long Term Timer ASCII To MorseCode Clear RAM Macro
Cylon Eye Delay Macro 7.96Hz Sinewave Generator Software RS232
Table Array FSR Macro PicDis Disassembler Win At Casino Roulette


Copyright© 2000..2022 Joe's Cat
counter