CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Flashing LEDs and selecting one at random HELP!!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
patel1790



Joined: 30 Jan 2012
Posts: 3

View user's profile Send private message

Flashing LEDs and selecting one at random HELP!!!
PostPosted: Mon Jan 30, 2012 4:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 7:46 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Mon Jan 30, 2012 8:19 am     Reply with quote

Might this be a school project ???
patel1790



Joined: 30 Jan 2012
Posts: 3

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 8:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 10:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 10:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 30, 2012 10:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 31, 2012 7:45 pm     Reply with quote

Er....yes. Reseed the seed randomly.

I could use a random number generator to do that, eh.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 3:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 3:43 am     Reply with quote

Gentlemen, I suspect the "subtleties" of true rando number generation on computers is well beyond what the original poster was after Cool 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

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 4:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 01, 2012 9:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 06, 2012 3:03 am     Reply with quote

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

View user's profile Send private message

Random selection
PostPosted: Mon Feb 06, 2012 6:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 06, 2012 7:27 am     Reply with quote

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...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group