Code: Random rumber generator for dsPIC

General discussion on mikroPascal for dsPIC30/33 and PIC24.
Post Reply
Author
Message
OT
Posts: 581
Joined: 19 May 2005 05:08
Location: Fairbanks, Alaska

Code: Random rumber generator for dsPIC

#1 Post by OT » 10 Feb 2006 11:57

I found an example for random number generation on the PIC by XOR,
http://www.mikroe.com/forum/viewtopic.p ... +generator
using timer 2 to create random numbers, and adapted it for dsPIC. And it worked on first attempt! :D :D
It generates 16 bit Word type random numbers. The initiation of the timer is a bit different. I have not done any statistical testing, however it looks very reasonable. Comments/improvements appreciated.

Code: Select all

//**************************************************************
//Demonstration of how to get random number 0-655535
//from unused timer2. Tested on EASYdsPIC2 dsPIC 30F4013 at 80MHz
//Adopted from example for PIC.         O.T.  2006-02-09
//**************************************************************
program RandomWord;

var
  Wnum   : Word;
  txt    : String[20];

procedure RandomInit;
//Timer initiation for Random funtion
begin
  T2CON   := $8000;   // Timer1 ON, internal clock FCY, no prescaler
 //Alternative, bit 5-4 sets the prescaler to 1,8,64,256 :
 //T2CON  := $8030;   // Timer1 ON, internal clock FCY, prescaler 1:256
end;

function Random: Word;
//get random number 0-65535 from timer2;
begin
  asm
    swapf   TMR2, F
  end;
  result:= TMR2;
end;


begin       //Main
  Delay_ms(1000);

  RandomInit;                    //Initiation for random funtion

 //***LCD initiation***
  ADPCFG  := $FFFF;
  LATB    := $0000;
  TrisB   := $000F;
  TRISD   := $0000;
  LATD    := $0000;
  Lcd_Init(LATD.2, LATD.0, LATB);
  Delay_ms(100);
  Lcd_Cmd(LCD_CURSOR_OFF);//** Clear screen
  Lcd_Cmd(LCD_CLEAR);
  txt:='Random number:';
  Lcd_out(1,1,txt);

  //loop to display randum number
  While true do begin
    Wnum:=Random;         //Generate randum number from Timer 2
    WordToStr(Wnum,txt);
    Lcd_out(2,3,txt);
    Delay_ms(300);
  end;
end.
I also tried XOR's "pseudo random generator form the following thread, but not too surprising it could not be used as is (I am not into assmbler programming.)
http://www.mikroe.com/forum/viewtopic.p ... 96&start=0

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#2 Post by jpc » 10 Feb 2006 20:12

Hi OT ,

thanks for translating , actually this method is even better , you can use the approach even when the timer is used for somethin else. I even considered using signal from the outside world on top of this timer-based random number , sound , temparature or noise on analog input will introduce better random quality.
I just received my easydspic2-board this week , hope to use it for more then just random-generation however , it is usefull to have a good randomgenerator which in this approach consumes very few processor-cycles

fredmatic
Posts: 6
Joined: 27 Feb 2006 13:55
Location: Michigan, USA
Contact:

Random rumber generator for dsPIC

#3 Post by fredmatic » 24 Apr 2006 19:54

One of the problems with using a timer for a random number generated, is sooner or later there will be a predictable repeat. Using this method is ok when you need a mundane random number, however, if you need something a bit more random, use one of the unused analog inputs on the DsPIC chip. Depending on the chip of choice, (in my case, the 30F4013 w/12 bit analog input), you can simply hook up a pcb trace to the input, (w/diode protections to over/under voltage), and read the noise input. In most digital systems, if you use lousey PCB layout techniques for this input, noise detected is quite random.

For something even more random, use 2 inputs multiplying the 2 noises by each other.



Politics is legalized criminal activity.

Post Reply

Return to “mikroPascal for dsPIC30/33 and PIC24 General”