Shop OBEX P1 Docs P2 Docs Learn Events
Help COUNTing delay /w BS2 and 555 timer — Parallax Forums

Help COUNTing delay /w BS2 and 555 timer

Justin_CJustin_C Posts: 9
edited 2008-05-01 16:37 in BASIC Stamp
Quick project abstract: I am using some BS2s, a PLC, some CTs, and other misc equipment to function as a digital overcurrent relay for a simulated grid.

In order to confirm the experimental accuracy of the inverse-time characteristic (closely modeled after a CO-8 relay), I need to be able to see how much time has elapsed between the time a fault is detected and the time the circuit breaker is ordered to open.

A HIGH signal will be sent out from the first BS2 saying that a fault is detected. A second BS2 will then count pulses from a 555 timer in astable mode as long as that initial signal remains HIGH (that is, the delay is complete and the command to open the circuit breaker is being issued).

The problem is that the COUNT command needs a specific duration to count for. In my situation, the duration will be different depending on how much/little the fault condition is. My current work-around for this problem is to invoke the COUNT command in 100mS increments, then check to see if the signal from the first BS2 is still set to HIGH. If it is still set to HIGH, go back and invoke the COUNT for another 100mS and keep a running total. If the signal is NOT set to HIGH, convert the pulses into time and display it.

I am running the 555 timer in astable configuration with a frequency of around 1kHz and a duty cycle of about 60%. For some reason, I am not able to count the pulses. I had the program working at one time with a frequency of about 40 Hz but that will not be fast enough. I am beyond the HIGH state duration that the COUNT command needs and well below the maximum allowed frequency. Also, I have verified the frequency/duty cycle of the 555 timer with an oscilloscope so I know thats not the problem. Any suggestions? Thanks in advance!

CODE ATTACHED

Comments

  • Justin_CJustin_C Posts: 9
    edited 2008-04-30 07:47
    Hmm, apparently I can't attach a BS2 file using Safari. Here is the code the old-fashioned way and attached in PDF format. Thanks again in advance.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'binary long-division algorithm courtesy of
    'http://www.emesystems.com/BS2math2.htm

    '*****************************
    '* Variable/Pin Declarations *
    '*****************************

    DataIn PIN 5 'square-wave pulse from 555 timer @ 1kHz
    StartCount PIN 10 'signal that fault condition is detected

    Pulses VAR WORD 'pulses counted from a signle 100ms increment
    PulsesTot VAR WORD 'total counted pulses

    index VAR BYTE

    CountFreq VAR WORD '555 timer astable freq. of 1000 Hz
    delayTime VAR WORD

    numr VAR WORD 'numerator in binary long-division
    deno VAR WORD 'denominator in binary long-division
    intg VAR WORD 'integer result of binary long-division
    frac VAR WORD 'fractional result of binary long-division


    '***************************
    '* Variable Initialization *
    '***************************
    delayTime = 0
    CountFreq = 1000 '555 timer astable freq. of 1000 Hz
    Pulses = 0 '555 pulse count of each 100mS increment of COUNT command
    PulsesTot = 0 ' running pulse-count total

    Main:
    DO 'check to see if pulses must be counted
    GOSUB CountCheck
    LOOP 'forever!

    CountCheck:
    IF StartCount = 1 THEN 'if pin is set to HIGH, count pulses
    GOSUB CountTrip
    ELSE '...otherwise go back to main and check again
    ENDIF
    RETURN

    CountTrip: '%% counts pulses from 555 timer %%
    COUNT DataIn, 100, Cycles 'counts pulses for 1/10th second increments
    PulsesTot = PulsesTot + Pulses 'accumulates all pulses counted
    IF StartCount = 1 THEN 'checks to see if pin is STILL set to HIGH
    GOSUB CountTrip 'it it is HIGH, count pulses for another 1/10th second
    ELSE '...otherwise break from IF-ELSE statement and display the elapsed time
    ENDIF
    GOSUB DisplayTrip
    RETURN

    DisplayTrip: '%% converts pulses to time and displays the result %%
    '%% Binary Long-Division to convert pulses to time in Sec %%
    ' ' ' ' ' ' ' ' ' ' ' ' '
    numr=(PulsesTot) '
    deno=(countFreq)-1 '
    intg=1-(numr.BIT15 ^ deno.BIT15*2) '
    numr=ABS nurm '
    deno=ABS deno '
    intg=numr/deno*intg '
    FOR index=15 TO 10 '
    numr=numr//deno<<1 '
    frac.BIT0(index)=numr/deno '
    NEXT '
    frac=frac**10000 '
    '** ' ' ' ' ' ' ' ' ' ' ' '
    DEBUG HOME, CLS, "Trip Time in Sec: "
    DEBUG DEC intg, ":", DEC frac
    RETURN
  • stamptrolstamptrol Posts: 1,731
    edited 2008-04-30 12:10
    ·As an old relay tech, I often wished we had this kind of technology (microcontrollers) when I was in the field!

    · When the stamp is required to do some other work while tracking pulse inputs, I've found 40 - 60 Hz is about the limit. In your case, you could move up to a BS2sx or BS2px to get better speed.

    · Or, feed the 555 output into a counter chip during the timing interval. When the interval is over, the stamp can read the counter at its leisure and not have to worry about missing a pulse.

    ·I've attached some info on a lumber tally system where I used your technique to measure board width in a sawmill. In my case, the pulse stream was generated by an encoder. Two 4024 counter chips counted the pulses during the measuring interval, then the stamp looked at the counter output when the board had passed.

    · Cheers,




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
  • Justin_CJustin_C Posts: 9
    edited 2008-04-30 15:23
    stamptrol,

    Thank you very much for posting your project, I really appreciate it.

    This is my first experience for the stamp and while it is quite flexible, I quickly ran found out the speed limitations. Unfortunately, I already have the BS2 and am limited to around 40 Hz. My advisor for my project said this is okay as it still gets the point across without having to spend any more money.

    In order to read the counter output, would you recommend a parallel to serial shift register? I dont have the pins available to read all of the outputs directly into the stamp.

    Thanks again,

    Justin
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-04-30 16:21
    What duration are your expected time intervals?

    There is an application note on measuring time intervals with the Stamp here. It uses program loops to get a resolution of better than one millisecond.

    There is no reason you could not use COUNT in the manner you suggested in 100ms intervals. However, the program you posted is recursive, in that the CountTrip subroutine calls itself with GOSUB, and that is a no-no on the Stamp. GOTO instead. Or put it in a DO:LOOP.

    In any case, I don't think the 555+COUNT is really the best solution. The second Stamp should be able to achieve the required 1ms resolution on its own using a program loop or by using a series of RCTIME commands in a loop like this:


    rollovers =-1   ' another variable, each unit of which represents 0.131 seconds
    DO
      DO : LOOP UNTIL Startcount   ' wait for pin to go high
        DO
          rollovers = rollovers+1  
          RCTIME startcount,1,cycles
        LOOP WHILE startcount   ' will stay in this loop accumulating rollovers and cycles until startcount goes low.
      DEBUG DEC rollovers,".",DEC5 cycles   ' each unit of rollovers is 0.131 second, each unit of cycles is 2 microseconds.
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Justin_CJustin_C Posts: 9
    edited 2008-04-30 16:56
    Tracy,

    The shortest time interval I will experience will be on the order of a few hundred milliseconds (around 200-ish).

    Hmm...I never really gave using RCTIME serious consideration as I wasn't sure of the accuracy that could be achieved by it. I will agree, the 555 solution has not been very successful; most likely due to delay/lack of accuracy. Also, the 555 requires more external circuitry, which is always nice to avoid [noparse]:)[/noparse]

    I just got to the lab and will post my progress (or lack thereof) once I give your RCTIME technique an honest effort.

    I sincerely appreciate your input, it's been hard to come up with new ways of attacking these problems since I am such a stamp-noob!

    Justin
  • stamptrolstamptrol Posts: 1,731
    edited 2008-04-30 17:10
    Justin,

    The shift register idea will work to read multiple bits. I think I used 4 inputs to read either 8 or 10 bits of counter outputs by using multiplexing chips. 74ls125. You can check on the schematic I posted.

    Good luck.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Justin_CJustin_C Posts: 9
    edited 2008-04-30 17:28
    Tracy,

    Two quick questions about the RCTIME implementation to make sure I'm understanding its operation:

    [noparse][[/noparse]1.] Will a standard RC circuit (such as the one shown in the manual) work with the input signal from the first BS2 to charge that circuit? (I dont need to explicity set the pin to high for whatever duration to ensure RCTIME works correctly if I used a short time-constant for the RC circuit)

    [noparse][[/noparse]2.] Since the 'rollover' variable is accumulating pulse-widths (with are 131.07 mSec in duration) while 'cycles' is counting the discharge time of the RC circuit in 2 uSec intervals, I can just multiply/divide those values by the corresponding scaling factors to get the time elapsed?

    Thanks again!

    Justin
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-04-30 20:09
    You don't need an RC circuit. The command is called RCTIME, but it has other uses as well that have nothing to do with Rs and Cs. It simply measures the time required for a pin to go low. There is an example in the Stamp manual under the heading "demo program 2" to measure the time it takes for a relay to close, very similar to your application. My understanding is that your second stamp will simply be measuring the time the startcount pin remains high.

    Right, to convert to commensurate units of milliseconds, mS = (rollovers * 132) + (cycles / 500). There are little things you can do to tweak the formula for best accuracy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

  • Justin_CJustin_C Posts: 9
    edited 2008-04-30 20:19
    Eureka!

    I have it working! (for the most part)

    Tracy, I would like to really thank you for all of your great work for the STAMP community (especially the math section on your site) and your help via these forums. I attribute most of my STAMP learning outside of the manual to what I found on your site. I truly, truly appreciate it!

    I'll post up my results of the entire project once I wrap it up this weekend.

    Justin
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-05-01 15:30
    You are very welcome. And thank you for your kind words. It is the community that keeps me coming back too, the give and take of technical knowledge as well as the spirit of friendship.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tedbeautedbeau Posts: 48
    edited 2008-05-01 16:37
    stamptrol said...
    I've attached some info on a lumber tally system where I used your technique to measure board width in a sawmill. In my case, the pulse stream was generated by an encoder. Two 4024 counter chips counted the pulses during the measuring interval, then the stamp looked at the counter output when the board had passed.
    Stamptrol, I have an application that I was considering using a counter chip for simular to what you describe. I want to scan across the front of my robot with an infrared sensor·for· object detection. My plan was to use one or two 74193 decade counter chips with a clock cycle driven by a 555. The speed of the clock chip would be set to divide the arc swept by the scanner into 16 equal segments. If the IR reciever gets a signal of an object I would read the counter and determine what the angle of the object was. I read your attached file but would also like to see your schematic and code implimentation for reading your counter.

    Thanks

    Ted
Sign In or Register to comment.