Stupid Newb Q: Need a ~20hz clock input
jmccorm
Posts: 16
I have a segment of code that needs to be run at regular intervals. Something close to 20 times per second is good. Unfortunately, because of the branching in the code, the run-length is not predictable, so I just can't insert a regular delay. Trying to time out multiple delays for the branches would not give me the precision I need.
I'm a good programmer, but an electronics idiot. Is there a low component count/cost way that I can get myself a ~20hz clock onto one of my pins? If an operation is necessary to clear the pin, that is fine.
I'm thinking some easy combination of resistors and capacitors might do it, but as I said, I'm an electronics idiot. I'd rather avoid a 555 chip if possible because I need to keep the component count low. (I've already crammed too many things onto my breadboard.)
The idea here is that I've got to take a button input, say, every 1/20th of a second (or an interval close to that... I don't care too much as long as it is REGULAR). That gets stored to an external eeprom. Later, it has to play it back with the exact timing it was recorded at.
Low end for recording would be 1/15th or so of a second. 1/50th of a second at the high end would be overkill but acceptable.
PSEUDOCODE FOR RECORD:
While [noparse][[/noparse] other activity is going on in support chip ]
Wait for clock signal
Clear clock if necessary
Read byte from I2C EEPROM
Read bit state from button
Is the button in a different state than the bit that is recorded in a position in the EEPROM byte?
If so, store the modified byte back to EEPROM
End While
PSEUDOCODE FOR PLAYBACK:
While [noparse][[/noparse] other activity is going on in support chip ]
Wait for clock signal
Clear clock if necessary
Read byte from I2C EEPROM
Output nibble of byte to pins 12-15
End While
I'm a good programmer, but an electronics idiot. Is there a low component count/cost way that I can get myself a ~20hz clock onto one of my pins? If an operation is necessary to clear the pin, that is fine.
I'm thinking some easy combination of resistors and capacitors might do it, but as I said, I'm an electronics idiot. I'd rather avoid a 555 chip if possible because I need to keep the component count low. (I've already crammed too many things onto my breadboard.)
The idea here is that I've got to take a button input, say, every 1/20th of a second (or an interval close to that... I don't care too much as long as it is REGULAR). That gets stored to an external eeprom. Later, it has to play it back with the exact timing it was recorded at.
Low end for recording would be 1/15th or so of a second. 1/50th of a second at the high end would be overkill but acceptable.
PSEUDOCODE FOR RECORD:
While [noparse][[/noparse] other activity is going on in support chip ]
Wait for clock signal
Clear clock if necessary
Read byte from I2C EEPROM
Read bit state from button
Is the button in a different state than the bit that is recorded in a position in the EEPROM byte?
If so, store the modified byte back to EEPROM
End While
PSEUDOCODE FOR PLAYBACK:
While [noparse][[/noparse] other activity is going on in support chip ]
Wait for clock signal
Clear clock if necessary
Read byte from I2C EEPROM
Output nibble of byte to pins 12-15
End While
Comments
You dont use rctime, you just keep checking the input pin for a low state during a program loop. You can use rctime to do the initial time testing for your cap/resistor timer though.
http://forums.parallax.com/forums/default.aspx?f=5&m=94508
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!
GROUND connects to one capacitor's lead
The other capacitor lead connects to.... the 57k resistor?
The 57k resistor's other lead connects to my PIC IO pin and also to the 370 ohm resistor.
The 370 ohm resistor's other lead connects to... ground?
+5 connects to the capacitor.
The capacitor connects both to R1 and R2.
R1 connects to ground.
R2 connects to my IO pin on my stamp.
R2 would be the 220-370ish ohm resistor?
R1 would be the 140k resistor?
Post Edited (jmccorm) : 11/23/2005 3:00:19 PM GMT
http://www.rhombus-tek.com/co-processors.html
This chip will generate a 50 mSec (20 Hz) signal, which you can then use to synchronize your BS2 program. It's only $12, it's an 8-pin DIP with resonator. It also has inside it a recieve only RS-232 port, which can be handy.
Check out their documentation for how to use this with a BS2 to provide simple multi-tasking. Very clever.
t = 1.347 * R * C
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Adjusting the big resistors value gives you your discharge time, Tweek the resistor value using an RCTIME loop (Rctime) that returns 50 mS
On the Bs2 the chart shows 2us per digit, you need 50 mS or 5000 us so you need rctime to return the number 2500
2500*2uS = 5000 uS = 50 mS
1000 Ms per second so 1000 / 50 = 20 times per second
The nice thing about this is the flag (the pin going low) waits for you to reset it, so your main program code should be in the case of the BS2 running at 2000 lines per second, less than 50 mS between polling the pin. That gives you at maximum 100 lines of code to execute between 20 mS resets of the RC timer.
If you have an Scope, you can see exactly how much time code takes by setting a pin high on entry and off on exit of the code and watching the scope while it's running.
Question to Tracy, Would the pin not a direct short to ground in circuit you posted through the capacitor when it is first brought high as the capacitor would have no resistance and cause a high current spike on the output pin?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!
Another way to do the charging with a single command, replace
HIGH 0 : INPUT 0
with
PWM 0,128,1 ' one cycle of PWM at 1/2 duty, leaves pin an input
BTW, the reason the initial current even without the 220 ohms is limited to around 80 milliamps is that the Stamp acts like it has internally a resistor of around 60 ohms, and that limits the initial current to 5/60 = 0.083 amp. The 5 microseconds comes from the time constant of 0.1uF with 60 ohms.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
I've got it working. I put it into a loop and incremented a counter. I saw how long the counter got in 10 seconds, and sure enough, almost smack on 200. rctime gave me a value of 67956, which on a BS2P is 52ms. Very very nice.
Being able to clear the timer flag is great, and not having to worry about missing the flag (in case my code runs long) in these circumstances is also great. I went ahead and put in the protection resistor because, if for no other reason, I can be an electronics idiot, so extra protection is worth putting in there in case I end up shorting something.
The resistance that I settled on (trial/error) was 400k. The only note that I would add is that the "delay 1" between the HIGH and INPUT commands really does make a difference in the time. Looks like the capacitor just doesn't get drained in time running at BS2P speeds.
I'll be back with the completed piece in about 20 days!
members.cox.net/jmccorm/santa.html