Shop OBEX P1 Docs P2 Docs Learn Events
Generate quadrature clock using P1? — Parallax Forums

Generate quadrature clock using P1?

porcupineporcupine Posts: 80
edited 2015-02-06 11:32 in Propeller 1
I have been using the Prop for a few years but I still don't understand the counters. For some reason all the docs (like this one: http://www.parallax.com/downloads/an001-propeller-p8x23a-counters) and the explanations in the manual feel very terse to me, hard to comprehend.

I am trying to drive a clock for an HD6309E (Hitachi's version of the MC6809E, where E stands for external clock...) from a Prop. It has two clock inputs, which are to be quadrature clock, with a signal that looks something like this, say: http://www.techlib.com/schmartboard/quadclock.JPG

I can do this with bit twiddling individual pins, and I've done that in C and then in inline asm, but I don't think I'm going to be able to get the 4mhz clock I want this way.

It seems like the clock are made for this kind of thing, but I'm unclear how I would make this happen.

Comments

  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2015-02-04 15:59
    porcupine wrote: »
    I have been using the Prop for a few years but I still don't understand the counters. For some reason all the docs (like this one: http://www.parallax.com/downloads/an001-propeller-p8x23a-counters) and the explanations in the manual feel very terse to me, hard to comprehend.

    I am trying to drive a clock for an HD6309E (Hitachi's version of the MC6809E, where E stands for external clock...) from a Prop. It has two clock inputs, which are to be quadrature clock, with a signal that looks something like this, say: http://www.techlib.com/schmartboard/quadclock.JPG

    I can do this with bit twiddling individual pins, and I've done that in C and then in inline asm, but I don't think I'm going to be able to get the 4mhz clock I want this way.

    It seems like the clock are made for this kind of thing, but I'm unclear how I would make this happen.

    Just start a counter to output 4MHz and then synchronize (or just count cycles) to quarter cycle and start another counter the same.
  • tonyp12tonyp12 Posts: 1,951
    edited 2015-02-04 16:11
    Looks simple.
    a cog have two counters, to simplify we only refer to counter who's registers ends in A, the second one is the same and have a B instead.

    Select both to be NCO mode, that means when the counter is at 2147483648 and up to 4294967295 the pin will be asserted
    e.g if last bit31 in PHSA is high=pin is high

    Adding 1 to the PHSA counter and it would take 53.68 seconds for a complete toggle (called cycle or Hertz)
    So something else have to be done and the solution is that you can tell how much to add to the counter at each Prop tick, a value you store at FRQA.

    You want 4MHz and the Prop ticks are at 80Mhz, the math is 4294967296/80*4= 214748365 value to store in FRQA

    As you can write and set the counters to a known value before you start them,so set PHSA to (4294967296 /4) and PHSB to 0 should give the two CLKs in the pic (A being the top)
  • porcupineporcupine Posts: 80
    edited 2015-02-04 17:39
    Thanks, that is much clearer to me than the descriptions I've read before.
  • altosackaltosack Posts: 132
    edited 2015-02-04 18:40
    In order to synchronize the counters, you need to start them precisely relative to each other. Since it's not possible to start both of the counters in the same instruction, a little cycle counting is necessary, and this will be hard-coded for a single clock speed.

    At 80 MHz, there are 20 clock cycles per 4 MHz cycle, and 5 clock cycles per 1/4 4 MHz cycle, so ideally we would want to start the counters (set PHSx to #0) 5 clock cycles apart. An instruction takes 4 clock cycles, so we need to put PHSA one clock cycle ahead to get them synchronized, i.e., set PHSA to 214748365 (as per Tony's calculation above) and PHSB to 0 in the next instruction.
    // This only works at 80 MHz
    // Sorry for the .S prop-gcc syntax; I've forgotten how to do it in SPIN's PASM !
    
    #define CTR_NCO_MODE            %0_00100_000
    
    movi    CTRA, #CTR_NCO_MODE
    movi    CTRB, #CTR_NCO_MODE
    mov     FRQA, _214748365
    mov     FRQB, _214748365
    mov     PHSA, _214748365
    mov     PHSB, #0
    
    _214748365:    .long  214748365
    
  • HarpritHarprit Posts: 539
    edited 2015-02-06 05:37
    Does quadrature mean 50% duty cycle
    H
  • BeanBean Posts: 8,129
    edited 2015-02-06 05:52
    Harprit wrote: »
    Does quadrature mean 50% duty cycle
    H

    I've always thought of quadrature as just a 90° phase shift.
    I think 50% duty cycle is kind of implied, but I don't think it is required for the signals to be quadrature.

    Bean
  • kwinnkwinn Posts: 8,697
    edited 2015-02-06 07:46
    Bean wrote: »
    I've always thought of quadrature as just a 90° phase shift.
    I think 50% duty cycle is kind of implied, but I don't think it is required for the signals to be quadrature.

    Bean

    50% duty cycle is mostly what is used but not required. As long as the signals overlap enough to meet the logic timing constraints they can be used to determine direction and speed/rpm.
  • HarpritHarprit Posts: 539
    edited 2015-02-06 09:13
    The 'quad' part seems to indicate some reference to 4 parts (out of 360 degrees)
    Then again the use here might not be exact and rigid
    Just thinking out loud.
    H
  • kwinnkwinn Posts: 8,697
    edited 2015-02-06 10:45
    Harprit wrote: »
    The 'quad' part seems to indicate some reference to 4 parts (out of 360 degrees)
    Then again the use here might not be exact and rigid
    Just thinking out loud.
    H

    I'm sure you're right. I'm also fairly sure the term started in the days when this was done with a magnet and two coils mounted at right angles to generate AC waveforms that were 90 degrees out of phase. Did a quick internet search but couldn't find any confirmation of that.

    Two square waves 90 degrees out of phase (diagram 1) provides four times the positional precision of the pulses in diagram 2 and twice that of the square waves of diagram 3, although all three can be used for position and speed measurements.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2015-02-06 11:32
    Quad-rat-ure : root meaning

    Quad - comes from Latin, where it has the meaning "four, fourth."
    rat - from the English word rotate (Edenic Word RATSah)
    ure - Middle English, from Anglo-French, from Latin -ura means 'state of','act','process & rank'


    So in thinking of a circle ( The rotational aspect ) with reference to Quad and rat, a circle divided into four equal quadrants would yield 90 Degree's per quadrant. Taking into account the entire word 'Quadrature' indicates that we can process the state of each quadrant within a circle. The two signals that are produced are the minimalistic requirements to obtain quadrant information being any two of the four quadrants that are adjacent to one another. In the analog realm the two output signals are sinusoidal in nature where in the digital realm the two output signals represent a 50% duty cycle square wave.

    Note: For increased resolution per rotation, an encoder .... optical, magnetic, or mechanical may have several divisions per revolution but the TWO sensors providing a quadrature output will always be positioned 90 deg apart from one another in relation to the pitch of the number divisions per rotation you may have. (See resolver for other methods not using an encoder)
Sign In or Register to comment.