//ECE476 -- Alex Cerruti and Chris Wherry //STEPTEST80.c -- tests operation of stepper motor //rotates wheel counter-clockwise, clockwise, and reset // with different buttons pushed. #include <90S8515.h> #include //timeout values for each task #define t1 25 #define t2 25 unsigned char reload; //timer 0 reload to set 1 mSec unsigned char time1; //timeout counters unsigned char count; //light states //the three task subroutines void task1(void); void initialize(void); //all the usual mcu stuff //********************************************************** //timer 0 overflow ISR interrupt [TIM0_OVF] void timer0_overflow(void) { //reload to force 1 mSec overflow TCNT0=reload; //Decrement the three times if they are not already zero if (time1>0) --time1; } //********************************************************** //Entry point and task scheduler loop void main(void) { initialize(); //main task scheduler loop while(1) { if (time1==0) task1(); if (time2==0) task2(); } } //********************************************************** //Task subroutines //Task 1 void task1(void) { time1=t1; //reset the task timer if(PINB.0==0) { //make motor step forward twenty steps for(count=0; count<5; count++) { PORTA = 0x01; delay_ms(5); PORTA = 0x02; delay_ms(5); PORTA = 0x04; delay_ms(5); PORTA = 0x08; delay_ms(5); } } else if(PINB.1==0) { //make motor step backward twenty steps for(count=0; count<5; count++) { PORTA = 0x08; delay_ms(5); PORTA = 0x04; delay_ms(5); PORTA = 0x02; delay_ms(5); PORTA = 0x01; delay_ms(5); } } else if(PINB.2 == 0) { //make motor to reset position for(count=0; count<52; count++) { PORTA = 0x01; delay_ms(5); PORTA = 0x02; delay_ms(5); PORTA = 0x04; delay_ms(5); PORTA = 0x08; delay_ms(5); } } else { PORTA = 0x08; //otherwise lock the colorwheel in place } } //********************************************************** //Set it all up void initialize(void) { //set up the ports DDRB=0x00; //PORT D in an input DDRA=0xff; // PORT A is an ouput PORTA=0x00; //PORT C is an output DDRC =0xff; PORTC = 0x00; reload=256-250; //value for 2 Msec for 8 MHz TCNT0=reload; TIMSK=2; //turn on timer 0 overflow ISR (Mega pg. 30) TCCR0=3; //prescalar to 64 //init the task timers time1=t1; //make motor to reset position for(count=0; count<52; count++) { PORTA = 0x01; delay_ms(5); PORTA = 0x02; delay_ms(5); PORTA = 0x04; delay_ms(5); PORTA = 0x08; delay_ms(5); } //crank up the ISRs #asm sei #endasm }