Exercise - Using Interrupts

From Flowcode Help
Revision as of 19:48, 12 August 2013 by JohnVerrill (talk | contribs)
Jump to navigationJump to search

<sidebar>Sidebar: What Is an Interrupt?</sidebar> Interrupts are a way of grabbing the microcontroller's attention immediately.

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.

They 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.


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

Polling vs interrupt

The microcontroller 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 - allow the device to interrupt the task being carried out by the controller.

The exercise aims to show the difference between polling and using interrupts.

The system

This exercise sets up a system with LEDs controlled by two switches:

  • Switch 1 is polled by the program, at regular intervals.
  • Switch 2 initiates an interrupt.


The polled switch, switch 1, is checked at regular (and predictable) intervals - every six seconds in this example. Switch 2 is connected so that as soon as it operates, the processor stops what it is doing and jumps to the Interrupt Service Routine (ISR). In the process, the return address is stored, so that at the end of the ISR, the processor can return to where it left the main program. In Flowcode, the ISR takes the form of a macro, configured like any other.


The flowchart

The flowchart sequence:

  • 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.
  • Whenever switch 2 is pressed, immediately make both LEDs flash quickly ten times and then go back to the main program.

The main program

  • 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.
  • Drag and drop a Loop icon between the BEGIN and END icons.

Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch):

This icon is going to turn the red LED on.
  • Drag and drop a Delay icon after the 'Output' icon.
This will keep the LED on for three seconds.
This icon is going to turn the LED off.
This will keep the LED off for three seconds.

The program then loops back and checks the state of the switch again.

Next, add icons to control what happens when switch is pressed, and so the program follows the 'Yes' branch:

  • Drag and drop a Delay icon after the 'Output' icon.

The program then loops back, as before, and checks the state of the switch again.


Add switch 1 and the LEDs

  • Locate the 'Inputs' toolbox, and click on the down arrow next to the switch labelled 'Toggle Metal PCB'.
Click on the 'Add to system panel' option.
Configure its properties so that it is connected to Port B, bit 1.
  • Locate the 'Outputs' toolbox, and click on the down arrow next to the 'LED array' label.
Click on the 'Add to system panel' option.
Do this twice to add two LEDs to the system panel.
  • Click on one, and configure its properties so that its colour is red, and it is connected to Port A, bit 0.
This will light when Port A is sent value '1'.
  • Click on the other, and configure its properties so that its colour is yellow, and it is connected to Port A, bit 1.
This will light when Port A is sent value '3'.


Set up the lamp=

Next we concentrate on the 'Yes' branch of the 'Decision' icon.

Set up the switch

This icon will be used to make the program perform what is known as a conditional branch.


When programmed, the current icons will be able to continually check if the switch is being pressed or not.
In the next step we will be adding icons which will allow us to control what happens if the switch is pressed.
Once the switch is pressed it will perform an the action or process and continue to check if it is being pressed again.
If the switch is not pressed it will follow the 'No' branch and keep looping to continue to check if the switch has been pressed.


Your flowchart should now be set up to resemble the image to the right.

You should now save the flowchart as "Lamp1.fcf", and close Flowcode.