/*This program is used to test stepper motor*/ #include #include #include #define begin { #define end } // Declare your global variables here unsigned char count,reload,step,stop; unsigned int time0; //********************************************************** //timer 0 overflow ISR interrupt [TIM0_OVF] void timer0_overflow(void) begin //reload to force 1 mSec overflow TCNT0=reload; //clear the flag, task1 should be executed time0++; end //********************************************************** void initialize(void) { DDRA=0xff; PORTA=0xff; //output LED DDRB=0xff; PORTB=0x00; DDRD=0xff; PORTD=0x00; //timer0 is used to control the turning speed time0=0; reload=256-125; TCCR0=0x03 ; TCNT0=reload; TIMSK=1; OCR0=0x00; stop=0; count=1; #asm sei #endasm } void leftsteer(void) { time0=0; step=0; while (step<4) //we can change number of steps the motor turns { if (time0==2000) //to clearly see the step, we put delays to the program { if (count==4) count=1; else count++; if (count==1) PORTA=0b00001010; else if (count==2) PORTA=0b00101000; else if (count==3) PORTA=0b10100000; else if (count==4) PORTA=0b10000010; step++; time0=0; } } } void rightsteer(void) { //same as leftsteer except the sequence is reversed time0=0; step =0; while (step<4) { if (time0==2000) { if (count==1) count=4; else count--; if (count==1) PORTA=0b00001010; else if (count==2) PORTA=0b00101000; else if (count==3) PORTA=0b10100000; else if (count==4) PORTA=0b10000001; step++; time0=0; } } } void main(void) { initialize(); count=1; while (1) { //perform 1 left turn and 1 right turn if (stop!=1) { leftsteer(); rightsteer(); stop=1; } } }