Shop OBEX P1 Docs P2 Docs Learn Events
The counters (working now thanks to Ariba) — Parallax Forums

The counters (working now thanks to Ariba)

PliersPliers Posts: 280
edited 2008-10-14 10:38 in Propeller 1
I have been doing a lot of reading and playing with sample codes, but I’m missing something.
I believe the code I need will only be a dozens lines, so I keep reading and trying. I feel close to understanding, but it is getting a little frustrating.

I understand the need to learn on my own, and don’t what to be a “give me” type of person.

That being said, I am putting up my request for some code.

I need to determine the duty cycle of a 5kHz square wave signal and convert it to a 1 kHz signal with the same duty cycle.

I did it with discreet components, and it works.
I know using the propeller would be a smarter way (more reliable, few components).
Also I have been dying to learn machine code.

I’ll keep trying.

Post Edited (Pliers) : 10/14/2008 11:42:37 AM GMT

Comments

  • PliersPliers Posts: 280
    edited 2008-10-13 20:03
    More details?
    The duty cycle is constantly changing from 1% to 95% high.
    The circuit will be in one pin, out another pin.

    I did the samples shown in AN001 Propeller counters.

    Post Edited (Pliers) : 10/14/2008 10:10:50 AM GMT
  • AribaAriba Posts: 2,687
    edited 2008-10-13 21:24
    This should be possible with 2 cogs, 1 for measuring the Pulswith of the 5 kHz and 1 for the 1 kHz PWM generation.
    Use the POS-DETECT mode of a counter to measure the high time of the 5 kHz PWM, then you can calculate the DutyCycle.
    (if the 5 kHz is constant: High-time / periode time). This can also be made in Spin, then store the value to the Pulswith control variable of the 1 kHz PWM. This PWM can you generate with the code from page 7 of the AN001.

    I modified this PWM code from the AN001 with the POSDETECT measuring and get this code:
    ''Demonstration of PWM version of NCO/PWM counter mode 
    CON _clkmode = xtal1 + pll16x 
        _xinfreq = 5_000_000
    
        INPIN  = 1        '<-- set your Pins here
        OUTPIN = 2 
     
    VAR long parameter, pinmask 
     
    PUB go | x
       
      ctra := %01000 << 26 + INPIN      'Establish POS DETECT mode on INPIN 
      frqa := 1
      pinmask := 1<<INPIN
      cognew(@entry, @parameter)        'start assembly cog
      phsa~ 
      repeat
        waitpeq(pinmask,pinmask,0)      'wait until high
        waitpne(pinmask,pinmask,0)      'wait until LOW
        parameter := phsa * 5           'copy hightime to PW (*5 for 5kHz->1kHz)
        phsa~                           'and clear hightime counter
     
    DAT 
    ''assembly cog fetches the value in parameter for PWM perecentage 
            org 
     
    entry   mov dira, diraval              'set APIN to output 
            mov ctra, ctraval              'establish counter A mode and APIN 
            mov frqa, #1                   'set counter to increment 1 each cycle 
     
            mov time, cnt                  'record current time 
            add time, period               'establish next period 
     
    :loop   rdlong value, par              'get an up to date pulse width 
            waitcnt time, period           'wait until next period 
            neg phsa, value                'back up phsa so that it trips "value" cycles from now 
            jmp #:loop                     'loop for next cycle 
     
    diraval long |< OUTPIN                 'APIN 
    ctraval long %00100 << 26 + OUTPIN     'NCO/PWM APIN 
    period  long 80_000                    '1 kHz period (_clkfreq / period)                       
    time    res 1 
    value   res 1
    
    


    This code is not tested!!
    The recalculation of the Pulswith from 5kHz to 1 kHz is just done with multiplying the high-time by 5.
    The Spin code waits for a high->low puls to synchronize, but the exact counting of the pulswith is done by the counter with 12.5ns resolution.

    Hope this helps
    Andy
  • PliersPliers Posts: 280
    edited 2008-10-14 10:01
    Thanks a whole bunch. The code works great.
    I'm looking forward to dissecting it and learning from it.
    Very best regards, amigos.

    Post Edited (Pliers) : 10/14/2008 10:08:48 AM GMT
  • PliersPliers Posts: 280
    edited 2008-10-14 10:11
    Do I need to close this thread?
    What does the thumbs down mean in Ariba's post reply?
  • BaggersBaggers Posts: 3,019
    edited 2008-10-14 10:38
    Don't click on it, as it's "ignore posts from Ariba" and I don't think you'd want to do that after him just helping you [noparse]:)[/noparse]

    Also you don't need to close the thread, but if you want you can just change the title to "The counters (sorted now thanks)" or something similar.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
Sign In or Register to comment.