Slow duty cycle application

RobertN

Member
I have an application for converting 2 70- 90 Hz duty cycle inputs into one 15 to 20 Hz dutycycle output. The input duty cycles are 0 to 40%, and the output is 15 to 85%. Not calling this a PWM application, since PWM is way to fast. The 2 inputs are 180 degrees out of phase and do not overlap. Would pulse in work best for the inputs? What would be best for the output toggleing, coding hi and low, or some other approch? Could a 08M do this with any precision? Any comments or suggestions appreciated.

66 year old retired newbe.
 

Dippy

Moderator
Tricky. Not a five minute job.

Just a question (as I haven't got the answer) re: the 2 inputs:- Are they identical frequencies and duties? i.e. just out of phase? i.e. do you need to look at 2 inputs?

Or is there somthing more to it i.e. you are mathematically 'mixing' the 2 inputs?
 

hippy

Ex-Staff (retired)
I think "tricky" sums it up. You not only have varying frequency but also varying duty cycle, so that is the first complication, even assuming there's only one input signal and one output signal derived from that.

Assuming it is possible to determine both ( PULSIN would probably be the best candidate ) there's then the problem of reading the signal while outputting another. You cannot stop to read the input signal as that will stop the output, but you're going to have to stop to use PULSIN and do whatever maths you need to. That's not going to be quick.

You need two autonomous systems, one generating a continuous output, the other determining and telling it what to do. Two PICAXE's could possibly do that. The obvious link would be serial but that could be too slow ( 4mS to send data at 4800 baud ) although you could use a faster PICAXE.

Ideally the generator should just be pulling up the latest instructions used to generate its desired output, ideally being set with no need to do any explicit fetching or transfer of those instructions. The X1 background receive or I2C networking would probably be useful there.
 

RobertN

Member
20 Hz PWM Scheme

This 20 HZ PWM output scheme uses a closed loop RC signal to generate a 20 HZ PWM output from 70 -90 HZ PWM pulsin values. It does not work as well as desired.

The simple closed loop RC circuit is driven by an on - off output with the resulting quasi triangular signal monitored by a ADC input. If-then statements switch the output on and off depending on the ADC signal level.

PWM output is generated by comparing the pulsin value to the RC signal level with pair of if-then's.

The resulting PWM output resolution is very course with about 5 steps for full range input. It is not known if this is a limitation of the 08M and/or code.


Present code;

main

pulsin 3,4,w1

readadc 1,w0

let w2=w1/2

if w0 < 25 then high 4 endif

if w0 > 211 then low 4 endif

if w2 > w0 then high 0 endif

if w2 < w0 then low 0 endif

goto main


No pauses. pwmout, etc were used to allow the code to cycle rapidly.

I'm not sure if the pulsin stops code operation while measuring the pulse. If it does, that maybe limiting the resolution and response.

The w1/2 was tried to increase the output resolution with out apparent affect.

The second pulse in has not been tried, it may not be nessesay and would not have a proportional fuction.

Any comments and suggestions appreciated. There maybe a better approch or refinement that will obtain more output resolution.

This my first attempt at writing code since a Basic course thirty some years ago. My 66 year old brain has a trouble wrapping its self around this stuff. It has been interesting, and I think the Picaxe system is well done.
 

inglewoodpete

Senior Member
Robert, Firstly I don't fully understand the application. However, a few pointers on your code.

The PulsIn is a foreground command: code execution stops until either the pulse finishes or the 0.65536 Second timeout occurs (returns a zero in the register). Also, the second parameter of the PulsIn should be either 0, 1 or a variable b0 - b13 (b27 is X1's). It indicates the start condition (1: start on low-to-high or 0: high-to-low)

On the other hand, PWM is a background task: set and forget.

Code:
Main: PulsIn 3, 1, w1        'Start pulse timer on rising leading edge
        ReadADC 1, w0
        Let w2 = w1 / 2
        If w0 < 25 Then
            High 4
        ElseIf w0 > 211 Then
            Low 4
        EndIf

        If w2 > w0 Then
            High 0
        ElseIf w2 < w0 Then
            Low 0
        EndIf
        Goto Main
 

RobertN

Member
Object of slow PWM

Thanks for the reply, and indicating the pulsin in a foreground command. If every thing stops for pulsin that messes up the scheme.

The object is to reduce the PWM frequency from 70 - 90 Hz to 15 - 20 Hz while retaining the pulse width intellegence. ie, Divide the frequency by 5 and increase the pulse time by 5, so the mark space ratio remains the same. The frequency of the 08M PWM output is way to fast.

The RC circuit was an attempt to get a 20Hz time base, but pulsin messes that up.

Some how the pulse in has to be measured and converted to a pulse out with 5 times the duration and a 1/5 the repetition rate.
 

RobertN

Member
Slow pulse width - frequency conversion

Code to read pulsin, multiply, then pulsout is straight forward, as long as the input and output pulses do not occur concurrently. Unfortunately they do, the longer pulsout overlaps an input pulse resulting in a missed pulse in. Is there way in code to count or keep track of the number of input pulses when a pulsout is active?
 

womai

Senior Member
One solution is to use the multitasking code snippet I posted a while a go:

http://www.picaxeforum.co.uk/showthread.php?t=8541

E.g. set it up so the time interrupt gets called every 1ms (1000 events per second). In the interrupt routine, you'd keep two set of counters. One set counts the number of calls that the incoming signal is high and low, respectively. This yields the incoming period and duty cycle. After each incoming period you can update the required duration for the outgoing signal. E.g. lets assume the frequency multiplier should be 5, and the incoming signal's last period was 60 times high, 40 times low, so incoming period is (60+40)=100ms and incoming duty cycle is 60/(60+40)=60%. Outgoing signal should thus stay high for 60*5=300 calls, and then low for the subsequent 40*5=200 interrupt calls. You'd use the second set of counters to determine when to toggle the output signal.

Some tweaking and refinement may be required to to some averaging of the incoming signal timing measurement, and to determine the optimum update rate pf the outgoing signal's parameters. I'd also recommend overclocking to 16 MHz to reduce the timing granularity. (1ms interrupt interval is still a bit optimistic, given the 800 commands/sec execution speed of a Picaxe at 16 MHz).

On the upside, that approach will run in the background and you are free to have your main program do any other task you like.

Wolfgang
 

RobertN

Member
Wolfgang,

Thats something that may work, the timing resolution should be more like .1 mS. And, I would like to use the smallest physical package (have a penchant for making things small).

Is there a way to peek at an internal clock to pick up time counts? If so input and output pulses could be handled without using pulsin or pulsout?

Otherwise I looking at a code managed capacitive stair step generator to count input pulses.
 
Top