/********************************** 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 ); /************************************************************* Function: RIThandler Purpose: This interrupt is used to toggle the status LDE of the master board every one second. Inputs: None Outputs: None **************************************************************/ void RTIhandler(void){ if((TFLG2&0x4F)!=0x40) /* Illegal interrupt detection */ asm("swi"); /* If illegal interrupt detected exit */ 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++; /* Otherwise increment the counter */ } /************************************************************* Function: TOC5handler Purpose: This interrupt occurs after any switch is pressed to avoid reading signal while the switch is bouncing. The interrupt inables the IC interupts and disables the OC interupts for the next time the switch is pressed. Inputs: None Outputs: None **************************************************************/ void TOC5handler(void){ printf("\nWaiting for switch debounce..."); OCoffICon(); /* Arm IC dissarm OC5 */ TFLG1=(bit0|bit1); /* Clear the ICFlags */ } /************************************************************* Function: TIC1handler Purpose: This interrupt occurs after switch one is pressed The interrupt enables OC5 for debouncing and dissables IC interupts. Additionally, it sets the global variable switchon to the corresponding switch code 1 for switch one 2 for switch two and 4 for switch three. Inputs: None Outputs: None **************************************************************/ void TIC1handler(void){ OConICoff(); /* turn OC interupt on and IC interupts off */ TOC5=TCNT+DELAY; /* reschedule the OC5 interrupt */ TFLG1=bit3; /* Reset the OC5 flag */ switchon=bit0; /* Set the global variable with the switch pressed code */ printf("\nSwitch 1 Pressed"); } /************************************************************* Function: TIC2handler Purpose: This interrupt occurs after switch two is pressed The interrupt enables OC5 for debouncing and dissables IC interupts. Additionally, it sets the global variable switchon to the corresponding switch code 1 for switch one 2 for switch two and 4 for switch three. Inputs: None Outputs: None **************************************************************/ void TIC2handler(void){ OConICoff(); /* turn OC interupt on and IC interupts off */ TOC5=TCNT+DELAY; /* reschedule the OC5 interrupt */ TFLG1=bit3; /* Reset the OC5 flag */ switchon=bit1; /* Set the global variable with the switch pressed code */ printf("\nSwitch 2 Pressed"); } /************************************************************* Function: TIC3handler Purpose: This interrupt occurs after switch three is pressed The interrupt enables OC5 for debouncing and dissables IC interupts. Additionally, it sets the global variable switchon to the corresponding switch code 1 for switch one 2 for switch two and 4 for switch three. Inputs: None Outputs: None **************************************************************/ void TIC3handler(void){ OConICoff(); /* turn OC interupt on and IC interupts off */ TOC5=TCNT+DELAY; /* reschedule the OC5 interrupt */ TFLG1=bit3; /* Reset the OC5 flag */ switchon=bit2; /* Set the global variable with the switch pressed code */ printf("\nSwitch 3 Pressed"); } /************************************************************* Function: initRTI Purpose: This function is used to initialize the RTI on the HC11. Inputs: None Outputs: None **************************************************************/ /* Initialize our RTI and enable RTI interupts to start toggleing the status LED */ void initRTI(void){ PACTL=(0xFC&TMSK2)|3|bit7; /* Set RTI rate to 3, 32.77ms */ TMSK2|=bit6; /* Arm RTI */ rticount=0; /* Initialize global data structures */ } /************************************************************* Function: OCoffICon Purpose: This function is used to initialize the IC and also turn the IC interupts on and the OC5 interupts off. Inputs: None Outputs: None **************************************************************/ void OCoffICon(void){ TMSK1=(bit0|bit1|bit2); /* Sets the Input Capture ON for PORTA pins 1,2,3 */ TFLG1=(bit0|bit1|bit2); /* Clears the Input Capture Flags for IC1, IC2, IC3*/ TCTL2 = (bit0|bit2|bit4); /* Set IC1 and IC2 interrupts on any edge */ } /************************************************************* Function: OConICoff Purpose: This function is used to initialize the OC and also turn the IC interupts off and the OC5 interupts on. Inputs: None Outputs: None **************************************************************/ void OConICoff(void){ TMSK1=bit3; /* Sets the OC5 interupt ON */ TFLG1=bit3; /* Clear the OC5 Flag */ } /************************************************************* Function: initSwitch Purpose: This function is used to initialize the global variable switchon. Inputs: None Outputs: None **************************************************************/ void initSwitch(void){ switchon=0x00; /* Clear the gloabal variable switchon */ } /************************************************************* Function: initSPCR Purpose: This function is used to initialize the IC and also turn the IC interupts on and the OC5 interupts off. Inputs: None Outputs: None **************************************************************/ void initSPCR(void){ SPCR = 0x50; /* Set up the SPI enable SPI/MSTR */ PORTD |= bit5; /* Enable Device as the master */ DDRD |= 0x38; /* Initialize PortD */ dummy = SPSR; /* Clear the SPIF flag via dummy */ dummy = SPDR; /* Clear the SPIF flag via dummy */ } /************************************************************* Function: main Purpose: This is the main function and is used to call the initilization funcions as well as set up the vectors to their corresponding addresses. It also sends the switchon variable to the slave via SPI. Inputs: None Outputs: None **************************************************************/ void main ( void ) { asm("sei"); /* Stop interupts */ /**** INITILIZATIONS FUNCTIONS ****/ initRTI(); initSPCR(); initSwitch(); OCoffICon(); /**** ISR VECTORS ADDRESSES FROM VECTOR.H ****/ RTIvector = &RTIhandler; TOC5vector = &TOC5handler; TIC3vector = &TIC3handler; TIC2vector = &TIC2handler; TIC1vector = &TIC1handler; asm("cli"); /* Start interupts */ while(1) { if(switchon!=0) { SPDR=switchon; /* Send the switchon variable via the SPI */ while((SPSR&bit7)==0); /* While the SPI is sending wait untill its done */ switchon=0; /* Reset the switchon variable after it has been sent via the SPI */ dummy=SPCR; /* Clear the SPIF by dumping the SPCR into a dummy variable */ dummy=SPDR; /* Clear the SPIF by dumping the SPCR into a dummy variable */ } } }