Difference between revisions of "Exercise - Using Interrupts"

From Flowcode Help
Jump to navigationJump to search
Line 21: Line 21:
  
 
This exercise sets up a system with two switches.<br />
 
This exercise sets up a system with two switches.<br />
One is polled by the program, at regular intervals.<br />
+
Switch 1 is polled by the program, at regular intervals.<br />
The other initiates an interrupt.
+
Switch 2 initiates an interrupt.
  
  
 
The flowchart sequence will be:
 
The flowchart sequence will be:
::::*
+
: Check if switch 1 is pressed.
::::*
+
:: If it isn't, make the red LED flash slowly.
 +
:: If it is, make the yellow LED flash slowly.
 +
:: Go back to the beginning and repeat the process.
 +
:: When switch 2 is pressed, make both LEDs flash quickly ten times and then go back to the main program.
  
 
==New flowchart==
 
==New flowchart==

Revision as of 11:18, 12 August 2013

<sidebar>Sidebar: What Is an Interrupt?</sidebar>

This exercise shows how to use an interrupt to sense when a switch is closed, (an external interrupt.)

Polling vs interrupt

Interrupt are a way of grabbing the microcontroller's attention immediately. It is often connected to a large number of peripheral input devices - switches, sensors, memory, timers etc. There are two broad ways in which one of these devices can be serviced by the microcontroller:

  • polling - each device is 'asked' in turn if it has data to transfer to the controller;
  • interrupts - to allow the device to interrupt the task being carried out by the controller.

They are very widely used, both in microcontrollers and microprocessors. For example, the keyboard and mouse in your computer probably use interrupts to talk to the CPU.

Interrupts can also be used to save energy. In many battery-powered applications, the microcontroller is 'put to sleep' when inactive, and so requires little energy. An interrupt is used to 'awaken' the controller, and bring it back into operation, when needed.

The first exercise aims to show the difference between polling and using interrupts. The second section looks .......

Exercise 1

This exercise sets up a system with two switches.
Switch 1 is polled by the program, at regular intervals.
Switch 2 initiates an interrupt.


The flowchart sequence will be:

Check if switch 1 is pressed.
If it isn't, make the red LED flash slowly.
If it is, make the yellow LED flash slowly.
Go back to the beginning and repeat the process.
When switch 2 is pressed, make both LEDs flash quickly ten times and then go back to the main program.

New flowchart

  • Make sure that the System Panel is visible. If necessary, click on View and then select 'System Panel' a check-box will appear next to the option when enabled.