#include <delay.h>
unsigned int motor_step[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000};
void turn(unsigned char direction, int step)
{
static unsigned char i = 0; // the index must be preserved so you can continue from where you left off
unsigned int j = 0;
while(j <= step)
{
j++;
if(direction)
{
i++;
}
else
{
i--;
}
if(i == 255)
{
i = 3;
}
else
if(i == 4)
{
i = 0;
}
PORTB = motor_step[i]; // this may need to e changed to affect only specific pins using bit masking
delay_ms(50);
}
}
void main(void)
{
PORTB = 0x00;
DDRB = 0xF8;
while(1)
{
turn(1, 95); // I'm not sure about left or right direction, they may be inverted
delay_ms(1000);
turnt(0, 95);
delay_ms(1000);
}
}