/********************************** Nick Sitarski Vladi Gergov Lab 2 ECE 473 W 2003 Professor shaout Master **********************************/ #include #include #include /********************************* EQUATES **********************************/ #define RTIF 0x40 #define DELAY 30000 /********************************** 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 /********************************** GLOBAL VARIABLES **********************************/ unsigned int rticount=0; /* Global variable to count the number of times the RTI interrupt has happend */ unsigned int switchon; /* FLAG used to know when a switch has been pressed */ unsigned char dummy; /* Dummy variable to clear the SPIF flag */ /********************************** PRAGMA DEFINES **********************************/ #pragma interrupt_handler RTIhandler() #pragma interrupt_handler TOC5handler() #pragma interrupt_handler TIC1handler() #pragma interrupt_handler TIC2handler() #pragma interrupt_handler TIC3handler() /********************************** FUNCTION PROTOTYPES **********************************/ void OCoffICon( void ); void OConICoff( void ); void RTIhandler(void){ // asm("sei"); if((TFLG2&0x4F)!=0x40) /* Illegal interrupt */ asm("swi"); TFLG2=RTIF; /* Acknowledge by clearing ( set to clear ) RTIF */ if(rticount==30){ /* If count reaches 30 then toggle status LED and reset count */ rticount=0; PORTA^=bit6; /* Toggle PORTD pin 1 which strobes the LED */ } else rticount++; // asm("cli"); } void TOC5handler(void){ // asm("sei"); printf("\nWaiting for switch debounce..."); OCoffICon(); /* Arm IC dissarm OC5 */ TFLG1=(bit0|bit1); /* Clear the ICFlags */ // asm("cli"); } void TIC1handler(void){ // asm("sei"); OConICoff(); TOC5=TCNT+DELAY; TFLG1=bit3; switchon=bit1; printf("\nSwitch 1 Pressed"); // asm("cli"); } void TIC2handler(void){ // asm("sei"); OConICoff(); TOC5=TCNT+DELAY; TFLG1=bit3; switchon=bit2; printf("\nSwitch 2 Pressed"); // asm("cli"); } void TIC3handler(void){ // asm("sei"); OConICoff(); TOC5=TCNT+DELAY; TFLG1=bit3; switchon=bit3; printf("\nSwitch 3 Pressed"); // asm("cli"); } void initRTI(void){ /* Initialize our RTI and enable RTI interupts to start toggleing the status LED */ PACTL=(0xFC&TMSK2)|3|bit7; /* Set RTI rate to 3, 32.77ms */ TMSK2|=bit6; /* Arm RTI */ rticount=0; /* Initialize global data structures */ } void OCoffICon(void){ TMSK1=(bit0|bit1|bit2); /* Sets the Input Capture ON for PORTA pins 1,2,3 */ TFLG1=(bit0|bit1|bit2); TCTL2 = (bit0|bit2|bit4); /*Set IC1 and IC2 interrupts on any edge*/ } void OConICoff(void){ TMSK1=bit3; /* Sets the OC5 interupt ON */ TFLG1=bit3; } void initSwitch(void){ switchon=0; } void initSPCR(void){ SPCR=0x57; dummy=SPSR; dummy=SPDR; } void main ( void ) { asm("sei"); /* Stop interupts */ initRTI(); initSPCR(); initSwitch(); OCoffICon(); RTIvector = &RTIhandler; TOC5vector = &TOC5handler; TIC3vector = &TIC3handler; TIC2vector = &TIC2handler; TIC1vector = &TIC1handler; asm("cli"); /* Start interupts */ while(1){ if(switchon!=0){ SPDR=switchon; while((SPSR&bit7)!=0) switchon=0; dummy=SPCR; dummy=SPDR; } } }