1

There has been a lot of time since I don't post anything here. Today I came here looking for help to implement a timer in a PIC18F microcontroller. I want this timer to be used for using it as seed for the srand() function...but after searching a lot here I couldn't find a way to solve this problem.

Would you help me, please.

Thanks from a newbe.

  • Up to now, what I have its a Timer when the program is started. After I do some setup configuration, I ask the user to push a button. When he push a button, I recover the time from that timer and put it as seed in the srand. However, what I was interested in, was in not making the user press any button. Is there any possible way to do it? –  Mar 17 '13 at 16:32

1 Answers1

0

You do not seem in need of a very complex thing. No interrupts etc. Start a timer and just at an arbitrary point of code read timer value then feed it into srand.

 srand(aTimeFunctionYouWrite()); // random seed USE ONCE...

now your rand() accesses are randomized.

In the peripheral library of C18 you can find functions and definitions needed. You can use them via

 #include <timers.h>

And look at the doc folder for documentation.

Volkan
  • 2,212
  • 1
  • 14
  • 14
  • Hi! Thanks for the answer. Thats more or less what I have. I have a Timer that is started at the beginning of the program. At some point I ask the user to press a button. At that time I recover the value from the Timer and use it as srand argument. However, I was looking forward not to making the user press anything. So that TimeFunctionYouWrite(), is there any possible way to do it without user interaction?? By the way, I am using XC8 :) Thank you! :) –  Mar 17 '13 at 16:35
  • Here is a lead: http://stackoverflow.com/questions/1558321/how-do-i-generate-random-numbers-in-a-microcontroller-efficiently – Volkan Mar 17 '13 at 17:41