U of U CS 5780 - Lecture 6 - Debouncing and Matrix Keypads

Unformatted text preview:

CS/ECE 5780/6780: Embedded SystemDesignJohn RegehrLecture 6: Debouncing and Matrix KeypadsInterfacing a Switch to a ComputerPort Pull ConfigurationIPort Pull Select Register (PPSAD, PPSJ, PPSP, PPSM,PPSS, PPST) select pull- up (0) or pull-dow n (1) for the port.IPull Enable Register (PERAD, PERJ, PERP, PERM, PERS,PERT) enables the pull-up or pull-down function.IYou should first set the PPSx register and then the PERxregister to avoid glitches.Port AD Initialization Softwarevoid PortAD_Init(void){ATDDIEN |= 0x03; // PAD1-0 digital I/ODDRAD &= ~0x03; // PAD1-0 inputsPPSAD |= 0x02; // pull-down on PAD1PPSAD &= ~0x01; // pull-up on PAD0PERAD |= 0x03; // enable pull-up and pull-down}Switch BounceHardware Debouncing Using a CapacitorHardware Debouncing Using a Capacitor (cont)Software DebouncingSoftware Debouncing with Gadfly Synchronizationvoid Key_WaitPress(void){while(PTT&0x08); // PT3=0 when pressedTimer_Wait10ms(1); // debouncing}void Key_WaitRelease(void){while((PTT&0x08)==0); // PT3=1 -> releasedTimer_Wait10ms(1); // debouncing}void Key_Init(void){Timer_Init();DDRT &=~0x08; // PT3 is input}Software DebouncingOutput Compare BasicsIUsed to c reate squarewaves, generate pulses, implement timedelays, and execute periodic i nterrupts.I6812 has 8 output compare modules.IEach module has:IAn external output pin (OCn).IA flag bit.IForce compare control bit (FOCn).ITwo control bits (OMn, OLn).IAn interrupt mask bit.I16-bit output compare register.Output Compare Process ExampleIRead the current 16-bit TCNT.ICalculate TCNT+delay.ISet the 16-bit output compare register to TCNT+delay.IClear the output comp are flag.IWait for the output compare flag to be set.Another Approach to Software Debouncingvoid Key_Init(void) {TIOS |= 0x20; // enable OC5 (see Chapter 6)TSCR1 = 0x80; // enableTSCR2 = 0x01; // 500 ns clockDDRT &=~0x08;} // PT3 is inputunsigned char Key_Read(void){unsigned char old;old = PTT&0x08; // Current valueTC5 = TCNT+20000; // 10ms delayTFLG1 = 0x20; // Clear C5Fwhile((TFLG1&0x20)==0){ // 10msif(old!=(PTT&0x08)){ // changed?old = PTT&0x08; // New valueTC5 = TCNT+20000;}} // restart delayreturn(old); }Debouncing Multiple Switches#define MAX_CHECKS 10uint8_t Debounced_State;uint8_t State[MAX_CHECKS];uint8_t Index;void DebounceSwitches(void) {uint8_t i,j;State[Index] = ReadKeys();Index++;j = 0xff;for(i=0;i<MAX_CHECKS-1;i++) {j &= State[i];}Debounced_State ^= j;if(Index >= MAX_CHECKS) { Index = 0; }}Based on ”My favorite software debouncers” by Jack Gannsle.Basic Approaches to Interfacing Multiple KeysRow Out3 Out2 Out1 Out03 0 HiZ HiZ HiZ2 HiZ 0 HiZ HiZ1 HiZ HiZ 0 HiZ0 HiZ HiZ HiZ 0Row Out3 Out2 Out1 Out0 15 14 . . . 015 1 1 1 1 0 HiZ . . . HiZ14 1 1 1 0 HiZ 0 . . . HiZ. . . . . . . . . . . . . . . . . . . . . . . . . . .0 0 0 0 0 HiZ H iZ . . . 04 by 4 Scanned KeypadRow 3 Row 2 Row 1 Row 0 Col 3 Col 2 Col 1 Col 00 1 1 1 a b c d1 0 1 1 e f g h1 1 0 1 i j k l1 1 1 1 m n o p4 by 4 Scanned KeypadIThere are two steps to scan a particular row:1. Select that row by driving low while other rows are not driven.2. Read the columns to see if any keys are pressed in that row(0 means key pressed, 1 means not pressed).IThe scanned keypad operates properly if:1. No key is pressed.2. Exactly one key is pressed.3. Exactly two ke ys are pressed.Software for a Matrix Scanned Keypadconst struct Row{ unsigned char direction;unsigned char keycode[4];}typedef const struct Row RowType;RowType ScanTab[5]={{ 0x80, "abcd" }, // row 3{ 0x40, "efgh" }, // row 2{ 0x20, "ijkl" }, // row 1{ 0x10, "mnop" }, // row 0{ 0x00, " " }};void Key_Init(void){DDRT = 0x00; // PT3-PT0 inputsPTT = 0; // PT7-PT4 oc outputPPST = 0; // pull-up on PT3-PT0PERT = 0x0F;}Software for a Matrix Scanned Keypad (cont)/* Returns ASCII code for key pressed,Num is the number of keys pressedboth equal zero if no key pressed */unsigned char Key_Scan(short *Num){RowType *pt; unsigned char column,key;short j;(*Num)=0; key=0; // default valuespt=&ScanTab[0];while(pt->direction){DDRT = pt->direction; // one outputcolumn = PTT; // read columnsfor(j=3; j>=0; j--){if((column&0x01)==0){key = pt->keycode[j];(*Num)++;}column>>=1;} // shift into positionpt++; }return


View Full Document

U of U CS 5780 - Lecture 6 - Debouncing and Matrix Keypads

Documents in this Course
Lab 1

Lab 1

5 pages

FIFOs

FIFOs

10 pages

FIFOs

FIFOs

5 pages

FIFO’s

FIFO’s

12 pages

MCU Ports

MCU Ports

12 pages

Serial IO

Serial IO

26 pages

Load more
Download Lecture 6 - Debouncing and Matrix Keypads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 6 - Debouncing and Matrix Keypads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 6 - Debouncing and Matrix Keypads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?