alternative to millis() or delay() in loop?

If anyone has any suggestions and can help please.

I am using a pro mini to produce 5 PWM outputs driving mosfets and a 5 phase stepper.
The final 6th PWM has to be at the same pre-scaler division.

The data sheet doesn't seem clear about how I can use this to generate an interrupt in my code which could serve as an alternative to millis() or delay() - at about 20 milliseconds.
I'd have to increment a counter each time the interrupt routine was called, waiting until it reached the time elapsed I wanted.

I could take a piece of wire from the PWM output to the pin 2 input, but it seems a bit crude and I'm sure this can be done more appropriately in software.

The final project purpose is driving a telescope at sidereal rate, using encoder feedback from the final axis to determine and correct the rate at which micro steps are produced.

I have split the project into stages.
I will eventually generate an accurate clock interrupt on pin 2 from a 32kHz crystal oscillator and divider.

For testing the microsteps, PWM frequencies and mosfet driver on the bench, I just want to put in a temporary code that steps through the PWM values at about 45 Hz.

Code here to confirm I am using all 3 Timers for PWM.

  //set up test of fast PWM on 5 outputs
  
  void setup()
{
  
  
    
pinMode(6, OUTPUT);//controlled by Timer 0
pinMode(5, OUTPUT);//controlled by Timer 0
pinMode(9, OUTPUT);//controlled by Timer 1
pinMode(10, OUTPUT);//controlled by Timer 1
pinMode(11, OUTPUT);//controlled by Timer 2
pinMode(3, OUTPUT);//controlled by Timer 2


TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = TCCR0B & B11111000 | B00000001; // set timer 0 divisor to 1 for PWM frequency of 62500.00 Hz

TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10) | _BV(WGM11);//Don't get this shift 1's from right _BV
TCCR1A = TCCR1A & B11111000 | B00000001; //8 bit Fast PWM this is duplicate code but now works
TCCR1B = TCCR1B & B11100111 | B00001000; //8 bit Fast PWM this is duplicate code but now works
TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz

TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz



OCR2A = 180;
OCR2B = 50;
OCR1A = 180;
OCR1B = 50;
OCR0A = 180;
OCR0B = 50;
}

void loop()
{}

Post a link to the datasheet for your stepper motor.

The steppers I am aware of don’t need PWM. But there may be many others that are more sophisticated.

…R
Stepper Motor Basics

Robin2:
Post a link to the datasheet for your stepper motor.

The steppers I am aware of don’t need PWM. But there may be many others that are more sophisticated.

…R
Stepper Motor Basics

No stepper “requires” PWM, but PWM is used, far more often than not, to do micro-stepping.

Regards,
Ray L.

simplystupid:
I will eventually generate an accurate clock interrupt on pin 2 from a 32kHz crystal oscillator and divider.

You should consider the divider outputs from a DS3231 IC. It will far outperform the accuracy of almost any 32Khz oscillator that you could build. It would be simpler too.