Newbie has some questions


Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 44
  1. #1
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189

    Question Newbie has some questions

    Hi everyone,

    This winter I want to dabble in PIC programming to make a small hobby project that will randomly turn on and off 16 LEDs.

    What I want to do is use a 12F629 to drive a 74HCT154 4->16 encoder/decoder that will run the LEDs.

    As I understand it, I will want to have 5 outputs from the 12F that I can make either high or low. This doesn't seem to be a problem from what I read of the 12F.

    I have a couple issues since this is all new to me. First, I would like to use the PicBasic compiler due to costs. Since I won't be churning out project after project I really don't want to get into the higher cost of the pro version. Reading the command list of the PicBasic I think it should do what I want.

    The main stumbling block is the random factor.

    Initially my idea was to generate four random numbers which would translate to 0 or 1 to drive the high/low outputs. This seems simple enough, but since the random numbers are not truely random I'd like to add something else to that end.

    I did more reading that in order to have a more true random number an outside event would need to happen to seed the random number generator. This could possible be from something like a 555 timer. Did I understand this correclty?

    I assume the timer will be producing pulses which the 12F is going to use to create the random number. This is where I get stumped. There should be one more I/O port on the 12F that I could use after setting aside 5 for the multiplexer, but how do I use that input for the random number?

    I want to do this with as few external components as possible and even the 555 would need a couple resistors and capacitors (I don't want to use any variable resistors) In actual operation the pseudo-random generator probably would work fine. It's just my desire to make it better that is driving this idea.

    Further, regarding the pseudo generator. I don't fully understand what it is generating. Is it 0 or 1 or 1 to x with x being what upper limit?

    I haven't purchased anything software yet until I am sure I have a reasonable chance of making this work. It is completely new to me so please gear any answers for the uneducated!

    I've decide the actual source code for my idea should be fairly simple if I remember enough of BASIC from back in school many years ago.

    Thanks in advance.

    Bart

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hi Batman,
    i recently find this article on hardware radom generator bit

    http://willware.net:8080/hw-rng.html

    there's some interesting theory in. To read fom this generator you maybe able to use serial communication DEBUG statement. Place the result in a variable... maybe do some math to get random result in a range from 0-16 and that's it.

    That's in theory. never tested but ... suppose to do something.
    Last edited by mister_e; - 7th November 2004 at 01:00.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Thanks. I checked out the link and while I can say that I have some idea of what they are saying generally it is beyond me.

    From what I can gather, though, is that it would have too many problems for me.

    Too many components.
    Wants 18 volts and I have 5 to offer.
    Uses commands that aren't in PicBasic.

    I do think I need to read the input (obviously), but what to do with it? A 555 timer could send pulses. Probably something else could to, but that be simpler? Regardless, I don't know what to do with said pulses.

    I'm sure my first attempt won't be exactly what I have in mind. A bit of a steep learning curve for me.

    Bart

    P.S. I picked the 12F629 pic just because it is small. It may not be the best choice. I also am looking at 16F676 to have more I/O options if needed as a way to the end of the random number puzzle. As long as the chip is fairly small I'm interested in suggestions there too.
    Last edited by bartman; - 7th November 2004 at 05:09.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    The easiest and simplest way to start with a Random Number seed is to employ the services of the User (unbeknown to them of course).

    If you have an On/Off BUTTON, then the PIC powers up very quickly when the ON Button is pressed, and starts counting how long the Button remains pressed until the user releases it. Counting from 0-256 continuously with fosc at 4MHz, there's little chance the User would be able to throw the result even if they knew the Random seed was linked to the ON Button.

    The component count in this instance is minimal and you have Push-Button On/Off for your device, which is easier and neater to install than a actual change-over switch (also allows you to have software control of Power-Off if needed).

    Melanie

  5. #5
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Thanks. I did read about this method as well. The project is powered up via a timer (one of those garden light transformers that has been converted to run DC current) and from there it does its job of randomly turning on one of the 16 LEDs. It is for a lighting effect so there is no human intervention and, at least no way I can think of, no way anyone can trigger the event short of adding more components.

    Bart

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You've got 16 lights you want to randomly switch.

    Chose a PIC with EEPROM. Fill the EEPROM with a random selection of numbers, and you're away. Nobody's ever going to guess you're recycling a 128 or 256 step sequence - just not humanly possible to remember more than 20 or 30 steps - if anybody can, and can do say 50 numbers repeatedly, contact me - there's a Cassino I wanna hit...

    Melanie

  7. #7
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    I'd go for the casino idea... love Vegas!

    You are just way beyond my understanding!

    Really, though, you are right that no one is going to notice any sequencing as it is happening. I could just use the Random command to end up with the same thing. I just don't like that on start up it will always start with the same thing.

    Let me try and describe my project better.

    There are 16 LEDs that will always be on EXCEPT for one that will randomly "blink" off then come back on. Repeat with another random LED and go on this way forever.

    Using the 74HCT154 multiplexer makes the job of holding the LED's on a blinking just one at a time easy and it requires 5 inputs in different combinations of high and low to do that. Sounds simple enough at this point.

    So using RANDOM to come up with 5 combinations of "0" and "1" in itself is simple using > and < to set the pin. The circuit "should" run away happily forever with not much coding at all except that on each start up it starts the same way and would be noticable until some point where the viewer lost track. Sort of like Pi. We can all remember the first few digits then we just sort of don't pay attention after that.

    To get away from this problem I thought I should somehow seed the generator with an outside event. Not because it totally can't work any other way, but because it is neat to come up with a solution that my limited knowledge can carry out.

    I've been reading the manual for the PicBasic and trying to understand "Free running" 555 timers and how I could make the two work together under PicBasic and some type of PIC controller. Right now it is all somewhat above me.

    Thanks.

    Bart

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    How often does the PIC get turned on? Each evening, or every time the lady of the house strips off to jump in the pool, or... ?

    Your 555 is total overkill.

  9. #9
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Each evening after dark.

    Bart

  10. #10
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    OK, so lets assume you fill all your EEPROM starting from location 1 to the end (let's pretend its address 127) with a Random sequence of Numbers from 1 to 16... EEPROM address zero is reserved.

    Data @1,13,11,2,5,3,7,9,8,14,2,16 etc etc you get the drift...

    PIC powers up, reads EEPROM location 0..., adds ONE, and stores it back to location zero...

    READ 0,DataA
    DataA=DataA+1
    If DataA>126 then DataA=DataA-127
    WRITE 0,DataA

    This ensures on the next power-up, we have an incremented number... now we read the memory location kndly provided...

    READ DataA,RandomValue

    hey presto... a totally random value to start todays sequence with... you don't need to add by ONE... why not add three or seven or forty-two...

    Now... all you do is for this evenings sequence just continue reading sequentially from that point (when you exceed 127, you rotate back to address 1 and continue). Totally random, no extra hardware, cheap, cheerful, easy, and Melanies comission cheque is in the post...

  11. #11
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Actually, here's a real easy way to get a fresh seed for your Random function every time you power up... no description... see if you can figure it...

    RandomSeed var Byte

    PIC powers up and runs this ditty...

    Read 0,RandomSeed
    RandomSeed=RandomSeed+23 ' best with any ODD number here
    Write 0,RandomSeed

    RandomSeed now contains a fresh seed every time you power up... three lines of code replaces your NE555 and a heap of headaches.

    Since the EEPROM has only got a lifespan of around 100,000 writes... let me see that means your product should die sometime after 270 years - I trust the warranty would have exprired by then...

    Melanie

  12. #12
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Actually, I think you are describing what I was thinking after reading the whole eeprom thing. I still hadn't figured out how to really use the new line of thought.

    RandomSeed is set as a Byte variable

    We read from address 0 of the eeprom then add something to it and save it for next time. This keeps it always changing.

    I'm not sure how to implement that yet though.

    First, how does it get a value to begin with at address 0?

    More code would be required to keep the value less than 256 correct?

    Now, I still want to do this with nothing but 0 and 1 as the values so I think I still need to use Random in conjuction with whatever is in eeprom. I don't fully understand the workings of Random. As I get it currently it is just blazing along on a pre-determined course and every time Random is invoked it picks off a number which I can do math on to make 0 or 1. When we talk about seeding it are we just talking about giving it a new starting point?

    I was checking out the data sheet on the 12F629 which is still my preference and see that it has 128 bytes of eeprom so if I can see this idea clearly and if I wrap my head around it correctly it would be much better than the 555 in the mix I agree.

    Bart

  13. #13
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Ignore my first example - it's actually unnescessarily complicated. Chosing the second example only...

    Pic Powers Up and executes this code...

    RandomSeed var WORD
    LightSelector var BYTE

    Read 0,RandomSeed.Lowbyte
    RandomSeed.Lowbyte=RandomSeed.Lowbyte+23
    ' best with any ODD number here
    Write 0,RandomSeed.Lowbyte

    (The starting value in EEPROM at location zero is irrelevant. It can itself be Zero).

    At this point we have a WORD value of 'something' (which is different each time the PIC powers up)... we now use this to seed the Random sequence for this evenings light show...

    Loop:
    RANDOM RandomSeed

    Now, we've got a 16-bit random value... but we're only interested really in the bottom 4 bits...

    LightSelector=RandomSeed & $000F

    Lightselector now contains a value between 0 and 15 which is used to select the next light to sequence...

    Gosub BlinkyLight

    This now Blinks our Light for whatever length of time, then...

    Goto Loop

    we loop around for the next Random light to be selected.

    Tomorrow, you will notice we start with a new Random value in EEPROM to which we add our constant and store. Therefore tomorrows Random sequence will be seeded with a DIFFERENT start value, so the entire light show will be different. Ad nauseum each night forever (or until EEPROM location zero wears out)... but since the EEPROM is only written to ONCE each evening, we've got 270 years of evenings to play with...

    This pretty much is the core of your ENTIRE program just handed to you - there is no more, this is it in it's entire complexity. Just add your I/O and your Config Fuses and you're done.

  14. #14
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Well to you it might be the whole core, but to me it's just so much confusion! :-)

    I am sure it is what I want, but until I understand it it's still like a puzzle waiting for me to figure it out.

    We're still not dealing with 0 and 1 as the random numbers. Generating something from 1 to 16 means 16 lines of IF...THEN or some such to branch. By using 0 and 1 I can set each of the four output pin as high or low then use a fifth pin that "turns on" the 74HCT. Smaller program.

    That doesn't mean the eeprom idea doesn't still work. I just can't understand it all yet to see how it works so I can use it. It's all giving me a headache! I am going back to study further your code.

    Thanks.

    Bart


    P.S. I'm still working on understanding the fuses thing. I gather I need a resistor from the +5 to MCLR pin and the programmer has a box to either check or uncheck MCLR, but there is nothing in the help file that explains when to check or uncheck. I also read that MCLR doesn't need anything and one circuit diagram I saw doesn't have the pin going anywhere.
    Last edited by bartman; - 7th November 2004 at 22:18.

  15. #15
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Okay. I see the problem here. I am trying to see this as "the big picture" when really I need to focus on the blocks that built it. If I can understand those then I can get this.

    Let’s just look at RANDOM and how you showed me to use this. Here is my take on it:

    RandomSeed var WORD

    Read 0,RandomSeed
    RandomSeed=RandomSeed+23 ' best with any ODD number here
    Write 0,RandomSeed

    Loop:

    RANDOM RandomSeed


    (Now we have a random number in RandomSeed between 1 and 65525 correct? Let’s say this number is 3245 just for an example. Here is what I wasn’t getting and I had to read the manual again to hopefully get this. The "Seed" is based on whatever is in that variable RandomSeed. A calculation is performed on that variable to get the random number. If we didn’t use eeprom the seed is really zero. The first time we use it the seed in eeprom it is also zero. It changes after that. Am I even close to correct on this? That is why it will always produce the same sequence each time unless the variable that is used as the seed is changed.)

    Then, if I did this instead:

    RandomSeed var WORD

    Read 0,RandomSeed
    RandomSeed=RandomSeed+103 ' best with any ODD number here
    Write 0,RandomSeed

    Loop:

    RANDOM RandomSeed

    (The random number generated would be different if all things were equal than the first example of 3245 correct? It is that way because I used a different factor to add to the seed - right?)

    I’m hoping I am explaining myself correct because I do not want to get too deep in mud. I just want to make sure I understand how RANDOM really works. That is first then I can address how this gets deeper. I’ve been going over your code word for word to figure it out.

    Then to address part of your code that is more confusing.

    You define:

    RandomSeed Var Word, but then you use RandomSeed.Lowbyte later. What is this "Lowbyte" part of this? I don’t see any reference to this in the manual.

    Bart

Similar Threads

  1. Olympic Timer
    By Melanie in forum Code Examples
    Replies: 47
    Last Post: - 17th September 2008, 19:46
  2. Analog port newbie questions
    By rngd in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 11:38
  3. Newbie: Temperature measurements
    By Budda in forum General
    Replies: 10
    Last Post: - 30th March 2007, 09:56
  4. Hi Everyone! Some newbie questions :-)
    By guest_05 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 28th October 2006, 22:24
  5. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts