Random Integer Generation using AVR

49 views
Skip to first unread message

itachi

unread,
Jan 28, 2010, 3:54:50 PM1/28/10
to openuniboard
Hello,
I'm currently trying to develop a memory game using AVR..
I've tried using rand() function in the stdlib.h header but it keeps
repeating the same number everytime i call it..
reason:it uses the same seed(I found it out ...if wrong please
correct)
solution:keep changing the seed continuously..to generate different
numbers using srand('some changing parameter')..function...
in our PC the seed can be our clock...
Can anybody please suggest any changing parameter in AVR...which i can
use easily..(without building a timer or taking in random noise
signals from ADC...)

In brief,All i want is to keep generating 10 numbers(integers) from 0
to 3...and build an array of those random numbers...the rest of my
game is very much done...although i'm using UART for interaction with
user....soon i'll try and interact using LCD display on board....which
i don't know how to do it btw :P Crying out loud for help!Jackie
Sir,Hardik Sir,Avinash Sir and friends...please help...also let me
know if i could come to powai office (or anywhere near to kharghar,new
bombay :P)

I had enrolled for embedded systems batch starting 11thJan 2010...
regards,
Sumeer Sinha

aditya_84

unread,
Jan 29, 2010, 12:14:52 AM1/29/10
to openuniboard
You could start the timer, and check the TCNTn value at random times
( let's say, when you call rand() ).
But I don't think I got this correct

" In brief,All i want is to keep generating 10 numbers(integers) from

0 to 3... ".

What exactly d'u mean by this ? You want to generate 10 integers
between what range ?
Hope the timer thing works, and please get back to us if it works or
not.

Regards,

itachi

unread,
Jan 29, 2010, 1:23:56 AM1/29/10
to openuniboard
Hi,
range is '0 to 3'.
eg.{0,2,3,1,0,1,3,1,0,2}
10 random numbers of range 0 to 3
Although I've tried using TCNT0 in normal mode...there was some
unknown glitch(may be some issue with datatype or my horrible method
of writing code :P ) and i got bored..
Will try again and post the code here soon.. :)

itachi

unread,
Jan 29, 2010, 7:42:13 AM1/29/10
to openuniboard
I think i got it...but I had tried this code earlier..
all i did was changed the seed datatype to int...is this called as
type casting..anyways the LED's seem to blink in a random order now :)

code:


#include <stdlib.h>
#include <string.h>
#include<avr/io.h>
#include<util/delay.h>

init_tim0(void)
{
TCNT0=0x00;
TCCR0=0x02;
}

int get_seed(void)
{
return (int)TCNT0;
}

void initrand(void)
{ int p;
p=get_seed();
srand(p);
}


int getrand(int range)
{
return (rand() % range);
}


void port_init(void)
{
DDRD=0x00;
PORTD=0xFF;
DDRC=0xFF;
PORTC=0xFF;
DDRE=0x00;
PORTE=0xFF;
}

int main(void)
{
port_init();
init_tim0();
initrand();
int p,q;
while(1)
{
p=getrand(4);
q=p;
switch (q)
{
case 0:
PORTC=0xFE;
_delay_ms(500);
PORTC=0xFF; _delay_ms(500);
break;
case 1:
PORTC=0xFD;
_delay_ms(500);
PORTC=0xFF; _delay_ms(500);break;
case 2:
PORTC=0xFB;
_delay_ms(500);
PORTC=0xFF; _delay_ms(500);break;
case 3:
PORTC=0xF7;
_delay_ms(500);
PORTC=0xFF; _delay_ms(500);break;
}
}
return 0;
}


There are some changes i did in the makefile as well to include
headers...just specified the path..
I hope i complete the project soon :)
Thanks for help!
cheers!

Surya

unread,
Jan 29, 2010, 10:33:40 PM1/29/10
to openuniboard
If you don't want to read / setup timer, you could read from the
EEPROM (assuming you have it in the IC) in serial order. That should
give you enough seeds for randomness.
Reply all
Reply to author
Forward
0 new messages