/************************************************************************ * Term: Fall 2003 * Course: ECE 473 * Name: Olamide Adabonyan / Philip Lechowicz * E-mail: o_mide@yahoo.com / phillech@aol.com * * REVISION HISTORY * * First Draft: Oct 18, 2002 * -LEDs not lighting up correctly * * Date Submitted: ?/??/02 * Demo Approval: ________ ************************************************************************/ #include #include #include #define endProg() asm("swi") #define enableInt() asm("cli") #define disableInt() asm("sei") #define fivePercent 1000 short ontime, offtime; char motorStatus; void initBoardB(void); void OC1handler(void); /******************************************************* Name: initBoardB Purpose: initilize slave board (boardB) - turn LEDs off - turn motor on with PWM duty cycle of 50% - configure SPI - set up OC1 Inputs: none Outputs: none ********************************************************/ void initBoardB() { char dummy; disableInt(); //disable interrupts PORTA = 0x00; ontime = 10000; offtime = 10000; //??? SPI Configuration... [master basically does everything] SPCR = 0x40; DDRD = 0x04; dummy = SPSR; dummy = SPDR; TOC1vector= &OC1handler; TMSK1 = 0x80; TFLG1 = 0x80; TOC1 = TCNT + ontime; enableInt(); //enable interrupts } /******************************************************* Name: OC1handler Purpose: control ontime/offtime for motor (using PWM) Inputs: none Outputs: none ********************************************************/ #pragma interrupt_handler OC1handler() void OC1handler() { if (ontime >= 20000) { ontime = 20000; offtime = 0; TOC1 = TOC1 + ontime; PORTA |= 0x60; //printf("max PWM...\n"); //debugger } else if (offtime >= 20000) { offtime = 20000; ontime = 0; TOC1 = TOC1 + offtime; PORTA = 0x10; //printf("min PWM...\n"); //debugger } else if (motorStatus == 1) { motorStatus = 0; TOC1 = TOC1 + offtime; PORTA ^= 0x40; } else if(motorStatus == 0) { motorStatus = 1; TOC1 = TOC1 + ontime; PORTA ^= 0x40; } TFLG1 = 0x80; } /******************************************************* Name: Main Purpose: - Initialize system by calling initBoardB() function - Wait for SPI messages and calls other functions to do specific tasks, depending on message recieved. - Controls state of LEDs Inputs: none Outputs: none ********************************************************/ main() { char dummy, masterSignal; initBoardB(); while(1) { //dummy = SPSR; //dummy = SPDR; while ((SPSR & 0x80) == 0) { /*check for collision error if((SPSR & 0x40) != 0) { dummy = SPSR; //this clears collision error flag (WCOL) together with next step dummy = SPDR; //read corrupted data printf("\ncollision error\n"); }*/ } //clear SPIF bit in SPSR (SPI transfer complete flag) dummy = SPSR; masterSignal = SPDR; if (masterSignal == 0x00) { if (offtime < 0) offtime = 0; ontime -= fivePercent; offtime += fivePercent; PORTA |= 0x10; if ((PORTA & 0x20) != 0) PORTA ^= 0x20; printf("ontime %d, offtime %d\n", ontime, offtime); //debugger printf("red PWM\n"); } else if (masterSignal == 0x01) { if (ontime < 0) ontime = 0; ontime += fivePercent; offtime -= fivePercent; PORTA |= 0x20; if ((PORTA & 0x10) != 0) PORTA ^= 0x10; printf("ontime %d, offtime %d\n", ontime, offtime); //debugger printf("inc PWM\n"); } /* else { printf("wrong input recieved"); }*/ } //endProg(); //4 debugging... } /* while ((SPSR & 0x80) == 0) { //check for collision error if((SPSR & 0x40) != 0) { dummy = SPSR; //this clears collision error flag (WCOL) together with next step dummy = SPDR; //read corrupted data } printf("just looping"); } //clear SPIF bit in SPSR (SPI transfer complete flag) dummy = SPSR; masterSignal = SPDR; if (masterSignal == 1) { while(1) printf("yah, i got a 1"); } else { while(1) { dummy = masterSignal; printf("nada, didnt get a 1 \n dummy signal = %d", dummy); } } */