I have a logic that controls the input of a RC speed controller, for this I use a Fast PWM on the Baby-O
TCCR1A = (1<<COM1A1) | (0<<COM1A0) | (1<<WGM11); // clear OC1A on compare, fast pwm 10 bit (mode14)
TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11); // CS11 prescaler 8
ICR1 = 50000; //native pwm 20 ms = 50 Hz frequency Prescaler 8 Freq 50 = 20.000.000 / 8 * 50 = 50.000
OCR1A = 2400; // At start we send out 0.9 ms, motor is off
When my logic reads some sensors and decides to change the motor rpm it changed the OCR1A value
OCR1A = (2400 + more);
The logic does not match the frequency of the pwm output, if it changes the value of OCR1A when the pwm pulse is being sent it needs 20 ms to deliver the new value to the speed controller and if it is just before there is no time delay.
I want to sync the PID calculation and the PWM output adjustment, the calculation is faster than 20 ms, can I use an interrupt on TIMER1 COMPB to trigger the calculation, in this way I can have my results available just before the next pwm is sent to the RC speed controller.
Can this work next to a PWM
dopid = 1;
in main I will loop the calculation with a statement
if (dopid == 1)
dopid = 0;
And the set OCR1B high enough to have the OCR1A value available just before the ICR value is reached