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!
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.
http://www.mikroe.com/forum/viewtopic.p ... 96&start=0