555 timers and counters
Matthew
Posts: 200
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?
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?
Comments
can't control the duty cycle.
By the way, what are you trying to do?
bugg
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
So many projects, so little time.
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.
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?
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.
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.
Post Edited (Paul Baker) : 1/23/2005 5:22:19 PM GMT
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.
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
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
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:
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
Maybe then it would be just one execution per half second. That should only add one (or two) commands per subroutine.
Just an idea.
Paysonbadboy, I'm a semi-newbe, so I'm not sure.
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.
Just curious, but what/how far are you trying to sense with the ultrasonic sensor?
Dave
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
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.
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
Thanks.
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}>==--
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?
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}>==--
·· 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!·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--==<{Chris}>==--
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
HIGH RTCCS
SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,SecReg\5,%10\2]
SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds]
LOW RTCCS