Hello all.
I am trying to control a stepper motor's velocity with the integrated 8-bit timer2 in an Arduino Uno.
I had set the timer compare interrupt at 255Hz. The motor has 200 steps per revolution, so I was expecting 1,275 rounds per second but only got vibrations on the stepper.
here's my source code
unsigned long tiempo = 0;
int divisor = 0;
void setup() {
// put your setup code here, to run once:
// TIMER1 SETUP
cli(); //PARO LAS INTERRUPCIONES
//set timer2 interrupt at 255Hz
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 255hz increments
OCR2A = 246;// = (16*10^6) / (255*255) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS21 bit CS22 for 256 prescaler
TCCR2B |= (1 << CS22 | 1 << CS21);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();
Serial.begin(115200);
tiempo = millis();
}
ISR(TIMER2_COMPA_vect){
digitalWrite(8,HIGH); //here are the pulses to the motor
digitalWrite(8,LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
What am I doing wrong?
The stepper motor is a NEMA17. Here's the technical details about it
https://reprapworld.com/products/motors/nema17_stepper_motor_1_8_degrees_step_4_8_kg_cm/
Thanks in advance and my apologies if I had grammar mistakes.
Zeke