• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Programming question

Status
Not open for further replies.

Tako Kichi

New Member
Hi Folks,

I'm just starting out with programming and even though I have searched long and hard on the web I seem unable to find an answer to a question and I wondered if anyone on here might help me out.

I am working on a hobby project that calls for a pseudo random time delay of between 0.5 and 60 seconds before sending a HIGH signal to a pinout. This delay would need to be different each time the sequence is activated.

I have found lots of info on writing code for various timed delays and for generating random numbers for dice type games or digital readouts but nothing for a random time delay.

Does anyone know if this is possible (I am thinking it should be) and if so could you give me some pointers on how to write the code to get it to work. I am not looking for you to write the entire program for me but just help to point me down the right track.

Thanks,

Larry
 

andy.davies

New Member
it depends on what language you are using. i use ASM (assembler) if you find a random number genorator, and a well commented time delay, then you can insert the random number genorator into the delay routine:


Code:
DELAY1  CLRF    TMR0                          ;START TMR0
LOOPA   MOVF    TMR0,W                      ;READ TMR0 INTO W
             SUBLW   .32                           ;TIME - 32

             ;insert randon nuber code instead of .32

             BTFSS   STATUS,ZEROBIT       ;CHECK TIME - W = 0
             GOTO    LOOPA                       ;TIME IS NOT = 32
             RETLW   0                               ;TIME IS 32, RETURN
you need to place the random number into a register, and instaed of .32, put the register.

hope this helps

- Andy
 

laroche73

New Member
psuedo-random delay

Hi Larry, I saw your earlier post in another forum, but wasn't sure if you had access to micros or not. The circuit you have in mind could be done with analog or digital electronics. A digital approach is a little complicated for discrete MSI chips, but would fit well into a small micro or CPLD (~32 cell density). Here's what I'd do:

Emulate a fairly long maximal length linear-feedback shift register (LFSR) in memory or registers. In hardware, it's basically just a shift register with an XOR gate providing feedback from appropriately placed taps. The following app. note lists all the taps for LFSR counters of length n=3 to 168.

http://www.xilinx.com/bvdocs/appnotes/xapp210.pdf

You can emulate an LFSR in code with shifts and xors (you also need to shift across byte boundries with an 8-bit PIC). Choose a counter length of around 16 - 32 bits to get a balance between a fast code loop and a large enough count granularity (the 0.5 - 90 seconds is divided by the counter size (2^n) to get the output count resolution). The loop should execute relatively fast compared to the button response time, around a few hundred Hz or better. Start shifting when the button is pressed & debounced, stop when it's released.

Then code a down-counter using the number in the LFSR (you can use the same registers or memory locations) and stop at zero. Start with a fixed output delay of 0.5 seconds, and add a delay to each iteration of the down-counter loop. Choose the loop delay so that the maximum starting count value of 2^n results in a 90 second output delay.

Notes to speed up the LFSR loop: registers are generally faster than memory (may not apply to your PIC, it's true for some micro architectures), if you have some available to hold the count. Also, pick an LFSR length that only needs two taps, like n=17 or 31, to eliminate extra "xor" instructions from the code.

I'm sure there are many other ways to do this, it's the first thing that came to mind. Good luck!


(I'll post an analog alternative once I've had some time to try it out first)
 

Tako Kichi

New Member
Thanks to both you guys, I appreciate the assistance.

At least now I know I am not totally barking up the wrong tree and that the project is now a viable option and not just a pipe dream :D

I will still have to sort out triggering a LED to show the cycle is running, de-activating 2 of 3 push buttons during the delay cycle to prevent accidental premature sounding of the tones and assigning a sort of 'IF, THEN, ELSE' type statement to select one of two outputs to the tone generator based on the same random number (or it could even be another one) but that should be relatively easy compared with the original request.

I guess my next move should be getting the idea out of my head and onto paper/computer so that I can build a logic flow chart and block circuit diagram to confirm the design and then I can get down to sorting out the program (or am I going about this ass-backwards.....lol).

Thanks for your help,

Larry
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top