I'm currently working at a project for school with three stepper motors, for this I'm using a nema17 stepper motor and a DRVB885 stepper motor driver. The driver is controlled by an arduino mega 2560. I want to control three things, the steps from the motor (number of PWM pulses), the speed of the motor (PWM period time) and the direction of the motor. I wrote a program to do these three things, unfortunatly my program doenst work. I get no errors or warnings (like most of the time in c) and its very hard to rotate the shaft of the stepper motor when the program is running. the motor doenst make any noise, this pushes me in to direction that the motor is being directed but duration of the pulses or setup is wrong. anyone an idea?
this is the code that I wrote:
#define F_CPU 16000000ul #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #include "basic_help.h" #define motor_a 1 #define dir_a_pin 1 //A1 - F #define step_a_pin 3 //6 - H void motor_setup() { pinmodeOutput(step_a_pin, &DDRH); pinmodeOutput(dir_a_pin, &DDRF); digitalWrite(en_a_pin,1,&DDRD); // disable motor digitalWrite(dir_a_pin,0,&DDRF); // set initial motor direction TCCR3A = 0; TCCR3B = 0; TCCR3A = (1<<COM3A1) | (1<<COM3B1) | (1<<WGM31) | (1<<WGM30); //clear timer on compare match & set timer at bottom - set pwm to fast pwm - TCCR3B = (1<<CS30) | (1<<CS32) | (1<<WGM33) | (1<<WGM32); //internal clock with CLK/1024 prescaling TIMSK3 = (1<<OCIE3A) | (1<<OCIE3B); //generate intterrupt when matches B register }; int aantal_stappen; ISR(TIMER0_COMPA_vect) //PWM => COMPA MAX, COMPB { if(aantal_stappen>0) { digitalWrite(dir_a_pin,1,&DDRF); // enable direction digitalWrite(step_a_pin,1,&DDRH); // set pin PWM aantal_stappen--; } else if(aantal_stappen<0) { digitalWrite(dir_a_pin,0,&DDRF); // disable direction digitalWrite(step_a_pin,1,&DDRH); // set pin PWM aantal_stappen++; } } ISR(TIMER0_COMPB_vect) { digitalWrite(step_a_pin,0,&DDRH); // reset pin PWM } int main(void) { motor_setup(); OCR0B = 156; // clock of 16Mhz ==> prescaler 1024 ==> 15625 divided by 156 ==> 100hz OCR0A = 50; sei(); aantal_stappen = 1000; while (1) { } } .c file basic_functions void pinmodeOutput(int pin_nr, volatile uint8_t *ddr){ *ddr |= (1<<pin_nr); } void digitalWrite(char pin_nr, int highLow, volatile uint8_t *port){ if(highLow) { *port |= (1<<pin_nr); } else { *port &= ~(1<<pin_nr); } }