Difference between revisions of "Exercise - Using Interrupts"

From Flowcode Help
Jump to navigationJump to search
Line 1: Line 1:
 
<sidebar>Sidebar: What Is an Interrupt?</sidebar>
 
<sidebar>Sidebar: What Is an Interrupt?</sidebar>
 +
 +
 +
Interrupt 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.)
 
This exercise shows how to use an interrupt to sense when a switch is closed, (an external interrupt.)
Line 7: Line 13:
 
==Polling vs interrupt==
 
==Polling vs interrupt==
  
Interrupt are a way of grabbing the microcontroller's attention immediately.
+
The controller 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 - 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 .......
 
The first exercise aims to show the difference between polling and using interrupts. The second section looks .......
Line 30: Line 32:
 
:: 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.
  
 
==New flowchart==
 
==New flowchart==
Line 37: Line 39:
 
* 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==
+
==Set up the main program==
 
* Drag and drop a [[Loop Icon Properties|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 Properties|Input icon]] from the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
 
* Inside the loop, drag and drop an [[Input Icon Properties|Input icon]] from the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
 +
* Drag and drop a [[Decision Icon Properties|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 Properties|Output icon]] in the 'No' branch.
 +
: This icon is going to turn the LED on.
  
==Set up the switch==
 
* Drag and drop a [[Decision Icon Properties|Decision icon]] after the 'Input' icon.
 
  
[[File:Exercise_Creating_a_Flowchart_Icons_Layout.png|right]]
+
* Drag and drop a [[Delay Icon Properties|Delay icon]] after the 'Output' icon.
 +
: This will keep the LED on for three seconds.
 +
* Follow it with another [[Output Icon Properties|Output icon]].
 +
: This icon is going to turn the LED off.
 +
* Add a second [[Delay Icon Properties|Delay icon]] next.
 +
: This will keep the LED off for three seconds.
  
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch:
+
The program then loops back and checks the state of the switch again.
  
=Set up the lamp==
+
Next, add icons to control what happens when switch is pressed, and so the program follows the 'Yes' branch:
: Next we concentrate on the 'Yes' branch of the 'Decision' icon.
 
 
* Drag and drop an [[Output Icon Properties|Output icon]] in the 'Yes' branch.
 
* 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.
 
  
 +
* Drag and drop a [[Delay Icon Properties|Delay icon]] after the 'Output' icon.
 +
: This will keep the LED on for three seconds.
 +
* Follow it with another [[Output Icon Properties|Output icon]].
 +
: This icon is going to turn the LED off.
 +
* Add a second [[Delay Icon Properties|Delay icon]] next.
 +
: This will keep the LED off for three seconds.
  
: Next we want the lamp to stay on for ten seconds and then turn off.
+
The program then loops back and checks the state of the switch again.
* 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.
 
  
  
Line 64: Line 75:
 
: To do this we will need another 'Output' icon.
 
: To do this we will need another 'Output' icon.
 
:* Drag and drop a second 'Output' icon after the 'Delay' icon.
 
:* Drag and drop a second 'Output' icon after the 'Delay' icon.
 +
==Set up the switch==
 +
 +
 +
[[File:Exercise_Creating_a_Flowchart_Icons_Layout.png|right]]
 +
 +
 +
:* 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.
 +
 +
 +
  
  
 
Your flowchart should now be set up to resemble the image to the right.  
 
Your flowchart should now be set up to resemble the image to the right.  
 +
 +
=Set up the lamp==
 +
: Next we concentrate on the 'Yes' branch of the 'Decision' icon.
 +
 +
==Set up the switch==
 +
* Drag and drop a [[Decision Icon Properties|Decision icon]] after the 'Input' icon.
 +
: This icon will be used to make the program perform what is known as a conditional branch.
 +
 +
[[File:Exercise_Creating_a_Flowchart_Icons_Layout.png|right]]
 +
 +
: 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.
 +
 +
 +
 +
  
  
 
You should now [[Saving a Flowchart|save the flowchart]] as "Lamp1.fcf", and [[Closing Flowcode|close Flowcode]].
 
You should now [[Saving a Flowchart|save the flowchart]] as "Lamp1.fcf", and [[Closing Flowcode|close Flowcode]].

Revision as of 13:36, 12 August 2013

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


Interrupt 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 controller 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.

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.
Whenever switch 2 is pressed, immediately 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.

Set up the main program

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


Finally we need to turn the lamp off, after the ten second delay.
To do this we will need another 'Output' icon.
  • Drag and drop a second 'Output' icon after the 'Delay' icon.

Set up the switch

Exercise Creating a Flowchart Icons Layout.png


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



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

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.
Exercise Creating a Flowchart Icons Layout.png
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.




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