Variable Frequency, Fixed Duty Cycle PWM

Sodapep

Member
Hello!

I'm looking to create a clock to drive a MAX7401 filter (datasheet: http://datasheets.maximintegrated.com/en/ds/MAX7401-MAX7405.pdf) using an 08M2. The datasheet states that a clock with a 40-60% duty cycle powered from 0 to VDD (5v for the 7401) should be used. The equation to determine the corner frequency, which can range from 1Hz to 5kHz, is corner frequency = clock frequency/100. To get the full range of filter, the PWM would need to from 100Hz to 500kHz. While getting the whole sweep would be great, it is probably not all that useable to me. Perhaps something in the line of clock frequency 10kHz to 200kHz (or 100Hz to 2kHz corner frequency) would be good to play around with, unless it's just as easy to get the 1Hz to 5kHz range.

I used the pwmout wizard in the programming editor to figure out what the code would be for each extreme and it puts out:

pwmout pin, period, duty cycles
pwmout 2,99,200 for 10kHz @50% duty cycle
pwmout 2,4,10 for 200kHz @50% duty cycle

So it seems that period and duty cycle are related, somehow. Doing a little math, it looks like they are related linearly, with duty cycle = (2*period)+2.

So I guess my question is, am I on the right track to get this accomplished, or is there an easier way? I figure I can have the chip read a variable voltage with readadc10, scale that into the period range of 4 to 99, then use the (2*period)+2 for the duty cycle? Will the pwmout accept values with decimal points that the scaling of the readadc to period is sure to put out?

Thank you for your guidance!
 

hippy

Technical Support
Staff member
You are on the right track. PICAXE doesn't handle non-integers; you will simply get rounding down.

ReadAdc POT_PIN, b0
b0 = b0 * 95 / 255 + 4
w1 = b0 * 2 + 2
PwmOut PWM_PIN, b0, w1
 

Sodapep

Member
You are on the right track. PICAXE doesn't handle non-integers; you will simply get rounding down.

ReadAdc POT_PIN, b0
b0 = b0 * 95 / 255 + 4
w1 = b0 * 2 + 2
PwmOut PWM_PIN, b0, w1
Thank you for the reply and code! I was hoping that a PWM would be sufficient for the 'clock' that they state in the data sheet. At 50% duty cycle, it's basically a square wave pulse, yah? I have a few follow up questions for anyone still tuned in.

Using readadc10 probably wouldn't make any more sense since it would involve a lot more decimals which would be rounded right?

Also, could I drive multiple 7401's from one PWM out? If the maximum a PICAXE can put out on one pin is 20mA, and the datasheet says the clock input current max is 60uA, it should have no problem driving quite a few?
 

premelec

Senior Member
Yes you can drive a lot of non-TTL loads - remember short leads and bypass capacitors anyhow. If you want precise 50% feed pulses to a flipflop divide by 2 IC and it'll come out squared up...
 

hippy

Technical Support
Staff member
Using readadc10 probably wouldn't make any more sense since it would involve a lot more decimals which would be rounded right?
In this case I believe that is correct and you could not use ...

ReadAdc10 POT_PIN, w0
w0 = w0 * 95 / 1023 + 4

As w0*95 could overflow the 16-bit maths limit the maths would need to be done differently.

As you need to get fewer period values than there are pot position values it's not worth the extra work of using a higher resolution ADC input.
 

Sodapep

Member
permelec - interesting idea, with the frequency divider. So I can feed the PWM direct to the clock input of one side of a 4013 set up as a frequency divider, and get an exact 50% square wave, regardless of the duty cycle of the PWM fed in? If that were the case, I could just read the pot value into the period to set the PWM between 200Hz to 1MHz (can it go that fast?). If the PWM still has to be 50% going into the flip flop, I'm afraid I've missed the point of why it would be useful :(

hippy - thanks for the clarification!
 

Goeytex

Senior Member
So I can feed the PWM direct to the clock input of one side of a 4013 set up as a frequency divider, and get an exact 50% square wave, regardless of the duty cycle of the PWM fed in?
That is correct. The output will be input Freq / 2 at 50 percent duty. To configure, tie S & R to ground. Then tie the not Q output to Data In and apply the PWM to clock.

Another option might be to use a CD4040 12- Bit Ripple Counter. Apply the PWM to the clock input. The Q1 output will be Clock/2, Q2 = Clock /4 , Q3 = Clock /8 and so on.

So, for example, with an input clock of 512KHz you will have outputs of 256KHz at Q1 down to 125Hz at Q12. (Q10 will be 500Hz). And all will be 50% duty.
 
Last edited:
Top