18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7<br />

Listing 7-1. Displays a message when external interrupt 1 is detected.<br />

10 ONEX1 100<br />

20 DO<br />

30 WHILE 1=1<br />

40 END<br />

100 PRINT “Interrupt detected”<br />

110 RETI<br />

For a pushbutton-triggered interrupt, connect a switch like Figure 7-1’s to the 8052-BASIC’s<br />

INT1 input at pin 13. Pin 13 will then be normally high. When you press and release the<br />

switch, pin 13 will briefly go low.<br />

Listing 7-1 is a simple program that waits for interrupts and jumps to an interrupt-handling<br />

routine when it detects one, signified by pin 13 going low. Line 10 enables external interrupt<br />

1 and specifies line 100 as the location to branch to when the 8052-BASIC detects an<br />

interrupt request at pin 13. Lines 20-30 are an endless loop that waits for an interrupt. Lines<br />

100 and 110 are the interrupt-service routine. In this example the routine doesn’t do very<br />

much; it just displays an on-screen message that the interrupt was detected, then returns to<br />

the main program loop.<br />

Edge-detecting Interrupts<br />

BASIC-52’s TCON operator allows you to write to the 8052’s special-function register<br />

TCON, which enables you to set up interrupt 1 as edge-detecting or level-detecting. <strong>The</strong><br />

default after bootup or reset is edge-detecting, where interrupts are triggered by a falling<br />

edge at pin 13. If you want a rising edge to trigger an interrupt, you’ll have to add an inverter<br />

at pin 13.<br />

Edge-triggering is handy for detecting switch presses, because the interrupt routine executes<br />

only once, when the switch is first pressed, no matter how long you hold down the switch.<br />

Switch debouncing. Even with edge-triggered interrupts, however, switch bounce can cause<br />

multiple interrupts to occur with a single switch press. Switch bounce occurs because manual<br />

presses of mechanical switches tend to be sloppy. When you press a switch, the contacts<br />

normally bounce open and closed several times before they close positively, and bounce<br />

again as you lift your finger and the contacts open.<br />

<strong>The</strong> computer has to be able to tell the difference between a bounce and a genuine switch<br />

press. Otherwise, each time you press a switch, and again when you release it, the computer<br />

will detect several rapid switch presses. One way to handle switch bounce is to ignore<br />

keypresses that are less than a certain length, usually around 10-20 milliseconds, with the<br />

112 <strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!