View previous topic :: View next topic |
Author |
Message |
patel1790
Joined: 30 Jan 2012 Posts: 3
|
Flashing LEDs and selecting one at random HELP!!! |
Posted: Mon Jan 30, 2012 4:05 am |
|
|
Hi,
I am working on a project and one aspect of it is to make 4 LEDs flash and then select one randomly.
As I am new to CCS Programming I am confused at where to start and was wondering if anyone could help.
I am using a PIC16f819 and using ports A2, A3, A4 and B0 on the PIC.
Thanks in advance.
Kind Regards. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Jan 30, 2012 7:46 am |
|
|
The 'normal' place to start is to look at the fine examples that CCS supplies with the compiler and to read the manual.Examples are in the 'example's folder....the manual can either be downloaded from the website or press F11 when you have a project open.
Fairly sure there's a 'blinking LED' program in the examples, if not just do a 'search' in this forum as there are some here.
learn by doing...is the best education. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Jan 30, 2012 8:19 am |
|
|
Might this be a school project ??? |
|
|
patel1790
Joined: 30 Jan 2012 Posts: 3
|
|
Posted: Mon Jan 30, 2012 8:47 am |
|
|
Hi yes this is a school project, I will have a look around and see if anything comes up. Once I complete this I will post the code online for others to use.
Thank you |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Mon Jan 30, 2012 10:01 am |
|
|
It is interesting to think about the ways in which a genuinely random selection could be made, if the assignment is to turn on one out of four lights simply by starting the processor. As I understand it (I've never used it) the compiler's "random number generator" is actually a pseudo-random number which would give the same sequence every time it's used, unless it's seeded with a random number to start with--and where does that seed come from? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Jan 30, 2012 10:38 am |
|
|
The 'classic' way is to use something else that stays running.
If (for instance), you have something like a RTC chip, left running off 32Khz, then when you wake up, you can seed the generator from the time from this. This gives you a nice unpredictable start value to feed into the psuedo random number generator, and will give results that are as close to 'random' as you can get without spending a _lot_ of money.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Jan 30, 2012 10:46 am |
|
|
John P..
Seed can be a 'stored constant', same as PI is in 8052 BASIC.
WRT CCS C, you can code a short program using the function 'random', dump the listing, and figure out where the seed comes from.
Yes the sequence of 'random' numbers is the same.Which is good if you want to test your algorithm or math functions on known data!
Once you've proved it right, just 'reseed' the seed randomly for totally unpredictable numbers. |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Tue Jan 31, 2012 7:45 pm |
|
|
Er....yes. Reseed the seed randomly.
I could use a random number generator to do that, eh. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Feb 01, 2012 3:08 am |
|
|
Yes, but generating _random_ numbers is really hard. Normally done by having a particle detector listening to the decay of an atomic source.
If you try to seed the number generator from another number generator, again the sequence is totally predictable.
No standard computer gives you a true 'random' number generator. All are 'psuedo random' programs designed to give numeric sequences, that statistically behave 'like' random numbers, _except_ for the fact they are always the same, when the same seed is used.
Using something that is unpredictable to seed the generator is the way to go. On the PC, you normally seed the generator from the clock, as I already mentioned. Other routes have a key that you have to press to start the code. Trigger a timer when it is pushed, and when it is released take the count as the seed. Even a uSec change in the key time will give a different seed, so the result will be unpredictable. Or do something like generate a crude radio receiver (tuned coil, RF diode, protection circuits etc.), and feed this into an adc pin. Take a reading on the pin, and use this as the seed.
There are hundreds of ways of seeding the generator, but all must have something that is nice an unpredictable as the 'start'...
Best Wishes |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Feb 01, 2012 3:43 am |
|
|
Gentlemen, I suspect the "subtleties" of true rando number generation on computers is well beyond what the original poster was after Interesting as it is.
There are indeed many ways of generating random numbers, some derived from noise sources, the best, and most expensive from physical processes such as radioactive decay. There is also a lot of difference between seeding a software PRNG (psuedo random number generator) from an "unpredictable" source or even a true random source, and generating true random numbers on demand. Of course, all software PRNGs do not generate random values, no matter how randomly we seed them. Some generate more random-like values than others, and there has been a lot of research on that, and on what the characteristics of randomness are and how to measure them. Practically all C rand() functions are simple multiplicative congruential PRNGs and as such produce statistically poor, basically unrandom, numbers. There are better PRNGs ou there such as R250/521 (which is CRC like) and the Mersenne Twister, which is more cryptographic in character.
All this though is probably not what our OP is after. I suspect he's basically wanting to do a simple digital dice type thingy.
RF Developer |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Feb 01, 2012 4:45 am |
|
|
Yes.
However he wants a dice thingy, which starts up differently, so needs to find a way of generating some unpredictable seed for the generator.
What's easiest, will depend on the constraints on the hardware.
Best Wishes |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Wed Feb 01, 2012 9:49 am |
|
|
It's generally OK if there's an "on" switch which the user has to press, because then you can seed the random number with the time from power-up to button press, or time the button was held down. But what if the requirement is to start direct from power-up?
If the processor can detect that power is failing before the processor actually dies, or if there's a button that tells the processor to turn off the supply itself, then in the time available before the shutdown occurs, you could store some number based on the time that the device had been operating (powerup to shutdown) in EEPROM. At the next turn-on, use that value to seed the random numbers. I think that would work.
Edited to say, or really anything that happened in the previous operating cycle, such as the length of time a button was pressed. But the point is, save it for next time the unit runs. |
|
|
patel1790
Joined: 30 Jan 2012 Posts: 3
|
|
Posted: Mon Feb 06, 2012 3:03 am |
|
|
Hey everyone sorry for not relying sooner, but i managed to generate a code that does the led flashing thing, however the problem now is how to make one of the LEDs be selected at random! Below is the code I created:
#include <16F819.h> //Include 16F819 header file
#INCLUDE <STDLIB.H>
//Set internal oscillator, no watchdog, external MCLR, no brownout detection
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOBROWNOUT
#use delay(clock=4000000) //Set your clock speed
//Each of these must connect to two transistors, one pin for each current path
#define Left PIN_B0 //Left current path transistor PIC pin
#define Right PIN_A2 //Right current path transistor PIC pin
void Rotate(unsigned char Direction){ //Simply turns in one of two directions
output_high(PIN_A2); //for a single short period
delay_ms(100);
output_low (PIN_A2);
output_high(PIN_A3); //for a single short period
delay_ms(100);
output_low (PIN_A3);
output_high(PIN_A4); //for a single short period
delay_ms(100);
output_low (PIN_A4);
output_high(PIN_B0); //for a single short period
delay_ms(100);
output_low (PIN_B0);
}
void main(){
unsigned char c;
Rotate(Right);
delay_ms(100);
Rotate(Left);
delay_ms(100);
Rotate(Right);
delay_ms(100);
Rotate(Left);
delay_ms(100);
Rotate(Right);
delay_ms(100);
Rotate(Left);
delay_ms(100);
}
If anyone could give me help on how I could do this???
Thanks! |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Random selection |
Posted: Mon Feb 06, 2012 6:55 am |
|
|
Use the Code box for your program text.
I can't see the difference between your rotate left and right code (but that's not the primary issue).
Are you using real hardware or a simulator?
Loads of forum members already have given you answers on random Nos.
The way you are driving your LEDs does not make it easy.
I suggest a change.
Connect ALL your LEDs to the same port (Say B0, B1, B2, B3)
Then introduce an array of the form
Code: |
const int led_data [4] = { 1, 2, 4, 8 };
|
Sending the led_data in sequence to portB will light each each LED in turn.
Rotating left or right is then simply a matter of making your index increase or decrease.
You then pick a random LED using the built in random number routine (for starters, then you will realise what the previous answers were all about).
Mike |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 06, 2012 7:27 am |
|
|
Additional hints:
Add the NOLVP fuse. I have never seen anyone on this forum using a Low Voltage Programmer. Setting the NOLVP fuse makes pin B3 available for digital I/O and prevents the chip from resetting when an environmental pulse arrives at the B3 pin.
The C function for getting random values is called Rand(). It has limitations and workarounds as discussed above. Read the CCS manual. Also check the chapter on srand().
Major hint: Your application is not very different from an electronic dice... |
|
|
|