Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can Timer0 use for random number generator?

Status
Not open for further replies.

gordon the light

Newbie level 4
Joined
Feb 13, 2012
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,319
Hi, Sorry if I posted in the wrong thread.

I tried to use Timer0 to generate 2 random numbers, using PIC18f4520 and c18 compiler in c language. But is not working. Please help me.

Here is my program.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<p18f4520.h>
#include <stdlib.h>
    
void ISR (void); 
char i,start; 
 
#pragma code L_vector=0x18void L_vector(void){_asm goto ISR _endasm} 
 
#pragma code#pragma interruptlow ISR 
void ISR(void) 
{if(INTCONbits.TMR0IF){ 
TMR0H=0xC2;TMR0L=0xF7;i=rand();start=(i+TMR0H)%8;j=rand();end=(j+TMR0L)%8;}} 
 
void main(void){ 
 
 INTCONbits.GIEH=0;RCONbits.IPEN=1;INTCON2bits.TMR0IP=0;TMR0H=0xC2;TMR0L=0xF7;T0CON=0b100000101;INTCON=0b11100000;
 
 
while(1);}

 
Last edited by a moderator:

Possibly, but the number may be cyclic because the timer will update at a regular rate and will always have the same start value. In other words, you may get something which looks random but gives the same pattern sequence after each processor reset. If you are finding a truly random sequence I would suspect it is because 'i' and 'start' are not initialized.

What is it actually producing?

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top