Shop OBEX P1 Docs P2 Docs Learn Events
Stupid Newb Q: Need a ~20hz clock input — Parallax Forums

Stupid Newb Q: Need a ~20hz clock input

jmccormjmccorm Posts: 16
edited 2005-12-15 02:08 in BASIC Stamp
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

Comments

  • metron9metron9 Posts: 1,100
    edited 2005-11-23 07:43
    I posted an RC timer here, One Capacitor and two resistors, the pin charges the cap through one resistor and the cap discharges through another resistor as the pin waits for the logic level to go low. I used a 0.1 uf cap and a 370 ohm resistor and I think a 57k resistor for the discharge resistor, something that gives you a 100ms discharge (You can calculate what you need see help RCtime) so you could set it up for 50 ms discharge to get 20 times per second timer.

    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!
  • jmccormjmccorm Posts: 16
    edited 2005-11-23 14:20
    So.... the wiring would be something like this?

    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?
  • jmccormjmccorm Posts: 16
    edited 2005-11-23 14:28
    I think I have it now....

    +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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-11-23 15:50
    This sounds like a job for the smT chip.

    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.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-11-23 17:06
    jmccorm, maybe like this in simplest case (without the pin protection resistor).
    t = 1.347 * R * C

                 370k       
    p0  ---o---/\/\---o--- Vss
             |             |
             `----||----'
                  0.1 uf
    
       HIGH 0
       INPUT 0
       ' ....
       ' in main loop
       IF IN0=0 THEN
         HIGH 0
         INPUT 0
         ' key input
       ENDIF
       ' other stuff  
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • metron9metron9 Posts: 1,100
    edited 2005-11-23 18:03
    I use this, The pin charges the cap through the low ohm resistor, then becomes input the cap discharges to ground through the big resistor

    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.



    
                          370k          CAP  
    p0 --------o---/\/\-----o------||---- Vss 
                   |           
                   o----/\/\----o---------- Vss
                      BIG R2
    
    
                 
    
    



    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!
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-11-23 18:26
    Metron, while it is true there is a surge of current when p0 goes high in the circuit, about 80 milliamps, it only stays at that level for about 5 microseconds. The Stamp is quite robust, and the surge wll not create enough heating in the internal circuits of the Stamp to do damage. (famous last words?!) HOWEVER, you are right, in that it is a good idea to include the pin protection resistor of ~220 ohms to limit the initial current. This resistor is much lower value than the timing resistor, so it doesn't significanlly affect the charging.


              220     370k       
    p0  ---/\/\---o---/\/\---o--- Vss
                      |             |
                      `----||----'
                          0.1 uf
    
    



    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
  • jmccormjmccorm Posts: 16
    edited 2005-11-23 19:01
    You guys are awesome. hop.gif

    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!
  • jmccormjmccorm Posts: 16
    edited 2005-12-15 02:08
    Animatronic Santa Claus Hack
    members.cox.net/jmccorm/santa.html
Sign In or Register to comment.