Here's another simple program that controls a stepper motor. /* Bill Bai Stepper Motor Controller */ #include <avr/io.h> #include <avr/interrupt.h> #define CLK 20000000 //20MHz clock //Define functions //====================== void ioinit(void); //Initializes IO void stepper(void); void init_Timer0(void); //====================== int main (void) { ioinit(); //Setup IO pins and defaults init_Timer0(); sei(); //unsigned char step[4] = {0x09,0x0C,0x06,0x03}; while(1) { } return(0); } unsigned char step = 0; void stepper(void) { unsigned char step_array[8] = {0x0C,0x04,0x06,0x02,0x03,0x01,0x09,0x08}; if(step==8) step=0; PORTC = step_array[step]; step++; } unsigned int count = 0; ISR(TIMER0_OVF_vect) { if( count == 100 ) //100*1ms { stepper(); } else { count++; } PORTB = PORTB ^ 0xFF; TCNT0 = 0; //reset timer 0 counter } void ioinit (void) { //1 = output, 0 = input DDRB = 0b11111111; //All outputs DDRC = 0b11111111; //All outputs DDRD = 0b11111111; //PORTD (RX on PD0) } //Initialize Timer0 void init_Timer0(void) { TCNT0 = 255; TCCR0A = 0; TCCR0B = 5; TIMSK0 = 1; } |
Projects > ATMega328P >