+ Reply to Thread
Page 1 of 4 1234 LastLast
Results 1 to 20 of 64

Thread: Is there a way to count pulses (a steady 120hz) for a changing length of a butt

  1. #1

    Default Is there a way to count pulses (a steady 120hz) for a changing length of a butt

    Is there a way to count pulses (a steady 120hz) for a changing length of a button.

    Im trying to count pulses that are a steady 120hz, for a length of time, that time is variable upon the amount of time that passes between each button push. pulsin wont work because its max pulse width is too small.

    so basically I want to

    COUNT pin, (duration is time between button presses), variable

    Is it possible for me to count a signal AND monitor a button press for a desired state?
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  2. #2
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,645
    Blog Entries
    8

    Default

    Hello,

    That's not going to work the way you plan. The COUNT command requires a specific duration to be used. You can't COUNT until another event, at least not using the command that way. You may need to implement an external RTC or other clock/counter source, reset the counter, initiate the clock, and when the button is pressed, read the counter. This could even be done using simple/common chips, depending on your part count requirements.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  3. #3

    Default

    Hmm, more and more I am beginning to realize that the stamp is mostly a calculator, and logic machine. For clocking and counting, its not so good. (for multitasking reasons)

    But thats not bad, (try to do major math operations with external logic chips heh)
    I have got a ton of mil-spec logic chips. So somehow I will be able to do this.
    As far as my part count? Dosen't matter. Room, and chips aren't a design consideration.
    But counting with say a 74HC393 (dual binary counter) (to count to 120 i would need 3 chips, 0011 0001 0011 0010 0011 0000 = 120
    But the problem comes trying to load the binary number into the stamp. I guess I would need another chip that takes parallel in serial out?

    Again, I think I am reminded to stick to One / Two wire chip systems. DAMN MIL CHIPS! (thats what I get for using 60's technology)

    (runs away screaming)
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  4. #4
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,645
    Blog Entries
    8

    Default

    Hello,

    I didn't have a specific arrangement in mind. I can think of two main ways to do this. One would involve a serial clock chip, such as the DS1302, although you would probably need one that counts tenths of seconds, so you might have to do a little looking.

    In any event you would reset the clock (minutes/seconds, tenths) then start checking for your button input. Once you get it, read the clock value for the elapsed time. I didn't read back to see if you specified the resolution you wanted for it.

    The other way would be using discrete chips, and would depend on your level of experience, but it's how we did things in the old days (80's and early 90's). You could have a TTL Oscillator clocking a counter (16-bit? Would depend on resolution, etc.). You would most likely need a divide-by-n counter between the two. The main counter chip would need a parallel output. This output could be tied to 74LS165's to be read into the BS2 via SHIFTIN (Using 3 lines). Now you would need one output from the Stamp to reset the counter (Many chips have a reset line on them).



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  5. #5

    Default

    What kind of time are you talking about between button presses? There may be a way to do it using an external SCR "latch" to
    enable or disable the incoming 120Hz pulses within a "time window" and still use the COUNT command.

    Also, what kind of resolution between presses do you need? What is the source of the 120Hz in relation to your circuit?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  6. #6

    Default

    Source is a 120hz crystal. Resolution? I need to be able to count every pulse coming from the 120hz crystal at the start of each button press to the next time the button is pressed.

    So in a sence Im counting EVERY 120hz pulse, but noting how many pulses happen from the instant the button is pressed, all the way to the next time the instant the button is pressed again. So I need to monitor two inputs heh. Which isn't possible at the same time.

    The button is pressed by a human not faster than mabee once every 100ms MAX. And can be as long as once every 1000ms between button presses.
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  7. #7

    Default

    So every 8.3 ms, I have a pulse that I must count. Wait, I can do this can't I ?

    I could monitor the 120hz input for a pulse, then at pulse, add 1 to my total pulse count. (every 5 ms?) then after that keep monitoring the button for a desired state, then desired state is reached, reset total pulse count?

    Something like that? Is the BS2 fast enough to do this?
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  8. #8

    Default

    Since a 120 Hz signal has a 8.3 ms period, you may be able to use the following subroutine. It waits for a button press, waits for the release, starts counting, and stops when the button is pressed again. I tested this on my PDB using the signal generator setting at 100 Hz. That's a little slower than your signal, but itworked, so I tested at 120 Hz using a 555.

    Code:
    Count_Pulses:
      DO : LOOP UNTIL (StartBtn = Pressed)
      DO : LOOP UNTIL (StartBtn = Released)
      DO
        IF (PulsesIn = 0) THEN
          pCount = pCount + 1
          DO : LOOP UNTIL (PulsesIn = 1)
        ENDIF
      LOOP UNTIL (StartBtn = Pressed)
      RETURN


    I've attached the full program that is currently running on my PDB with a 120 Hz signal from a 555 circuit that I happened to have built-up for another project (StampWorks update). Here's how I tested: I pressed the button and then counted out loud: "One thousand on, one thousand two..." up to five seconds. The result: 597. Since 120 x 6 = 600 and my button timing may not have been exactly five seconds I'd say it's working.

    Now ... if you have to do a bunch of other things while the inputs are being counted, then you will need an enternal circuit. An easy solution could be an RTC (real time clock chip) set to counter mode. Have a look at the PCF8483. It's an I2C device, but has an event counter mode and I2C routines can be synthesized with the stock BS2. If you're using any of the "p" family members, I2C is built in.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/26/2005 9:10:49 PM GMT
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	Pulse_Count_Scope.jpg‎
Views:	123
Size:	183.6 KB
ID:	38132   Click image for larger version

Name:	Pulse_Count_DEBUG.jpg‎
Views:	169
Size:	52.0 KB
ID:	38133  
    Attached Files Attached Files
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  9. #9

    Default

    The BS2 isn't fast enough to make this work, is it?

    Code:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    hz        PIN   0       '120hz input on pin 0
    bttn      PIN   1       'Button input on pin 1
    prev      VAR   Bit     'Previous button state
    prevhz    VAR   Bit     'Previous 120hz state
    counthz   VAR   Byte    'hertz count
    
    onn       CON   1       'Active High
    off       CON   0
    
    
    Main:
    
      DO
        DEBUG ? counthz
    
        IF bttn = off THEN
        prev = off                              'button isn't pressed set prev state to 0
        ENDIF
    
        IF bttn = onn AND prev = off THEN       'button is pressed and prev state was 0
        counthz = off                           'reset 120hz pulse counter
        prev = onn                              'set previous state to 1
        ENDIF
    
        IF hz = off THEN
        prevhz = off                            '120hz line is in low mode, set prevhz state to 0
        ENDIF
    
        IF hz = onn AND prevhz = off THEN       '120hz is in pulse state, and previous state was 0
        counthz = counthz + 1                   'counthz adds 1 because cycle is complete.
        prevhz = onn                            'set prevhz state to 1
        ENDIF
    
      LOOP
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  10. #10

    Default

    You're doing things the hard way and adding a lot of overhead that is killing the ability to count pulses. For example, you have pulses coming every 8.3 milliseconds yet you're sending a DEBUG message while attempting to count. At 9600 baud, each byte transmitted takes one millisecond, so doing this:

    DEBUG ? countHz

    Will consume at least 11 milliseconds (when countHz is one digit; add another ms for each additional digit)-- no good.

    Is there some problem with the program I wrote for you? It proves that the BS2 if fast plenty enough to do what you want. The key is efficient programming; something I have a bit of practice with in 12 years of BASIC Stamp programming.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/26/2005 10:19:58 PM GMT
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  11. #11

    Default

    No, for somereason I was not able to get it to work right. I connected my 120hz pulse to pin1.
    And then connected my button, to pin2, with the other pin of the button to low (vss). I also connected pin2 to Vdd thru a 1k resistor to make the button active low.

    Is this right?
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  12. #12

    Default

    oops, i reversed button with 120hz in pins.

    But after switching them, it still doesn't work. heh
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  13. #13

    Default

    No. If you look in the declarations you'll see that my button was connected to P0. If you want it on P2, update the declaration like this:

    StartBtn PIN2 ' active-low button input

    And I can assure you that the program does work ... when connected properly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  14. #14

    Default

    LOL.. ok ok

    Let me SAY this right.

    I DO have pin 1 connected to my freq.

    AND I DO have pin0 connected to my button.

    and it doesn't work heh.
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  15. #15
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,645
    Blog Entries
    8

    Default

    I should've waited because I knew Jon would come up with something simpler to do this. Even I learn something new, as I didn't realize that chip (PCF8483) had an event counter mode. I'll have to file that info away...


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com


    Post Edited (Chris Savage (Parallax)) : 6/26/2005 10:34:46 PM GMT
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  16. #16

    Default

    If you're running my program without changes and you've got the connections correct, then the problem may be with you 120 Hz input. Is it TTL level (you mentioned a cyrstal earlier, so I'm suspicious). You should probably put a 'scope on your circuit and check the levels. If the 120 Hz input is not going higher than 1.4 volts then the BASIC Stamp will never see it.

    If you don't have a scope, do this:

    COUNT 1, 1000, countHz
    DEBUG ? countHz

    If it doesn't show

    countHz = 120

    ... then you know the BASIC Stamp is not seeing the 120 Hz input and you need to fix that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/26/2005 10:35:32 PM GMT
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  17. #17

    Default

    I got it to work.

    You were active low on BOTH(not just button). I use a different power regulator for my 555(setup on another breadboard). So I use common ground. (not common 5) Since you were active low on both button and 120hz, the 120hz wasn't being seen.
    (due to common low) I just modified your code to make active high.

    :)
    Thank you
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  18. #18
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,645
    Blog Entries
    8

    Default

    BPM,

    Just remember, before you think the Stamp can't do something, ask someone with experience like Jon Williams and it's probably either been done, or can be done with the right code and/or minimal hardware. Also, clear details about your circuit and what you're trying to do can save a lot of back and forth posting to get all the information. But at least you have it working now.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  19. #19

    Default

    I'm glad it's finally working. Now take back every mean thing you ever said about the BASIC Stamp!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

  20. #20

    Default

    I have NEVER said anything mean! I ALWAYS assume it is human error. (because even if I fried it, it would be my fault)

    Stamps rawk!

    Im just thinking I may need to get a BS2P24 or BS2P40. I2C, and other commands are nice. so might more speed. convincing my wife that I need another 100$ item is the problem... :)

    Got a program to help me with that one?

    Ok, heres what im REALLY trying to do with this stamp.

    Count a button click and calculate the Beats Per Minute the button clicks at. Then take the result and display it on a 3 digit display.
    Also, based on some pin inputs(switches), flash two pin outputs at the same BPM speed.
    I do have alot of work to do.
    :)
    Last edited by ForumTools; 09-30-2010 at 10:07 AM. Reason: Forum Migration

+ Reply to Thread

Bookmarks

Posting Permissions

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