Creating Phase Correct PWM for stepper motor

Thread Starter

mahmud1994

Joined Jul 16, 2017
23
#include <avr/io.h>
#include <avr/interrupt.h>


int main(void)
{
DDRB = 0xFF;

TCCR1A |= 1<<WGM11;
TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
TIMSK |= 1<<OCIE1A;

ICR1 = 19999;

sei();

while (1)
{
if (TCNT1 > 2000 && TCNT1 <6000)
{
if(TCNT1 >= 2100 && bit_is_set(PORTB,PINB0)) PORTB &= ~(1<<PINB0);
if(TCNT1 >= 2700 && bit_is_set(PORTB,PINB1)) PORTB &= ~(1<<PINB1);
if(TCNT1 >= 3800 && bit_is_set(PORTB,PINB2)) PORTB &= ~(1<<PINB2);
if(TCNT1 >= 5500 && bit_is_set(PORTB,PINB3)) PORTB &= ~(1<<PINB3);

}


}
}

ISR(TIMER1_COMPA_vect)
{
PORTB = 0xFF;
}


This is my code for generating 4 Fast PWM in PORTB for stepper motor running. however if i change the TCNT1 value it only run to certain degree then motor start to vibrate so much. My concept for the time period i used for pin0 suppose 0- 5ms. then next wave form for pin 1 will generate from 1 ms to 7ms however when it(pin1) reached 2ms then the value for pin0 will decrease 5 to 4 ms and decrease to 0 as pin 1 increase. next two pin follow the same. how can i implement this in this code. or what to do???
 
Top