Slide 1'&$%ECE/CE 3720: Embedded System DesignChris J. MyersLecture 16: Input Switches and KeyboardsSlide 2'&$%Interfacing a Switch to a Computer(See Figures 8.1, 8.2., and 8.3)1Slide 3'&$%Switch Bouncing(See Figures 8.4 and 8.5)Slide 4'&$%Hardware Debouncing Using a Capacitor(See Figures 8.6 through 8.13)2Slide 5'&$%Software Debouncing(See Figures 8.14 and 8.15)Slide 6'&$%Software Debouncing with Gadfly Synchronizationvoid WaitPress(void){while (PORTA&0x01); // Loop till pressedTOC5=TCNT+20000; // 10ms delayTFLG1=0x08; // Clear OC5Fwhile((TFLG1&0x08)==0);} // wait for bouncevoid WaitRelease(void){while ((PORTA&0x01)==0); // Loop till releasedTOC5=TCNT+20000; // 10ms delayTFLG1=0x08; // Clear OC5Fwhile((TFLG1&0x08)==0); } // wait for bounce3Slide 7'&$%Software Debouncing with Gadfly SynchronizationWaitPress: ldaa PORTA ;PA0=0 if pressedanda #$01bne WaitPress ;loop till pressedldd TCNTaddd #20000 ;10ms delaystd TOC5ldaa #$08staa TFLG1 ;clear OC5FloopP: ldaa TFLG1 ;10ms for bouncinganda #$08 ;OC5F set?beq loopPrtsSlide 8'&$%Software Debouncing with Gadfly SynchronizationWaitRel: ldaa PORTA ;PA0=1 if releasedanda #$01beq WaitRel ;loop until releasedldd TCNTaddd #20000 ;10ms delaystd TOC5ldaa #$08staa TFLG1 ;clear OC5FloopR: ldaa TFLG1 ;10ms for bouncinganda #$08 ;OC5F set?beq loopRrts4Slide 9'&$%Software Debouncing(See Figure 8.16)Slide 10'&$%Software Debouncing with Gadfly Synchronizationunsigned char ReadPA0(void){unsigned char old;old=PORTA&0x01; // Current valueTOC5=TCNT+20000; // 10ms delayTFLG1=0x08; // Clear OC5Fwhile((TFLG1&0x08)==0){ // unchanged for 10ms?if(old<>(PORTA&0x01)){ // changed?old=PORTA&0x01; // New valueTOC5=TCNT+20000; // restart delayTFLG1=0x08; } // Clear OC5F}return(old);}5Slide 11'&$%Software Debouncing with Gadfly Synchronization* Reg B is the return valueReadPA0: ldd TCNTaddd #20000 ;10ms delaystd TOC5ldaa #$08staa TFLG1 ;clear OC5Fldab PORTA ;0 if pressedandb #$01 ;B=old valueSame: ldaa TFLG1 ;10ms bouncinganda #$08 ;OC5F set?bne Doneldaa PORTA ;0 if pressedanda #$01 ;A=new valuecba ;same as beforebeq Samebra ReadPA0 ;different, start overDone: rtsSlide 12'&$%Software Debouncing with Interrupt Synchronization(See Figure 8.18)6Slide 13'&$%Software Debouncing with Interrupt Synchronization// counts the number of button pushes// signal connected to IC3=PA0unsigned int count; // times pushed#define wait 20000 // bounce wait (cyc)void Ritual(void){asm(" sei"); // make atomicTMSK1=0x01; // Arm IC3, disarm OC5TFLG1=0x01; // clear IC3FTCTL2 = 0x01; // IC3F set on risingcount=0;asm(" cli"); }Slide 14'&$%Software Debouncing with Interrupt Synchronization#pragma interrupt_handler TOC5handler()void TOC5handler(void){TMSK1=0x01; // Arm IC3, disarm OC5TFLG1=0x01; // clear IC3Fif(PORTA&0x01==0) count++;}// new count if PA0=0#pragma interrupt_handler TIC3handler()void TIC3handler(void){TMSK1=0x08; // Disarm IC3, Arm OC5TOC5=TCNT+wait;TFLG1=0x08;} // clear OC5F7Slide 15'&$%Software Debouncing with Interrupt Synchronization// counts the number of button pushes, IC3=PA0unsigned int count; // times pushedchar LastState; // looking for touch?// true means open, looking for a touch// false means closed, looking for release#define wait 20000 // bounce wait (cyc)void Ritual(void){asm(" sei"); // make atomicTMSK1=0x01; // Arm IC3, disarm OC5TFLG1=0x01; // clear IC3FTCTL2 = 0x01; // IC3F set on risingcount=0;LastState=PORTA&0x01;asm(" cli"); }Slide 16'&$%Software Debouncing with Interrupt Synchronization#pragma interrupt_handler TOC5handler()void TOC5handler(void){TMSK1=0x01; // Arm IC3, disarm OC5TFLG1=0x01;} // clear IC3F#pragma interrupt_handler TIC3handler()void TIC3handler(void){if(LastState){count++; // a touch has occurredLastState=0;}elseLastState=1; // release occurredTMSK1=0x08; // Disarm IC3, Arm OC5TOC5=TCNT+wait;TFLG1=0x08;} // clear OC5F8Slide 17'&$%Basic Approaches to Interfacing Multiple Keys(See Figure 8.19 and Tables 8.1 and 8.2)Slide 18'&$%Sixteen-Key Electronic Piano(See Figures 8.21 and 8.22)9Slide 19'&$%Software for Direct Connection Keyboardunsigned int KEY; // current pattern#define wait 20000 // bounce time (cyc)void Ritual(void){asm(" sei"); // make atomicTMSK1=0x00; // Disarm OC5DDRC=0; // PortC are inputs from the keyboardPIOC=0x54; //Inp hs, fall, arm, STRB neg pulseKeyBoard(); // Initially readasm(" cli"); }void KeyBoard(void){dummyRead=PIOC; // read statusKEY=(PORTE<<8)+PORTCL;} // Set global,clr STAFSlide 20'&$%Software for Direct Connection Keyboard#pragma interrupt_handler IRQhandler()void IRQhandler(void){PIOC=0x14; // Disarm STAFTMSK1=0x08; // Arm OC5TOC5=TCNT+wait;TFLG1=0x08;} // clear OC5F#pragma interrupt_handler TOC5handler()void TOC5handler(void){TMSK1=0x00; // Disarm OC5PIOC=0x54; // Rearm STAFKeyBoard();} // Read keys, set LS374’s10Slide 21'&$%Software for Direct Connection Keyboardunsigned int Key; // current pattern#define period 20000 // 10ms pollingunsigned int KeyBoard(void){return((PORTE<<8)+PORTC);} // patternvoid Ritual(void){asm(" sei"); // make atomicDDRC=0; // inputs from keyboardKey=KeyBoard(); // read 16 keysTMSK1=0x08; // Arm OC5TOC5=TCNT+period;TFLG1=0x08; // clear OC5Fasm(" cli"); }Slide 22'&$%Software for Direct Connection Keyboard#pragma interrupt_handler TOC5handler()void TOC5handler(void){Key=KeyBoard(); // Current patternTOC5=TOC5+period;TFLG1=0x08;} // ack OC5F11Slide 23'&$%Software for Direct Connection KeyboardKEY: ds 2 ;current valueRITUAL: clr DDRC ; all inputsldaa #$54staa PIOCbsr KeyBoard ;set 74LS374’scli ;Enable IRQrtsKeyBoard: ldaa PIOC ;part of clearldaa PORTE ;Read MSBldab PORTCL ;Read LSB, ackstd KEYrtsSlide 24'&$%Software for Direct Connection KeyboardIRQHan: ldaa #$14 ;Disarm STAFstaa PIOCldd TCNTaddd #20000 ;OC5 10ms laterstd TOC5ldaa #$08 ;Arm OC5staa TMSK1staa TFLG1 ;Clear OC5FrtiOC5Han: clr TMSK1 ;Disarm OC5ldaa #$54 ;Rearm STAFstaa $PIOCbsr KeyBoard ;read keyboardrti12Slide 25'&$%Hardware for Generating Interrupts(See Figure 8.23)Slide 26'&$%4 by 4 Scanned Keyboard(See Figures 8.25, 8.26, and Table 8.3)13Slide 27'&$%4 by 4 Scanned Keyboard• There are two steps to scan a particular row:1. Select that row by driving low while other rows arenot driven.2. Read the columns to see if any keys are pressed inthat row (0 means key pressed, 1 means not pressed).• The scanned keyboard operates properly if:1. No key is pressed.2. Exactly one key is pressed.3. Exactly two keys are pressed.Slide 28'&$%Software for Matrix Scanned KeyboardRitual: ldaa #$40staa PIOC ;CWOM=1ldaa #$F0 ;PC7-PC4 outputsstaa
View Full Document