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.

1
  • 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? Commented Mar 17, 2013 at 16:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.