Shop OBEX P1 Docs P2 Docs Learn Events
555 timers and counters — Parallax Forums

555 timers and counters

MatthewMatthew Posts: 200
edited 2005-02-12 17:37 in General Discussion
So, if I understand correctly, 555 timers give off evenly spaced high pulses, and the spacing can be controlled by the resistors and capacitors connected to it. Right?

Now. I'm not exactly sure how counters work. If I want to attach a counter to the 555, it should keep track of how many pulses it has read. How does the stamp read how many pulses the counter has read from the 555?

I need a counter that will count pulses that are 0.5 seconds apart, and atleast 240 pulses (2 minutes worth of pulses).

Any ideas?
«1

Comments

  • MacGeek117MacGeek117 Posts: 747
    edited 2005-01-23 00:08
    Matthew, you can control the frequency of the circuit through the resistors, but you
    can't control the duty cycle.
    By the way, what are you trying to do?
    bugg

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    So many projects, so little time.
  • MatthewMatthew Posts: 200
    edited 2005-01-23 01:13
    What's the difference between frequency and duty cycle?

    I'm trying to create a program that begins after a trigger is released. The program will control a motor to turn either one way or the other. Based on how long it has been since the trigger was released, the objective of the program changes. For example, the trigger is released, and I have 3 sub programs. Depending on the amount of time since the trigger has been released (0-30secs, 30-60secs, or 60-90secs), the program will go to one of the three sub programs.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-01-23 02:26
    The frequency is the amount of time it takes for a full cycle (one low portion and one high portion, this is actually the period. Period = 1/frequency), the duty cycle is the portion of that cycle that is high. A 50% duty cycle the high portion is equal to the low portion. A 25% duty cycle the cycle is high for 1/4 of the total time of the cycle and the cycle is low for 75% of the cycle.

    What platform are you using? Basic Stamp or SX? If you are using a Basic Stamp are you doing anything else while you are doing the motor control?
  • MatthewMatthew Posts: 200
    edited 2005-01-23 03:09
    Paul,

    Thanks for the clarification, that makes a lot of sense.

    I'll be using a Basic Stamp 2 for this. Pretty much, the stamp will first take in distance from an ultrasonic sensor and save the distance as a variable. Depending on the distance, the motor will turn one way or the other to compensate and hopfully change its location so that it is distanced correctly. After sending a pulse to an electronic speed controller to control the motor, the stamp will send a pulse to a continuous rotating servo, which will turn it one way or the other. After that, the current distance is displayed in the debug menu, then the loop repeats itself.

    So here's what it should look like:

    -Timer triggered

    Sub1: If time is between 0-30 seconds
    -check distance and save as variable
    -if distance is 5'+, turn motor right (for one pulse)
    -elseif distance is 5'-, turn motor left (for one pulse)
    -endif
    -if motor turned right, turn servo right (for one pulse)
    -elseif motor turned left, turn servo left (for one pulse)
    -endif
    -loop

    Sub2: If time is between 30-60 seconds
    -check distance and save as variable
    -if distance is 10'+, turn motor right (for one pulse)
    -elseif distance is 10'-, turn motor left (for one pulse)
    -endif
    -if motor turned right, turn servo right (for one pulse)
    -elseif motor turned left, turn servo left (for one pulse)
    -endif
    -loop

    Sub3: If time is between 60-90 seconds
    -check distance and save as variable
    -if distance is 15'+, turn motor right (for one pulse)
    -elseif distance is 15'-, turn motor left (for one pulse)
    -endif
    -if motor turned right, turn servo right (for one pulse)
    -elseif motor turned left, turn servo left (for one pulse)
    -endif
    -loop

    Hope that was clear enough.
  • paysonbadboypaysonbadboy Posts: 81
    edited 2005-01-23 04:27
    Hey. New here to the board.

    I just happened to be using a 555 earlier.

    Now from what I've experience the duty cycle is hardly ever EXACTLY 50%. sometimes it's a ways from 50%.

    NOW however when looking up ways to make a PWM from a 555 I HAVE done that for a past project.

    taking the pulse from a 555 (not from pin 3 out but from the charge on the cap providing a sawtooth waveform) that is then fed to one of the inputs of a comparitor circuit.
    using a center tapped pot to vary the VOLTAGE to the other input of the comparitor you get square waves from the comparitor output. Varying the voltage via the pot to the comparitor varies the pulse width from almost nothing to about 100%. THAT actually worked when i tried it.

    I'll have to look for the schematic but you may figure it out from what i said.

    i don't have a good scope. i have a POS antique HeathKit from when i was younger that I fire up every now and then. No accuracy as to what voltage or exact timing, but I CAN see the pulses with it.

    I think i need to buy one of those USB o-scope programs.

    Anyway, hope that helps.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-01-23 17:10
    Ok let me try to paraphrase your idea to see if I understand, tell me I'm right or clarify, then I can provide you some direct guidance. You have 3 subroutines, each of which is an infinite loop consisting of a closed loop feedback (CLF) system (take measurment, adjust output based on measurment, repeat). Based on a timer you wish to switch from one CLF to another. Do I understand you correctly? Also how important is it to switch exactly at a certain time (is being off by a few seconds important?) One final question, does your distance measurment take a variable or fixed amount of time? If its variable can you make it fixed by pausing the difference between the time it took to do the measurment and the maximum time it would take to do·any measurement?

    Post Edited (Paul Baker) : 1/23/2005 5:22:19 PM GMT
  • MatthewMatthew Posts: 200
    edited 2005-01-23 18:26
    Paul,

    Yes, you've got it right.

    It's very important the switches are done at the correct moment (as precise as possible, atleast 0.1 sec).

    I'm pretty sure the distance measurment is fixed. The ultrasonic sensor I have automatically gives off a voltage reading between 0-5v depending on the distance at all times. I'll have an ADC connected to it, so the ADC should be constantly changing (whenever the ultrasonic sensor is changing). When it comes time, the Stamp will take one reading and save it as a variable. When it's done with the loop, it will take another one.

    Thanks a bunch Paul.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-01-23 19:29
    Ok one of the problems with the 555 is its level of accuracy is limited. This is especially true when you take in environmental effects such as temperature. This is because the timing element of the 555 is a resistor and a capacitor, each of which has thier own tolerance (% deviation from the listed value) and thier own temperature coefficients. This is why RC circuits are used to clock microprocessors only when timing is not critical (cystals and other means of timing are used when timing is more important). You have two options availible to you for a more precise measurement, internal timing and a real time module.

    A real time module is an external clock circuit (like the one at www.sparkfun.com, type Comp-RTC into the search box) that one uses a I2C interface, I don't know if the Basic stamp can do I2C (I use the SX) (I think you might be able to, achilles03 would be the one to ask), but you should be able to find a similar module the basic stamp is compatible with (Id check Parallax first).

    Alternatively you can do internal timing, this is where you calculate the time it takes to complete each iteration of a loop for each of your subroutines. It appears that each should take the same amount of time, if not place an appropriate pause to make each of them take the same amount of time. Then after each loop iteration, increment a counter, after the appropriate number of counter ticks happen you switch the routine you call. You may want to extend the time each loop takes to make the calculation easier (ie if you find each loop iteration takes 0.097 seconds pause for 3 milliseconds to make it 0.1 seconds, then when you counter reaches 300, 30 seconds have passed and its time to switch to the next routine. Depending on how you organize the program, you may need to account for the time it takes the main routine. If you place your infinite loop in the main routine (so that when you call a certain subroutine, it returns after completion of the loop iteration) you'll have to account for the intructions the main loop executes.

    One thing you could do to simplify the calculation of the timing is just to have 1 loop. Since·the only difference between each of you CFLs is the distance threshold,·make that threshold a variable. Heres an example, using as much of your own words as possible:

    (Initialization portion)
    -set Time Frame variable to 1
    -set Distance Threshold to 5'
    -set Time Counter to 0

    (Loop portion)
    -check distance and save as variable
    -if distance is greater than Distance Threshold, turn motor right (for one pulse)
    -elseif distance is less than Distance Threshold, turn motor left (for one pulse)
    -endif
    -if motor turned right, turn servo right (for one pulse)
    -elseif motor turned left, turn servo left (for one pulse)
    -endif
    -pause for a bit
    -increment Time Counter
    -if Time Counter is less than 30 seconds
    -loop
    -endif
    (Time Frame change portion)
    -increment Time Frame
    -add 5' to Distance Threshold
    -set Time Counter to 0
    -loop

    I did not include code for an end condition (this code will set the threshold to 5',10',15',20',...) If you want to restart (5',10',15',5',10',...) you would jump to the Intialize Portion when you detect an end condition. If you want it to stop altogether you would enter an infinite loop that does nothing.

    Hope this helps,
    Paul

    Post Edited (Paul Baker) : 1/23/2005 7:36:45 PM GMT
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-23 20:05
    You can do real time with the BS2 using the DS1302 chip. Parallax publishes an app note for it at www.parallax.com/dl/docs/prod/appkit/ds1302rtc.pdf.

    Jim
  • MatthewMatthew Posts: 200
    edited 2005-01-23 23:06
    Paul,

    To me, the internal timing seems easiest. That will just take time to calculate.

    I've read the appnote for the DS1302, and I still don't understand how the stamp reads time from it. Is there a way of just starting the clock when needed, and only reading the seconds portion?

    Thanks
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-01-24 00:00
    Matthew said...
    I've read the appnote for the DS1302, and I still don't understand how the stamp reads time from it. Is there a way of just starting the clock when needed, and only reading the seconds portion?

    The DS1302 maintains the time internally. It is always running completely independent of the stamp. It is usually battery backed so that when power is shut off it keeps the time. You set it once by sending a datastream from the stamp, then forget it. To get the time, you send it a command and it replies with the requested data. Here is the code chunk from their sample app that reads the time:

    ReadRTCBurst:
    'Read all time-keeping registers in one burst
    HIGH RTCCS
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Day,Year]
    LOW RTCCS
    RETURN
    
    



    There different commands that are listed in the data sheet, including retrieving only the seconds.

    I think that in order to meet your 0.1 second accuracy you'd have to be reading the clock quite regularly to catch the second rollover.

    Jim
  • paysonbadboypaysonbadboy Posts: 81
    edited 2005-01-24 00:09
    Could you just use an external clock sourse on one of the unused I/O pins of the Stamp and not allow each subroutine to execute until it GETS that pulse each time?
    Maybe then it would be just one execution per half second. That should only add one (or two) commands per subroutine.

    Just an idea.
  • MatthewMatthew Posts: 200
    edited 2005-01-24 04:02
    Ah hah. Thanks Jim. I'm going to try that this week during class. I'll let you guys know how it turns out.

    Paysonbadboy, I'm a semi-newbe, so I'm not sure.
  • paysonbadboypaysonbadboy Posts: 81
    edited 2005-01-24 05:34
    Me too. I just got my Controller this week but been hard at it :-P
    trying to remember the BASIC programming I played with 20 yrs ago on the first home computer I had.
    It's amazing how something like that starts coming back.
  • achilles03achilles03 Posts: 247
    edited 2005-01-24 13:47
    Matthew,
    Just curious, but what/how far are you trying to sense with the ultrasonic sensor?

    Dave
  • MatthewMatthew Posts: 200
    edited 2005-01-26 21:28
    Trying to find out how far an object is from the ground. The sensor is supposed to measure up to 40'.
  • achilles03achilles03 Posts: 247
    edited 2005-01-27 18:32
    If this is for your rocket project, I think all you'd need would be an accelerometer (like the ADXL150) and some sort of timer.· All you need is to program the BS2 with·a pre-calculated table based on height and elapsed time.· Based on the·estimated height (from the accelerometer) and how much time has elapsed from launch, have the BS2 determine when the parachute should be ejected.

    For instance,·let's say·it takes·8 seconds for the rocket to reach apogee.· The BS2 can·predict its altitude at apogee based on the accelerometer's measurements.··Every second, have the BS2 determine how long it'd take to hit the ground if the parachute were ejected at that altitude.· When the value it estimates plus the elapsed time = 60, then blow the chute.· You could also do this with a pressure sensor instead of the accelerometer.



    Dave
  • MatthewMatthew Posts: 200
    edited 2005-02-01 07:42
    Jim McCorison said...
    ReadRTCBurst:
    'Read·all·time-keeping·registers·in·one·burst
    HIGH·RTCCS
    SHIFTOUT·DTA,·Clk,·LSBFIRST,·[noparse][[/noparse]%1\1,BrstReg\5,%10\2]
    SHIFTIN·DTA,·Clk,·LSBPRE,·[noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Day,Year]
    LOW·RTCCS
    RETURN
    Jim, does this subroutine save the corresponding values for the variables: seconds, minutes, hours, date, month, day, year?

    Also, why does the module need a 32.768 kHz crystal? Is this a common piece and can I get it from Radio Shack?

    Thanks.
  • ForrestForrest Posts: 1,341
    edited 2005-02-01 11:35
    Don't expect to find crystals at Radio Shack - they've been reducing the electronic components they carry over the past few years. The only crystals they currently carry is for RC cars operating at 27 MHz. For electronic components - I'd recommend www.mouser.com and www.digikey.com Note Digikey has a surcharge if your total order is under $25, while Mouser doesn't.
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-02-01 16:39
    Matthew,

    The specific code sample returns the variables into the byte variables Seconds, Minutes, etc. You can read the whole document, correction, you _should_ read the whole document. You can find it at www.parallax.com/dl/docs/prod/appkit/ds1302rtc.pdf. The DS1302 can be told to return all it's data items, as in the sample code, a subset, or just one. It's pretty flexible.

    As for the crystal, Forrest summed it up pretty good. Radio Shack is stocking less and less of the kind of products that we need. It has basically turned into a cell phone and satellite dish store. Sad. I remember when I was much younger spending hours pouring over their catalog whenever it arrived in the mail. Now it is Mouser, Digikey, and a bunch of other web sites instead. But you just can't pop down to Mouser to pick up a single part. Or at least most of us can't.

    Jim

    Post Edited (Jim McCorison) : 2/3/2005 3:38:12 AM GMT
  • MatthewMatthew Posts: 200
    edited 2005-02-03 03:03
    Are all 32.768 kHz crystals the same? I went over to Mouser and found around 63 different kinds. Any suggestions on which ones I'm looking for?

    Thanks.
  • MatthewMatthew Posts: 200
    edited 2005-02-05 23:47
    If I don't want the:
    Hours
    Date
    Month
    Day
    Year

    Can I just not include them?

    And if I don't include them, do I just leave them blank in ReadRTCBurst?
    ReadRTCBurst:
    'Read all time-keeping registers in one burst
    HIGH RTCCS
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Day,Year]
    LOW RTCCS
    RETURN

    Thanks.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-02-06 03:56
    I'm not sure exactly what you're trying to do.· But as far as not reading all the registers, you could just read the one you want.· Of course, you wouldn't use Burst mode for that, since it's designed to read them all in with one command.· Check the Datasheet and example source code provided by Parallax to guide you in accessing individual registers.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --==<{Chris}>==--
  • MatthewMatthew Posts: 200
    edited 2005-02-06 04:39
    Chris, I've read the datasheet and example codes, but I still don't have an idea of what I'm looking for.

    Page 8 on the datasheet seems to be what I'm looking for: http://pdfserv.maxim-ic.com/en/ds/DS1302.pdf

    But how do I look for those specific bits using PBASIC commands?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-02-06 05:15
    Have you tried this document?· http://www.parallax.com/dl/docs/prod/appkit/ds1302rtc.pdf

    Another thing is, since the Burst Mode seems to be something you are familiar with, you could use it to read in all the data, the just ignore what you don't need.· If you're trying to conserve variables, you will need to read the individual register.· The codes for accessing them are in the PDF file above.· You only need to understand how the DS1302 is written to (Command are sent) and how they are read.· Also you can try this file:

    http://www.parallax.com/dl/docs/books/stampworksmanual.pdf

    Which is a PDF of the Stampworks manual, and has example code for accessing the DS1302.· In fact the code I use now is based on many of the routines used in this book, with a few modifications, like·better resolution.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --==<{Chris}>==--
  • MatthewMatthew Posts: 200
    edited 2005-02-06 05:42
    Chris, thanks for showing me the second PDF file (I've read the first one several times now), but I'm still lost... I'm looking to only access the 'seconds' register.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-02-06 14:54
    Mathew,

    ·· I am trying to help you to help yourself here...With all the documentation on the DS1302 provided, and the code examples, I am wondering if maybe you should read the WAM (What's A Microcontroller) book.· The information is there.· You will need to send the DS1302 a command to read a register, basically what register you want to read, and the data in another variable that you want to write to that register.· Then you will SHIFTOUT this data to the DS1302.· The codes for each register are listed in at least 2 of the docs I sent you.· Hopefully you will be able to figure it out now that I have laid out the steps and provided the information.· Just try it out...Experiment a little.· That's how you learn!· turn.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --==<{Chris}>==--
  • MatthewMatthew Posts: 200
    edited 2005-02-06 15:01
    Okay, I'll give it a shot, but here's a question that I seem to not know:

    HIGH RTCCS
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds, Minutes, Hours, Date, Month, Day,
    Year]
    LOW RTCCS

    What does the '%1\1' amd '%10\2' mean
  • MatthewMatthew Posts: 200
    edited 2005-02-06 15:13
    So you think if I replace 'BrstReg' with 'SecReg' it would work? I just realized all of those constants it told me to place, and how I wasn't using any of them...

    HIGH RTCCS
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,SecReg\5,%10\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds]
    LOW RTCCS
  • MatthewMatthew Posts: 200
    edited 2005-02-06 15:14
    Oh, and I would try it to see if it works, but I'm still waiting on a crystal to be delivered.
Sign In or Register to comment.