/********************************** Nick Sitarski Vladi Gergov Lab 2 ECE 473 W 2003 Professor shaout Slave **********************************/ #include #include #include /********************************** BIT MASK **********************************/ #define bit0 0x01 #define bit1 0x02 #define bit2 0x04 #define bit3 0x08 #define bit4 0x10 #define bit5 0x20 #define bit6 0x40 #define bit7 0x80 /********************************** PRAGMA DEFINES **********************************/ #pragma interrupt_handler TOC1handler() /********************************** FUNCTION PROTOTYPES **********************************/ /********************************** Global Variables ***********************************/ double dutyon; //will be the on time duty cycle double dutyoff; //will be the off time duty cycle unsigned char dummy; //Dummy variable used in setting and clearing for SPI unsigned char switch; //variable to see if the input is changed from 0 unsigned char onoroff; //variable to tell wether the motor is on or off void TOC1handler(void){ // asm("sei"); if (!(TFLG1&bit7)) { TFLG1 = bit7; //Re-enable interrupt return; } if(onoroff==0){ PORTA=PORTA|bit3; TOC1=TCNT+dutyon; onoroff=1; } if(onoroff==1){ PORTA=PORTA&0x70; TOC1=TCNT+dutyoff; onoroff=0; } TFLG1=0; // asm("cli"); } void initDDRD(void){ //initialized DDRD for using the SPI communication DDRD=bit2; } void initSPCR(void){ //Function for setting up the SPI communication SPCR=0xc7; PORTD |= !bit5; /* Enable Device as the slave */ DDRD |= 0x38; /* Initialize PortD */ dummy=SPSR; dummy=SPDR; } void initDUTYCYC(void){ //this will initialize the duty cycle .5 of a cycle will be 10000 cycles dutyon=10000; dutyoff=10000; } void main(void){ asm ("sei"); //disables all interupts PORTA=0; //Turns off everything TFLG1=0; //resets the oc1 flag initDDRD(); //initialize everything initSPCR(); initDUTYCYC(); onoroff=0; TOC1vector = &TOC1_ISR; asm ("cli"); while(1){ SPDR=0; while((SPSR&bit7)!=0) //waits for command transmission and resets SPI switch=SPDR; dummy=SPCR; dummy=SPDR; if(switch==1){ //turns on red light hooked to porta pin 6 and disable oc1 interupt TMSK1=0; PORTA=bit6; switch=0; } if(switch==2){ //turns on green led hooked to PORTa pin 5 and turns on OC1 interupt TMSK1=bit7; //also this resets the duty cycle. PORTA=bit5; initDUTYCYC(); } if(switch==4){ PORTA=bit4; if(dutyon*1.05>20000){ dutyon=20000; } else dutyon=dutyon*1.05; dutyoff=20000-dutyon; } } }