Shop OBEX P1 Docs P2 Docs Learn Events
Frequency counter using parallax P8X32A — Parallax Forums

Frequency counter using parallax P8X32A

Hello everyone,

I am trying to create a little project with spin. I have an encoder which two outputs A+ and B+ are connected to the propeller chip. A and B outputs produces a 360 pulses per each rotation and depending of the direction of rotation A+ will lead or lag to the B+. I want to be able to get the direction of the rotation and the speed of the rotation using parallax chip i mentioned in title. Max input frequency from the encoder will be around 12 kHz, difference between A+ and B+ is a quarter of the period.

To accomplish this i am trying to use internal counters ctra and ctrb. But i am really stuck on that. I am getting some date from the encoder, but it makes no sense. First i only getting pulses from the encoder when encoder wheel is moving, but i am keep getting values from phsa even though the encoder stopped. Plus if i do the single full rotation on the encoder i expect my value of phsa add up to 360. But the numbers i am getting are way different.
In the examples i found in forum seems that everyone is using gate time and getting the amount of pulses which happens during that time. Is it possible to measure the frequency with these counter without using this wait gate and if the frequency from the encoder isn't constant. Can you point out what i am doing wrong here, and maybe explain a bit how actually these counter works? I have read the parallax article about the counter.
This is my code example, for now i am just trying to read single encoder input and display value.

Thank you for help.

Comments

  • What type of encoder? Are you using pull-up resistors?
  • To read direction as well as speed counters won't help you, some PASM code to detect and categorize every
    transition is needed.
  • The encoder is BAUMER Incremental encoder EIL500. No I am not using pull-up resistors, but do i really need them? I was able to read the pulses just by using ina[10] == for both high and low signal but that only works to about 10kHz. If i get the speed working first i will think about the direction, maybe set some byte depending which phsa gets updated first if thats even possible.
  • If you want to measure frequency you can use one counter to get the high time of a signal, the other to get the low time of a signal. The sum of these values is the period from which you can derive frequency and duty cycle without having to use a 1-second window. All this is best handled in PASM (ctrx stuff, anyway). If the 1-second window is not an issue, use POSEDGE detect so that you just get the transitions.
  • Thanks Jon, i am able to read both encoder outputs now. I wonder is there a way to distinguish which of these two signals leads or lags. On clockwise rotation B output leads by quarter of period, and on counterclockwise A signal leads by quarter of period. But how to distinguish that using spin?
  • Mindaugas,
    If I understand your question correctly, you are trying to read the speed and direction of a motor. There is an object in Obex by Kwabena W. Agyeman, called “Dual Quadrature Encoder Driver” that may satisfy your needs. Kwabena’s code was always excellent and at least could act as a guide for you in developing your own code.
    Jim
  • jmgjmg Posts: 15,173
    Mindaugas wrote: »
    To accomplish this i am trying to use internal counters ctra and ctrb. But i am really stuck on that. I am getting some date from the encoder, but it makes no sense. First i only getting pulses from the encoder when encoder wheel is moving, but i am keep getting values from phsa even though the encoder stopped. Plus if i do the single full rotation on the encoder i expect my value of phsa add up to 360. But the numbers i am getting are way different.
    ... Can you point out what i am doing wrong here, and maybe explain a bit how actually these counter works?

    See this work, of 2 Frequency counters
    http://forums.parallax.com/discussion/123170/propbasic-reciprocal-frequency-counter-0-5hz-to-40mhz-40mhz-now
    The reciprocal version, counts both Fi Cycles and time for Whole Fi Cycles, and then does Cycles/Time calculation.

    I'd suggest connect that, and test your encoder outputs. The code could also form the basis for what you need to do.

    Mindaugas wrote: »
    In the examples i found in forum seems that everyone is using gate time and getting the amount of pulses which happens during that time. Is it possible to measure the frequency with these counter without using this wait gate and if the frequency from the encoder isn't constant.
    You can derive frequency, by measuring period, but usually there is always some minimum frequency.
    ie What do you need to read down to ? 10pps, 1pps, or 10 seconds per pulse ?

    Measuring just a single period, also squeezes things at the upper end - you have 12kHz, so that's 83us to measure and calculate, but at those high speeds you do not need reading every 83us, so things can be easier using more periods, and voila, you now have a Reciprocal Counter (above), which nicely auto-scales for you.
    Mindaugas wrote: »
    .. I have an encoder which two outputs A+ and B+ are connected to the propeller chip. A and B outputs produces a 360 pulses per each rotation and depending of the direction of rotation A+ will lead or lag to the B+. I want to be able to get the direction of the rotation and the speed of the rotation ..

    Speed is covered above, direction is going to be tricky in Spin and 12kHz....

    here is a Quad channel waveform for Up/Down
            a  b c  d                   A B  C D 
    |       +  + +  + +  + +  + +    -  - -  - -  - -  - -  - -  -                                                      |
    |A  ____/====\____/====\____/====\____/====\____/====\____/====          |
    |B  _______/====\____/====\_________/====\____/====\____/====\____/====  |
    |      -----UP ---------------| -------------Down --------------         |
    
       bb.aa    b.a  Change 
        00      00   0    Do nothing
        00      01   1    Down
        00      10   1    Up
        00      11   1    Not legal
        01      00   1    Up
        01      01   0    Do nothing
        01      10   1    Not legal
        01      11   1    Down
        10      00   1    Down
        10      01   1    Not legal
        10      10   0    Do nothing
        10      11   1    Up
        11      00   1    Not legal
        11      01   1    Up  
        11      10   1    Down
        11      11   0    Do nothing
    
    To fully sense dirn, you respond to any pin change, and check new:old. At 12kHz, you have to do this in under ~20us

    However, it may be enough to update direction when you take the speed (frequency reading) and there you just need to capture the other channel, on measured channel edge.
    eg a falling b edge, has b=a for UP ( a=0 ) and b<>a for down. (a=1)
  • I've written a couple encoder objects -- one gets a lot of use in camera control platforms. Attached is a simpler version that works well and may be helpful. It's written in PASM for best speed.
  • Mindaugas wrote: »
    The encoder is BAUMER Incremental encoder EIL500. No I am not using pull-up resistors, but do i really need them? I was able to read the pulses just by using ina[10] == for both high and low signal but that only works to about 10kHz. If i get the speed working first i will think about the direction, maybe set some byte depending which phsa gets updated first if thats even possible.

    Can't find a datasheet for that device.

    Many encoders have open-collector outputs and require pull-up resistors, others are differential and won't.

    Find a PASM encoder object as mentioned above, it will handle direction and speed to as fast as most
    encoders and generate.
  • Hi Mark, thanks i really needed the resistors. Now the values makes sense.
Sign In or Register to comment.