Which Approach is Better to Drive Brushless DC Motor with PWM Duty Cycle
You say that you're going to use a PID controller, but then you start talking about driving the motor's duty cycle directly.  I believe that you do not understand what a PID controller is.

You should use a linear controller, possibly decorated with integrator anti-windup.  Here's a basic block diagram that should work for you.  The 'rotation' block is sort of fictitious; it's just there to go from the motor speed to motor position.

You do not need wacky sequencing with timers and if statements and hundreds of lines of code!  You just need simple linear controllers, as mankind has been making for the last 100 years.







simulate this circuit &ndash; Schematic created using CircuitLab