36
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Embed Size (px)

Citation preview

Page 1: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

UNIT 8

Keypad Interface

Contact Closure Counter

Exceptions (Interrupts and Reset)

Page 2: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Keypad Figure 5.5

Use as Outputs

Use as Inputs

Page 3: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interface Keypad to Port H

• Port H[3:0] – Output– Full Drive

• Port H[7:4] – Input– Pulls are Optional since Pull-ups on Keypad– Disable Interrupts

Page 4: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Port H Definitions

/* Port H Port Definitions */#define PTH _P(0x260)#define PTIH _P(0x261)#define DDRH _P(0x262#define RDRH _P( 0x263#define PERH _P( 0x264#define PPSH _P( 0x265#define PIEH _P( 0x266#define PIFH _P( 0x267

Page 5: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Port H Initialization

/* Direction */DDRH = 0x0F;/* Full Drive */RDRH = 0x00;/* Pulls Not Required since pull resistors on keypad */PERH = 0x00;/* Disable Interrupts */PIEH = 0x00;/* Unselect Rows in Keypad */PTH = 0x0F;

Page 6: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Procedure to Read Keypad/* if key is pressed, return ‘0’, ‘1’, …, ‘F’ else ‘z’ */unsigned char keypress(void){unsigned char mask[4]=0x0E,0x0D,0x0B,0x07; //1110, 1101, 1011, 0111 int i,j; char key; for(key=‘z’,i=0;i<4;i++) {PTH = mask[i]; // assert a 0 on a row in keypad for(j=0;j<4;j++) {if((PTH>>4) == mask[j]) //check each column for a returned 0 {if (((i<<2)+j)<10) key = ‘0’+(i<<2)+j; //value = 4*row# + column# else key=‘A’+(i<<2)+j -10;} //different offset for values>9 } } PTH = 0x0F; // unselect row return (key);}

Page 7: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Algorithm Implemented in Ci=0 PTH[3:0] = 0xE // Row 0 j=0 PTH[7:4] = 0xE key=‘0’ j=1 PTH[7:4] = 0xD key=‘1’ j=2 PTH[7:4] = 0xB key=‘2’ j=3 PTH[7:4] = 0x7 key=‘3’i=1 PK[3:0] = 0xD // Row 1 j=0 PTH[7:4] = 0xE key=‘4’ j=1 PTH[7:4] = 0xD key=‘5’ j=2 PTH[7:4] = 0xB key=‘6’ j=3 PTH[7:4] = 0x7 key=‘7’i=2 PTH[3:0] = 0xB // Row 2 j=0 PTH[7:4] = 0xE key=‘8’ j=1 PTH[7:4] = 0xD key=‘9’ j=2 PTH[7:4] = 0xB key=‘A’ j=3 PTH[7:4] = 0x7 key=‘B’

i=3 PTH[3:0] = 0x7 //Row 3 j=0 PTH[7:4] = 0xE key=‘C’ j=1 PTH[7:4] = 0xD key=‘D’ j=2 PTH[7:4] = 0xB key=‘E’ j=3 PTH[7:4] = 0x7 key=‘F’

keycode = 4*i+j;If(keycode<10) key = ‘0’ + keycode;else key = ‘A’ + keycode – 10;

Page 8: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Another ProcedureRead Keypad

/* if key is pressed, return ‘0’, ‘1’, …, ‘F’ else ‘z’ */unsigned char keypress(void){unsigned char mask[16]=0xEE,0xDE,0xBE,0x7E, 0XED,0xDD,0xBD,0x7D, 0xEB,0xDB,0xBB,0x7B, 0xE7,0xD7,0xB7,0x77; int i; char key; for(key=‘z’,i=0;i<16;i++) {PTH = mask[i]; // assert row in keypad if(PTH == mask[i]) {if (i<10) key = ‘0’+i; else key=‘A’+i-10;} } PTH = 0x0F; // unselect row return (key);} //SEE NEXT PAGE

Page 9: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

EE 1110 1110-------key = 0DE 1101 1110-------key = 1BE 1011 1110-------key = 27E 0111 1110-------key = 3

sent out

EB 1110 1011-------key = 8 DB 1101 1011-------key = 9 BB 1011 1011-------key = A 7B 0111 1011-------key = B

E7 1110 0111-------key = CD7 1101 0111-------key = DB7 1011 0111-------key = E77 0111 0111-------key = F

ED 1110 1101-------key = 4DD 1101 1101-------key = 5BD 1011 1101-------key = 67D 0111 1101-------key = 7

Row 0

Row 3

Row 2

Row 1

Page 10: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Keypads - Section 5.2.3

• Example Uses Port B and not Port H

• Uses switch() statements rather than for loops.

Page 11: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Another I/O Example: Contact Closure Counter

Connect 8 Push-Button to the Pins of Port H

Push-Button is normally open => “1” is input

Press Momentary Push-Button => “0” is input

Count Number of Times a Push-Button is Pressed

Count “1” to “0” Transitions on each Pin

Page 12: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

/* port definitions */int main(){int count[8] = 0,0,0,0,0,0,0,0; int sum=0, int i; unsigned char oldsw, newsw, change; /* init port h */ oldsw = PTH; newsw = PTH; while(sum<1000) { while(oldsw == newsw) newsw = PTH; change = (oldsw^newsw) & oldsw; oldsw = newsw; for(i=0, i<8, i++) if(((change>>i)&0x01)!=0) {count[i]++; sum++} } return(0);}

//count only 1->0 transitions

Page 13: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Details

• Old = 0x23 = 0b00100011

• New = 0x32 = 0b00110010

• Old ^ New = 0b00010001

• (Old ^ New) & Old = 0b00000001

• Increment count[0]

Page 14: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Exceptions, Interrupts, and Resets

Page 15: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

“Exception”:

A break in normal program flow, normally initiated by some hardware condition.

Page 16: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Two types of exceptions:

• Resets

• Interrupts

Page 17: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

•Exceptions can be initiated by either Internal or external events

•When exceptions occurs, the microcontroller:

- Uses an address from a Vector Table to get to a Service Routine.

- Saves the processor state on the stack (for Interrupts)

Page 18: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Events that Can Cause a Reset:

– External Reset (via active low reset pin)– Power-on Reset– Computer Operating Properly (COP) Reset– Clock Monitor Reset

• Disabled by Debugger in Lab

Page 19: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Computer Operating Properly(COP Reset)

• Also called Watch Dog Timer

• User-configurable countdown timer

• If timer reaches 0, a system reset is triggered

• User must repeatedly reset using a reset timer sequence

• Disabled in Lab

Page 20: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Clock Monitor Reset

• Clock Monitor Reset when system clock frequency drops below a prescribed value or stops

Page 21: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Two Classes of Interrupts:

– Nonmaskable Interrupts (cannot be disabled by software)

– Maskable Interrupts• Masked – Disabled (By software)• Unmasked – Enabled (By software)

Page 22: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Nonmaskable Interrupts

• X bit – Condition Code Register (CCR)– X=“1” – Disables all Interrupts– X=“0” – Enable all Interrupts

• During Normal System Reset, X=“1”

• After System Initialization, X=“0” by software

• X can not be set to “1” by software

Page 23: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Types of Nonmaskable Interrupts

• Nonmaskable Interrupt Request (/XIRQ)– /XIRQ is External Pin– /XIRQ Active Low Generates Request

• Unimplemented Instruction Trap

• Software Interrupt Instruction (SWI)

Page 24: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Enabling of Maskable Interrupts

• I bit in Condition Code Register (CCR)– I = “0” Enable Maskable Interrupts– I = “1” Disable Maskable Interrupts

• C Program Instructions– ENABLE(); – Sets I=“0”– DISABLE(); – Sets I=“1”

Page 25: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Maskable Interrupt Sources

• IRQ Pin• Real Time Clock Interrupt• Enhanced Capture Timer Channels• Pulse Accumulators• SPI and SCI Serial I/O• A/D System• Port J, H, and P Pins• Network Serial I/O• Other

Page 26: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interrupt Initialization (Fig. 4.21)

Page 27: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interrupt Vector Table

• 2 Bytes Allocated for each Interrupt Source

• Contents of Vector– Starting Address of Interrupt Service Routine

• Vectored System– Each Source has a dedicated Vector

• “Boost” priority of one maskable interrupt to highest priority (HPRIO Register)

Page 28: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
Page 29: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interrupt Handling – Fig. 4.21

Page 30: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interrupt Handling

• Save State of CPU on Stack by Hardware• Disable Masked Interrupt System• Determine Interrupt Source by Hardware • Transfer Control to Interrupt Handling

Software by Hardware• Service Interrupt Request by Interrupt

Service Routine (ISR) – User written• Restore State of CPU from Stack using

RTI instruction

Page 31: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Why Interrupts?

• Most operating systems support multiprocessing – Several application running at same time.

• Desirable to eliminate “busy waits” in all software since busy waits can monopolize CPU resources.

Page 32: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Recall: Parallel Printer Interface(Not using interrupts)

Printer

/Strobe

Busy

Data[7:0]

Busy

WriteData

Strobe

Yes

No

Page 33: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Print a Character on Printer

void printc(unsigned char c){ /* Wait while Printer is Busy */ while((PTJ&0x02)!=0); // BUSY WAIT

/* Printer not Busy, Output Data */ PTH = c;

/* Generate Strobe * PTJ = PTJ & 0xFE; // Bit 0 Low PTJ = PTJ | 0x01; // Bit 0 High}

Page 34: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Busy

Data

/Strobe

ACK

Printer Waveforms

PC Parallel Printer Port Definitionwww.fapo.com/ieee1284.htm

Page 35: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Parallel Printer Interface

Printer

/Strobe

Busy

Data[7:0]

ACK

Busy

WriteData

Strobe

Yes

No

Page 36: UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)

Interfacing a Parallel Printer Using Interrupts

• Present data to printer

• Send Strobe signal to printer

• When printer is no long busy, it sends out an ACK pulse to request an interrupt

• (The interrupt indicates the printer is ready to receive a new character to print.)