//used 20,000 for PWM period therefore ontime and offtime //are changed by a difference of 1000 (5% of 20000) #include #include #include #define endProg() asm("swi") #define enableInt() asm("cli") #define disableInt() asm("sei") #define fivePercent 1000 short ontime, offtime; char motorStatus, lightStatus; 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; PACTL = 0x08; //configure PA3 as output ontime = 10000; offtime = 10000; lightStatus = 2; //??? SPI Configuration... [master basically does everything] //SPCR = 0x40; dummy = SCSR; dummy = SCDR; TOC1vector= &OC1handler; TMSK1 = 0x80; TFLG1 = 0x80; TOC1 = TCNT + ontime; printf("init complete..., interrupts started...\n\n"); printf("init ontime %d, offtime %d\n", ontime, offtime); //debugger enableInt(); //enable interrupts } /******************************************************* Name: OC1handler Purpose: - control ontime/offtime for motor (using PWM) Inputs: none Outputs: none ********************************************************/ /* #pragma interrupt_handler OC1handler() void OC1handler() { if (motorStatus == 1) { motorStatus = 0; if (ontime >= 20000) TOC1 = 20000; else TOC1 = TOC1 + offtime; PORTA |= 0x08; } else if(motorStatus == 0) { motorStatus = 1; if (offtime >= 20000) { TOC1 = 20000; printf("dec..."); } else TOC1 = TOC1 + ontime; PORTA ^= 0x08; } TFLG1 = 0x80; } */ #pragma interrupt_handler OC1handler() void OC1handler() { if (ontime >= 20000) { ontime = 20000; offtime = 0; TOC1 = TOC1 + ontime; PORTA |= 0x60; printf("max PWM...\n"); } else if (offtime >= 20000) { offtime = 20000; ontime = 0; TOC1 = TOC1 + offtime; PORTA = 0x10; printf("min PWM...\n"); } else if (motorStatus == 1) { motorStatus = 0; TOC1 = TOC1 + offtime; PORTA ^= 0x40; //printf("status-on \n ontime %d, offtime %d\n", ontime, offtime); //debugger //printf("on"); } else if(motorStatus == 0) { motorStatus = 1; TOC1 = TOC1 + ontime; PORTA ^= 0x40; //printf("status-on \n ontime %d, offtime %d\n", ontime, offtime); //debugger //printf("off"); } 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. - Turns on correct LED (depending on button pushed from master) Inputs: none Outputs: none ********************************************************/ main() { char dummy, masterSignal; initBoardB(); /* while(1) { 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 } } //clear SPIF bit in SPSR (SPI transfer complete flag) dummy = SPSR; masterSignal = SPDR; */ //=================delete this======================= while(1) { dummy = SCSR; dummy = SCDR; while ((SCSR & 0x20) == 0) { //clear SCDR dummy = SCSR; masterSignal = SCDR; //printf("waiting..."); } //===============delete this===================== if (masterSignal == 0x30) { 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 == 0x31) { 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"); } } }