Difference between revisions of "Exercise - Using Interrupts"

From Flowcode Help
Jump to navigationJump to search
 
(36 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<sidebar>Sidebar: What Is an Interrupt?</sidebar>
+
<sidebar>Sidebar: Flowcode Exercises:Ex1</sidebar>
 +
Interrupts are a way of grabbing the microcontroller's attention immediately.
  
This exercise shows how to use an interrupt to sense when a switch is closed, (an external interrupt.)
+
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).
  
 
__TOC__
 
__TOC__
 +
  
 
==Polling vs interrupt==
 
==Polling vs interrupt==
  
Interrupt are a way of grabbing the microcontroller's attention immediately.
+
The microcontroller is often connected to a large number of peripheral input devices - switches, sensors, memory, timers etc.  
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:
+
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;
 
* 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.
+
* interrupts - 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.
+
The exercise aims to show the difference between polling and using interrupts.  
 +
 
 +
 
 +
==The system==
  
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.
+
This exercise sets up a system with LEDs controlled by two switches:
 +
::* Switch 1 is polled by the program, at regular intervals.<br />
 +
::* Switch 2 initiates an interrupt.
  
The first exercise aims to show the difference between polling and using interrupts. The second section looks .......
 
  
==Exercise 1==
+
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.
  
This exercise sets up a system with two switches.<br />
 
Switch 1 is polled by the program, at regular intervals.<br />
 
Switch 2 initiates an interrupt.
 
  
 +
==The flowchart==
  
The flowchart sequence will be:
+
The flowchart sequence:
: Check if switch 1 is pressed.
+
:* Check if switch 1 is pressed.
 
:: If it isn't, make the red LED flash slowly.
 
:: If it isn't, make the red LED flash slowly.
 
:: If it is, make the yellow LED flash slowly.
 
:: If it is, make the yellow LED flash slowly.
:: Go back to the beginning and repeat the process.
+
:* 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.
+
:* Whenever switch 2 is pressed, immediately make both LEDs flash quickly ten times and then go back to the main program.
 +
 
 +
 
 +
==The main program==
  
==New flowchart==
 
 
* [[Creating Flowcharts|Start a new Flowcode flowchart]], using the default microcontroller.
 
* [[Creating Flowcharts|Start a new Flowcode flowchart]], using the default microcontroller.
  
 
* 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.
 
* 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.
  
==Set up the input==
+
* Drag and drop a 'Loop' icon between the BEGIN and END icons.
* Drag and drop a [[Loop Icon Properties|Loop icon]] between the BEGIN and END icons.
+
 
 +
* Inside the loop, drag and drop an 'Input' icon from the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
 +
 
 +
* Drag and drop a 'Decision' icon after the 'Input' icon.
 +
 
 +
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch):
 +
 
 +
* Drag and drop an 'Output' icon in 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.
 +
 
 +
* Follow it with another 'Output' icon.
 +
: This icon is going to turn the LED off.
 +
 
 +
* Add a second 'Delay' icon next.
 +
: 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 an 'Output' icon in the 'Yes' branch.
 +
 
 +
* Drag and drop a 'Delay' icon after the 'Output' icon.
 +
 
 +
* Follow it with another 'Output' icon.
 +
 
 +
* Add a second 'Delay' icon next.
 +
 
 +
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'. [[File:Component Icon c6c91999 1fe0 414e b6aa 0a7ddc9a6417.png]]
 +
: 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. [[File:Component Icon c8da67a7 fc7b 48c0 8c3f 3264c74f4024.png]]
 +
: Click on the 'Add to system panel' option.
 +
* Click on one, and configure its properties as follows
 +
:: set the 'Count' property to "2", to give two LEDs in the array;
 +
:: set the colour of LED0 to red, and that of LED1 to yellow.
 +
:: LED0 will light when Port A is sent value '1', and LED1 will light when Port A is sent value '3';
 +
:: check that the 'Connections' property shows that it is connected to Port A.
 +
 
 +
Double click on each icon in the flowchart and configure it as shown in the following images:
 +
 
 +
<gallery caption="Configuring the icons" perrow="6">
 +
File:Exercise_UsingInt_redLEDon.jpg|Turn the red LED on
 +
File:Exercise_UsingInt_3sdelay.jpg|Three seconds delay
 +
File:Exercise_UsingInt_LEDoff.jpg|Turn the LEDs off
 +
File:Exercise_UsingInt_yellLEDon.jpg|Turn on the yellow LED
 +
File:Exercise_UsingInt_sw1_prop.jpg|Configure switch 1
 +
File:Exercise_UsingInt_decision.jpg|Configure the decision icon
 +
</gallery>
 +
 
 +
To see a larger version, double click on the image.
 +
 
 +
Simulate the flowchart to make sure that the LEDs behave as expected.
 +
 
 +
 
 +
==Add switch 2==
 +
[[File:Exercise_UsingInt_int_prop.jpg|200px|right]]
 +
 
 +
* Add a second 'Toggle Metal PCB' switch to the system panel.
 +
: Configure its properties so that it is connected to Port B, bit 0 - the hardware interrupt bit.
 +
* Drag and drop an interrupt icon [[File:Btn_Interrupt.gif|border]] between 'BEGIN'and the upper loop icon, (although it will work anywhere in the flowchart.)
 +
: Double click on the icon, and configure it as shown opposite.
 +
* The next task is to create the ISR. Click on the 'Create New Macro...' button.
 +
[[File:Exercise UsingInt Int flashfastmacro.jpg|200px|right]]
 +
 
 +
* The 'Create a New Macro' dialogue box opens.
 +
: Give the macro the name "flash_fast".
 +
: Click on OK.
  
* Inside the loop, drag and drop an [[Input Icon Properties|Input icon]] from the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
+
* In the 'Properties:Interrupt' dialogue box, click on the 'OK & Edit Macro' button.
 +
: Create the flowchart shown opposite for the macro.
 +
: Sending value '3' (=00011 in binary,) to Port A switches on both LEDs.
  
==Set up the switch==
+
==The main flowchart==
* Drag and drop a [[Decision Icon Properties|Decision icon]] after the 'Input' icon.
 
  
[[File:Exercise_Creating_a_Flowchart_Icons_Layout.png|right]]
+
Your flowchart should now be set up to resemble the image below:  
  
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch:
 
  
=Set up the lamp==
+
[[File:Exercise_UsingInt_main.jpg|350px|center]]
: Next we concentrate on the 'Yes' branch of the 'Decision' icon.
 
* Drag and drop an [[Output Icon Properties|Output icon]] in the 'Yes' branch.
 
: This icon is going to function as a trigger to turn the lamp on.
 
  
  
: Next we want the lamp to stay on for ten seconds and then turn off.
 
* Drag and drop a [[Delay Icon Properties|Delay icon]] after the 'Output' icon.
 
:* This is the icon which will allow us to delay the circuit for a specified period of time.
 
:* The delay will last for 10 seconds, in this time the circuit will not be able to carry out any other function until the delay is complete.
 
  
 +
==Test the system==
  
: Finally we need to turn the lamp off, after the ten second delay.
+
Earlier, you tested the effect of switch 1 on the LEDs. Now you can test the effect of switch 2 as well.
: To do this we will need another 'Output' icon.
+
* Run the simulation, and confirm the action of switch 1 on the LEDs.
:* Drag and drop a second 'Output' icon after the 'Delay' icon.
+
* While Flowcode is simulating a three second delay, toggle switch 2 on and off.
 +
: The  three second simulationshould stop, mid-delay, and both LEDs should flash quickly, ten times, as specified in the ISR.
 +
: After this, the microcontroller resumes the main program from where it left off, the three second delay.
 +
* Repeat the tests until you are happy with the way the system operates.
  
  
Your flowchart should now be set up to resemble the image to the right.  
+
==Download the exercise==
 +
You can download the file created by this exercise and open it in Flowcode to identify errors in your program/file or you could also download the file to skip to the next exercise.
  
 +
*To download the file, click on the link below and then either:
 +
:*Click on the file name.
 +
:*Right click the file name and select 'Save link as...' or 'Save target as...' (depending on your browser).
  
You should now [[Saving a Flowchart|save the flowchart]] as "Lamp1.fcf", and [[Closing Flowcode|close Flowcode]].
+
:::{{Fcfile|Exercise - Using Interrupts.fcfx|Exercise - Using Interrupts}}

Latest revision as of 14:32, 13 March 2014

<sidebar>Sidebar: Flowcode Exercises:Ex1</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.
  • Inside the loop, drag and drop an 'Input' icon from the Icons toolbar.
  • Drag and drop a 'Decision' icon after the 'Input' icon.

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

  • Drag and drop an 'Output' icon in 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.
  • Follow it with another 'Output' icon.
This icon is going to turn the LED off.
  • Add a second 'Delay' icon next.
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 an 'Output' icon in the 'Yes' branch.
  • Drag and drop a 'Delay' icon after the 'Output' icon.
  • Follow it with another 'Output' icon.
  • Add a second 'Delay' icon next.

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'. Component Icon c6c91999 1fe0 414e b6aa 0a7ddc9a6417.png
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. Component Icon c8da67a7 fc7b 48c0 8c3f 3264c74f4024.png
Click on the 'Add to system panel' option.
  • Click on one, and configure its properties as follows
set the 'Count' property to "2", to give two LEDs in the array;
set the colour of LED0 to red, and that of LED1 to yellow.
LED0 will light when Port A is sent value '1', and LED1 will light when Port A is sent value '3';
check that the 'Connections' property shows that it is connected to Port A.

Double click on each icon in the flowchart and configure it as shown in the following images:

To see a larger version, double click on the image.

Simulate the flowchart to make sure that the LEDs behave as expected.


Add switch 2

Exercise UsingInt int prop.jpg
  • Add a second 'Toggle Metal PCB' switch to the system panel.
Configure its properties so that it is connected to Port B, bit 0 - the hardware interrupt bit.
  • Drag and drop an interrupt icon Btn Interrupt.gif between 'BEGIN'and the upper loop icon, (although it will work anywhere in the flowchart.)
Double click on the icon, and configure it as shown opposite.
  • The next task is to create the ISR. Click on the 'Create New Macro...' button.
Exercise UsingInt Int flashfastmacro.jpg
  • The 'Create a New Macro' dialogue box opens.
Give the macro the name "flash_fast".
Click on OK.
  • In the 'Properties:Interrupt' dialogue box, click on the 'OK & Edit Macro' button.
Create the flowchart shown opposite for the macro.
Sending value '3' (=00011 in binary,) to Port A switches on both LEDs.

The main flowchart

Your flowchart should now be set up to resemble the image below:


Exercise UsingInt main.jpg


Test the system

Earlier, you tested the effect of switch 1 on the LEDs. Now you can test the effect of switch 2 as well.

  • Run the simulation, and confirm the action of switch 1 on the LEDs.
  • While Flowcode is simulating a three second delay, toggle switch 2 on and off.
The three second simulationshould stop, mid-delay, and both LEDs should flash quickly, ten times, as specified in the ISR.
After this, the microcontroller resumes the main program from where it left off, the three second delay.
  • Repeat the tests until you are happy with the way the system operates.


Download the exercise

You can download the file created by this exercise and open it in Flowcode to identify errors in your program/file or you could also download the file to skip to the next exercise.

  • To download the file, click on the link below and then either:
  • Click on the file name.
  • Right click the file name and select 'Save link as...' or 'Save target as...' (depending on your browser).
FC6 Icon.png

Exercise - Using Interrupts