Continue to Site

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.

  • 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.

12F675 random delay ......

Status
Not open for further replies.

captnstoed

New Member
I have a 12F675 pic I am using to delay 8 seconds and pusle an output for 300ms. Can any one suggest an easy way to get the 8 seconds to a random time between 5 and 8 seconds? I am am using C code.
I am also looking to use a low out instead of a 5 v high. Is that possible or do I need another chip?
 
There are many ways to get a pseudo random number and hence a random delay. Most schemes would produce the same result every time the pic is reset. There are methods to get around this.

If you want truer random numbers then you need hardware. The intosc+timer1osc could be used. Charging a capacitor would vary dependent on temp and supply voltage etc.

As for inverting the output. Change your code to be active low! If you are using a built in function such as PulseOut then don't. Write your own.

More info about what you are trying to do would help.

Mike.
 
Sounds like a simple project, and perhaps a simple method would be a great idea,

Your compiler should have some sort of Random number function. Use its help file and search for "Random".

Usually the functions use a pseudo method, and will return a Random number from 0-65535.

So you want to create a value from 5.000 to 8.000? Assuming your compiler supports Floats (decimal numbers), you could create a rather simple random number generator (This would be the PIC Basic equivalent - I don’t use C sorry)

Code:
Device = 18F458
Xtal = 20

Dim Random_Number as Float

Symbol Min_Val = 5
Symbol Max_Val = 8

Cls

Print At 1, 1, "Number = "

SEED $0345                                                        ' Put the pointer in the middle of the random table.

Main:

	 Random_Number = Random                                   ' Generate a pseudo-randomisation on Variable.
	 
	 Random_Number = (Random_Number / 65535)                  ' Perform The math to create a number from 5 to 8.
	 Random_Number = Random_Number * (Max_Val - Min_Val)      ' Proton only allows upto 3 values per equation,
	 Random_Number = Random_Number + Min_Val                  '  thats why they are broken up onto seperate lines.
	 
	 Print At 1, 10, DEC3 Random_Number                       ' Display the value on the LCD
	 
	 DelaymS 250                                              ' Small delay
	 
	 Goto Main                                                ' Loop for eve
Click here to watch the Program in action.


Thats just throwing info on an LCD, to delay for that many seconds, simply scale up the value and use what ever the equivalent of DelaymS is, eg,

Code:
Random_Number = Random_Number * 1000
DelaymS Random_Number

The above was all written in Proton PIC Basic
 

Attachments

  • untitled.JPG
    untitled.JPG
    7.4 KB · Views: 363
Just on that, because the program will seed to $0345 every program restart, it will start at the same number, and generate the same sequence of random numbers.

There is an easy work around with this - simple save the random number to the PIC's EEPROM every now and then (not in a continuous loop! They are rated for 1,000,000 writes usually) eg,

Code:
Dim Write_Delay As Word
Dim New_Seed As Word


New_Seed =[B] EREAD[/B] 0
[B]SEED[/B] New_Seed

Write_Delay = 0

Main

     ' Program Loop
     '
     '
     [B]Inc[/B] Write_Delay
     If Write_Delay = 10000 Then
          Write_Delay = 0
          [B]EWRITE[/B] 0, [[B]Random[/B]]
     EndIf

     Goto Main

You don’t have to use two Word variables as I have done - it was just for ease of explanation.

This way, the PIC will initialize with a new pointer in the random table every time (providing it counted to 10000 last time). The Counter could be reduced/expanded depending on the app, but just remember that EEPROM's are rated to 1,000,000 writes in most cases, then things get hairy...
 
Hmm, I didn’t read your full post until now, If you tie my last two posts together, a couple of simple mods will produce something similar to what you want..

Code:
Dim Random_Number as Float
Dim Write_Delay As Word
Dim New_Seed As Word

Symbol Min_Val = 5
Symbol Max_Val = 8

New_Seed = EREAD 0
SEED New_Seed

Write_Delay = 0

High PORTB.0                          ' Make PORTB.0 an output and set it high

Main:

      PortB.0 = 1 

      GoSub Random_Gen

      Random_Number = Random_Number * 1000

      DelaymS Random_Number                              ' Delay for 5.000 to 8.000 seconds

      PORTB.0 = 0                                        ' Create a 300 mS low pulse
      DelaymS 300
      PORTB.0 = 1

      Inc Write_Delay
      If Write_Delay = 1000 Then
            Write_Delay = 0
            EWRITE 0, [Random]
      EndIf

      Goto Main
      

Random_Gen:
	 Random_Number = Random                                   ' Generate a pseudo-randomisation on Variable.
	 
	 Random_Number = (Random_Number / 65535)                  ' Perform The math to create a number from 5 to 8.
	 Random_Number = Random_Number * (Max_Val - Min_Val)      ' Proton only allows upto 3 values per equation,
	 Random_Number = Random_Number + Min_Val                  '  thats why they are broken up onto seperate lines.
         
         Return
 
Gramo,

Have you any suggestion for a true random number generator?

Mike.
 
Pommie said:
Gramo,

Have you any suggestion for a true random number generator?

Mike.

You need a hardware solution for that, a white noise generator or similar.

The pseudo method is fine, you just need to generate a random seed before you use the Random() function - which isn't as hard as it sounds.

I would suggest setting a hardware timer running as fast as it can at the beginning of the program, then have the program display "Press button to start", then when the button is pressed you read the timer and transfer it's value as the seed for the Random() function.

Because the timer is running very fast, and it takes differing times to press the button, the seed will be a reasonably random one.

BTW, not my original idea, I've been using it for decades, I can't even remember where it came from.
 
Nigel Goodwin said:
You need a hardware solution for that, a white noise generator or similar.

I believe I suggested that in the second post. My question was if Gramo had a suggestion as to how a true random number generator could be implemented. His Basic compiler could implement some real random functions I am not aware of.

Mike.
 
A quote from somewhere on the net;

"The problem with logic is that it is logical, and the outcome of any operation can be predicted. To make random numbers, we need a “maybe” gate; unfortunately, nobody knows how to make one that is truly random."


To create a true random generator - that shares no consistencies other than complete equal chance to generate any value between a minimum and maximum value usually revolves around interfacing with an 'unpredictable' source that has a true 'random' nature...

If you got any of that, then you will see where I'm coming from

TMR1 is a popular choice - but it is limited to how often you require samples.

Another common method is taking an ADC Sample from an RC circuit, but the issue here is that the charging time of the capacitor is not linear. This means your random numbers that are generated will share the same downfall.

There are a number of commercial products on the market that create white and/or pink audio noise, but the source of most use some sort of digital pseudo as the source - leading to the same issue we have at the moment.

If close enough is good enough - then don’t bother creating a truly random generator. Almost everything out there is susceptible to something - therein lay their weaknesses. To create a truly linear random generator would be quite a project, it’s anything but simple
 
Last edited:
Pommie said:
I believe I suggested that in the second post. My question was if Gramo had a suggestion as to how a true random number generator could be implemented. His Basic compiler could implement some real random functions I am not aware of.

Like I said above, you need hardware for true random numbers (as you yourself mentioned previously), a compiler can't create them, unless it has some kind of extra hardware you connect to your project.
 
Nigel Goodwin said:
Like I said above, you need hardware for true random numbers

Even then, how true are the samples?

The pseudo technique is more than adequate enough for most applications. You can take a 10Bit ADC sample from an RC circuit, and seed from that.

1024 random seeding locations is more than enough for many.


eg,
Code:
Random_Seed = ADIn 0

Seed Random_Seed

Random_Variable = Random

Now the table is seeded from 1 of 1024 possible starting locations.

Written with Proton PIC Basic
 
As this is a 12F675, I think one way I can see to get a true random number would be to use the internal RC oscillator and a Timer1 crystal. Use the difference between the clocks to generate a random number. As one of them is dependant on temperature then it should be pretty random.

Another method would use the WDT. Write a counter to EEPROM until the WDT times out. You would of course double buffer to eliminate write errors.

Mike.
 
Pommie said:
As this is a 12F675, I think one way I can see to get a true random number would be to use the internal RC oscillator and a Timer1 crystal. Use the difference between the clocks to generate a random number. As one of them is dependant on temperature then it should be pretty random.

Using a pseudo technique, with an innative seeding routing would require less peripherals and code overhead I think.

Another method would use the WDT. Write a counter to EEPROM until the WDT times out. You would of course double buffer to eliminate write errors.

Bad juju, especially depending on the number of times you require to use the technique :eek:
 
As I stated in the second post there are various pseudo random number generators. I suggest a possible real random number generator and everyone spouts up about pseudo!!

Oh well.

Mike.
P.S. As a former occupant of West Africa I would prefer you didn't mention JuJu.
 
Pommie said:
P.S. As a former occupant of West Africa I would prefer you didn't mention JuJu.


Heh, I'm sorry, I use it all the time (probably still will), but its by no means meant to be an insult/discrimination - just an expression of.. well.. bad juju/bad luck/leads to something bad/feels wrong/looks wrong/sums up a lot of inconspicuous meanings with 4 letters...

What does it mean in Africa?
 
JuJu is the West African equivalent of Voodoo. I wasn't offended and was being a little flippant when I asked that you didn't mention it. I was however rather surprised to see it mentioned here. I have seen some amazing things attributed to JuJu and the Maribou (witchdoctor).

Anyway, any more thoughts on random numbers.

Mike.
 
I've used TMR1 (and TMR2 where available) with an external switch. If this is not possible for any reason, the ADC can be used with a true random noise source (a resistor or a bipolar junction): you need to amplify noise with high gain AC-coupled stages. See an example on the PICLIST: http://www.piclist.com/techref/microchip/rand.htm.

Back to the OP's question, I think he can use software routines for his project. C compilers have these functions in libraries.
 
Last edited:
before one goes down the path of getting truly random numbers, one should examine the need. For many many applications, pseudo-random is quite sufficient. what is driving the need for true randomness?

By the way, many of the techniques mentioned can be shown to have non-linear distribution. Depending on the specific application, it may not be a problem but with out understanding the application, it's impossible to say.
 
philba said:
before one goes down the path of getting truly random numbers, one should examine the need. For many many applications, pseudo-random is quite sufficient. what is driving the need for true randomness?

By the way, many of the techniques mentioned can be shown to have non-linear distribution. Depending on the specific application, it may not be a problem but with out understanding the application, it's impossible to say.


We've already gone down that path and talked about all that on the previous page.

There are several methods to ensure a somewhat random starting point with the pseudo method that are also covered
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top