Generating PWM from PID to control DC motor

If you do the modulation in a software loop like that your code cannot do anything else, such as
run the PID algorithm.

Answer #5 guides to how to set the frequency, and more control of the PWM units. That's definitely
how to do this. The hardware generates the PWM, the software regularly updates the duty cycle
(and even frequency if that's needed) from the PID output. The software has a high-level role, the
hardware does the grunt work.