1
\$\begingroup\$

I'm doing a project and I need the microcontroller (ATmega328) to do these two things:

  • Generate a rectangular wave to be filtered and obtain a DC voltage (that varies linearly with the duty cycle)
  • Generate a square wave (50% duty cycle) with a certain period

The micro will be reading its input pins constantly because the DC voltage and the period needed can change at any moment.

I had thought of doing the first one with PWM and the second one manually setting an output pin or clearing it, counting the period with a timer. However, my question is: can these two things be done together?

I'm thinking that in the code, the microcontroller will be able to follow a routine at a time. So how could I generate these two waves simultaneously (if possible)? If it can be done, could two different PWMs output be used to generate two different signals instead of using a timer and generating the second one manually?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Each OCnx pin of the ATmega328 can output its own independent PWM waveform. Additionally, each timer ("n") can have its own frequency and/or phase. Additionally, the ATmega328PB has a pin that can output a waveform that is a modulation of two waveforms generated from separate timers.

Since you say "the DC voltage and the period needed can change at any moment", read the datasheet carefully to find out when you can safely change the values that are used to generate the waveforms. If the values are changed at the wrong time then the waveform can glitch, giving you a signal that does not have a 50% DC.

\$\endgroup\$
2
  • \$\begingroup\$ Hi Ignacio, thanks for your answer. So the two signals can be generated simultaneously? And regarding the last thing you said, I didn't get it. Why do you say that there can be a time when it would be wrong to change the values? \$\endgroup\$
    – Tendero
    Commented Dec 6, 2016 at 3:03
  • \$\begingroup\$ Take a look at the timing diagrams in the timer sections of the datasheet as well as the wavegen selection table of the register description subsection of same. \$\endgroup\$ Commented Dec 6, 2016 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.