The counters (working now thanks to Ariba)
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
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
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
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 1This 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
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
What does the thumbs down mean in Ariba's post reply?
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
·