News:

SMF for DIYStompboxes.com!

Main Menu

square waves and pwm

Started by Dimitree, March 28, 2013, 08:34:13 PM

Previous topic - Next topic

Dimitree

hi everyone
I have a little doubt:
usually to produce a square wave, to be used as the simplest tone possible, we use the PWM of a microcontroller, with 50% duty cycle and at the frequency we want.
but if we program a pin (not a PWM one) to toggle on and off in a loop, with the right timing obviously, we got the same results?
I mean, is the square wave generated from the PWM pin different from a square wave that we could potentially generate from whatever pin? is there any noise, or different voltage, or whatever difference? or in the case of a square wave there are no differences (of course differences occur when we change the voltage of the PWM output, since we can'd to that on normal pins) ?
many thanks and sorry if the question seems stupid

slacker

#1
They're exactly the same, there's no difference between a PWM output and any other output. The PWM outputs just make it easy to do PWM, you tell it what level you want and the micro deals with converting that into the correct duty cycle.
You can do PWM on any output pin by writing your own routine to generate the duty cycle. For example, instead of just having a loop that switches the pin on and off, you use a counter instead that loops repeatedly through 0,1,2,3. You can then say if the counter is less than 2 the output is high else it's low, that give you a 50% duty cycle. If you said less than 1 is high, that gives a 25% duty cycle.

PRR

Very often:

Handy CPUs/PICs have internal hardware PWM circuits. You set rate and duty, and forget about it.

You -can- put the micro in a loop and update the pin high/low as you desire. However this takes away brain-power which could be doing something else. If you need a very high rate, the CPU may not have time for anything else. If it does, that something-else may take different amounts of time depending what it has to do, which will cause your PWM to vary.

If the built-in PWM does what you want, now you know why it is built-in: it leaves the rest of your CPU free to do more interesting things.

If you can't work with the internal PWM, then yes of course it is 1978 all over again, when we called it "bit-boffing" and wrote tightly timed loops of code to do the right thing at the right time. (You can bit-boff an entire NTSC video signal, Lancaster wrote the book.)
  • SUPPORTER

PRR

> when we change the voltage of the PWM output, since we can'd to that on normal pins

The voltage on any output is *only* zero and +4V (or +3V).

In PWM we can set the _average_ voltage from 0% to 100% of that, by setting the low/high ratio 0% to 100%.

We can do that with code-loops:

:REPEAT

for i = 0 to 10
 {i = i + 1};
set OUT1 to HI;
for i = 0 to 90
 {i = i + 1};
set OUT1 to LO;

GOTO REPEAT;


This will give *roughly* 10% high 90% low and about 10% of the 4V high level or 0.4V.

And _if_ the brain does a million instructions a second, it will run about 10,000Hz. (Maybe 9,709Hz, because there are 103 instructions in the loop.)
  • SUPPORTER

MoltenVoltage

if you use the simple pics, they don't have the PWM built in, so you need to roll your own in ASM.

a digital out is a digital out, and an analog out can be a digital out if you just use the top and bottom voltages.

PWM is used to convert a digital out into an analog out by varying the duty cycle, then using some smoothing circuitry.

i prefer to use PICs with DAC's on board to output a clean analog voltage that doesn't require filtering.
MoltenVoltage.com for PedalSync audio control chips - make programmable and MIDI-controlled analog pedals!