ooPIC Logo

ooPIC Programmer's Guide

 Chapter 9 - Event Driven Programming

Back to top of page What is an Event Driven Program
 An event is defined as any change in state that is recognized by an Object. An Event Driven Program is a program where any change in state can cause a subprocedure to be executed even when the program flow was not expecting to do so. Therefore, the order in which your code executes depends on which events occur.

In Event Driven Programs, the ooPIC's oEvent Object is used to execute code in response to an event.

NOTE:
In ooPIC II and later microcontrollers you can also use a prioritized event oEventX if you want to allow a priority event structure.

NOTE AGAIN:
An oEvent can interrupt a currently running oEvent subprogram, when the new event has completed the interrupted event will resume operation at the point where it left off before it was interrupted. Keep this in mind when programming events, you cannot cancel an event once it has been started. This is true for oEventX prioritized events, a higher priority event can interrupt a lower priority event. Without care, this characteristic of the ooPIC event structure can cause priority inversions, which means that a lower priority event (the interrupted one) can override changes caused by a higher priority event (the interrupting event) depending upon where in the code the interrupt occurred.

Back to top of page Steps to Creating Your First Event Driven Program
 

There are 4 steps involved in creating an application for the ooPIC

  1. Create an ooPIC application
  2. Identify functions applicable to events
  3. Add the Event Object to the application
  4. Create the Event Object's Subprocedure.
Back to top of page Creating an ooPIC Application
 In Chapter 7, an ooPIC application was created that blinks an LED by using an oWire Object to assign the ooPIC.Hz1 property to the State property of an oDio1 Object.

In this chapter, instructions are given on how to modify the application in chapter 7 to be Event Driven.

Back to top of page Identify Functions Applicable to Events
 In the ooPIC application in Chapter 7, a Virtual Circuit continuously assigns the ooPIC.Hz1 property to an oDio1 Object's State property. The ooPIC.Hz1 property changes state from 0 to 1 and then back to 0 once every second which results in the LED blinking once every second.

The change in state of the ooPIC.Hz1 property can be recognized as an event by the program. Modifying the program to be Event Driven will allow the application's program to call a subprocedure which in turn can do more complex processing.

Back to top of page Add the Event Object to the Application
 Adding the Event Driven routines to the ooPIC application is done programmatically with lines of code.

Because this application is a modification of the "First Virtual Circuit" application, found in Chapter 7, the final code listing for the ooPIC application in Chapter 7, as shown below, will need to be modified.

Dim WIRE As New oWire
Dim LED As New oDIO1

Sub Main()
  LED.IOLine = 7
  LED.Direction = cvOutput
  WIRE.Input.Link(ooPIC.Hz1)
  WIRE.Output.Link(LED.State)
  WIRE.Operate = cvTrue
End Sub

As previously detailed, this application will use an oEvent Object to provide more complex processing. Starting at the first line of code, insert a blank line and type the following statement:

Dim BLINK As New oEvent

This instructs the ooPIC to create an instance of an oEvent Object with the name "BLINK". The function of an oEvent Object is to call a subprocedure when its Operate property is set to 1.

Next, the Virtual Circuit needs to be changed. In the Chapter 7 application, the "WIRE" Object's output was linked to the oDio1 Object's State property. This needs to be changed so that it is linked to the oEvent Object's Operate property.

Locate the line that reads "WIRE.Output.Link(LED.Value)", and change it to read as show:

WIRE.Output.Link(BLINK.Operate)

This instructs the "WIRE" Object to link its Output property to the Operate property of the "BLINK" Object.

After the program executes this line of code, the Virtual Circuit begins to operate in the following manner; and will continue to operate until WIRE.Operate is set back to a value of 0;

  1. The value of the ooPIC.Hz1 property will be loaded by the oWire Object.
  2. The oWire will then use that value in a logical OR operation with its other inputs. In this application, only one input was used, so the result of the logical OR function will always equal the input.
  3. The BLINK.Operate property is set to the result of the logical OR function.
  4. Each time the BLINK.Operate property changes from 0 to 1, the "BLINK" Object's Subprocedure is executed.

The following code shows the complete code listing written as instructed in the previous paragraphs.  Note that this will not comiler yet as it still needs the Event routine.

Dim BLINK As New oEvent
Dim WIRE As New oWire
Dim LED As New oDIO1

Sub Main()
  LED.IOLine = 31
  LED.Direction = cvOutput
  WIRE.Input.Link(ooPIC.Hz1)
  WIRE.Output.Link(BLINK.Operate)
  WIRE.Operate = cvTrue
End Sub
Back to top of page Create the Event Object's Subprocedure
 Each oEvent Object has an associated subprocedure that is executed when the oEvent Object's Operate property is set to 1. The name of this routine is the name of the oEvent Object followed by "_CODE".

At the bottom of the program add the following lines of code:

Sub BLINK_Code()
  LED.State = 1
  LED.State = 0
  LED.State = 1
  LED.State = 0
  LED.State = 1
  LED.State = 0
End Sub

The following code shows the complete code listing written as instructed in the previous paragraphs.

Dim BLINK As New oEvent
Dim WIRE As New oWire
Dim LED As New oDIO1

Sub Main()
  LED.IOLine = 31
  LED.Direction = cvOutput
  WIRE.Input.Link(ooPIC.Hz1)
  WIRE.Output.Link(BLINK.Operate)
  WIRE.Operate = cvTrue
End Sub

Sub BLINK_Code()
  LED.State = 1
  LED.State = 0
  LED.State = 1
  LED.State = 0
  LED.State = 1
  LED.State = 0
End Sub
Back to top of page Downloading and Running the Event Driven Application
 Downloading and running the Event Driven Application is no different than download and running any other ooPIC application.

Pressing the F5 key will compile the program and then download the "oex" file to the ooPIC. If any errors are encountered, they will be reported and the compilation will stop. If no errors were encountered, an "oex" (ooPIC Executable) file is generated. If you have not saved the program or you have changed it since the last time you have saved it, the compiler will ask you to save it. If this is the first time the program is going to be saved, the compiler will ask you for a program name. In this case, let's call the program "FirstEV". Type "FirstEV" in the "File name" area of the "Save As" dialog box and hit Enter.

In order for the executable to be downloaded to the ooPIC, the programming cable must be connected to the PC and properly configured. It also needs to be connected to the ooPIC's programming connector and an adequate power supply must be attached to the ooPIC. See the "Programming Cable" section for more information.


ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.