 |
ooPIC Programmer's Guide
Chapter 2 - Your First ooPIC Application
|
|
|
|
What is an ooPIC Application
|
| | An ooPIC application is a computer program written for the purpose of controlling electronic equipment connected to an ooPIC microcontroller. The program itself is written and stored on a P.C. style computer using the ooPIC Software. Once written, it is "compiled" and downloaded to an ooPIC microcontroller. Once downloaded, the ooPIC runs the program and the P.C. style computer is no longer needed for it's continued operation. In this chapter, instructions are given to create a very small program that can be easily implemented. The intent is to familiarize the new users with the procedures involved in the ooPIC development environment. |
Steps to Creating Your First ooPIC Application
|
| | There are 6 steps involved in creating an application for the ooPIC
- Start the ooPIC Software.
- Design the program's concept.
- Design a hardware interface.
- Write code.
- Connect the hardware.
- Download and run the application.
|
Starting the ooPIC Software Development Kit
|
| | Clicking the "ooPIC Compiler" selection on the Windows Start Menu will start the ooPIC's Software. When the ooPIC's Software opens, it will be in full screen mode with a large program editor area for you to type in your program. A menu provides a set of operations that you will need while writing your application including a file save and load selection for you to save the applications that you have written. |
First ooPIC Application Concept
|
| | The concept for our first application is a simple one; We are going to make an LED Blink. The materials required are as follows:
For the ooPIC-R, ooPIC-C and ooPIC-E Boards (These board have an LED) | For the ooPIC-S Board (You will need to add an LED) |
- 1 ooPIC
- 1 9-volt battery
- 1 9-volt battery connector
- 1 ooPIC programming cable
|
- 1 ooPIC
- 1 9-volt battery
- 1 9-volt battery connector
- 1 ooPIC programming cable
- 1 red LED
- 1 220 ohm resistor
- 2 test jumpers.
|
If you have an ooPIC-S Board, then you will need to add the LED. Instructions are given later on in this chapter on how to do that. If you have an ooPIC-R, C or E style boards, then then an LED is already connected and ready. |
Creating the Hardware Interface.
|
| | In an ooPIC application, a hardware interface is a set of Objects that control the physical hardware that is connected to the ooPIC. In our program concept, we decided that we were going to make a LED blink. By looking through the Object List, we found an object that will do what we need it to do and as a bonus, it can even control the brightness of the LED. We will use the oLED Object for our hardware interface because it knows how to "interface" with the electronics of the LED. This will allow us to write a program that controls the oLED Object without worrying about HOW the oLED Object is controlling the electronics.
| | Object | Description | A1 | A2 | B1 | B2 | C1 |
 | oLED | Controls an LED light with brightness control. | x | x | x |
|
Before starting, Notice that the oLED Object requires firmware version B1 or greater. Be sure that you are using an ooPIC with at least this version number and then begin. The first thing the program will need to do is to declare that it needs to use the oLED Object. Since the ooPIC can understand a number of different languages, the statements are given here for the BASIC, C and Java Languages. Starting at the first line of code, pick a language and type in the following statement: (Note that is does not matter which language you choose, the compiler will recognize which language you are using and compiler accordingly.)
| Visual Basic syntax | C & Java Syntax |
Dim LED As New oLED | oLED led = new oLED; |
| BASIC syntax | |
LED As oLED | |
(After typing this line, you can push the F6 key to see an icon for the oLED Object appear in the Object browser.) Now that "LED" exists, it needs to be told which wires to use to control the physical LED connect to the ooPIC. On the next line, add one of the following statements: (here you will need to pick which ooPIC you have as well as which language you want)
For the ooPIC-S and ooPIC-E Use I/O Line 8 | For the ooPIC-R and ooPIC-C Use I/O Line 5 |
| Visual Basic syntax | C & Java Syntax |
LED.IOLine = 8 | LED.IOLine = 8; |
| BASIC | |
LED.IOLine = 8 | |
|
| Visual Basic syntax | C & Java Syntax |
LED.IOLine = 5 | LED.IOLine = 5; |
| BASIC | |
LED.IOLine = 5 | |
|
|
Writing the Application
|
| | With the hardware interface in place, the next step is to write the part of the program that controls it. Making the LED Blink is a simple matter of telling the LED to turn on and off while waiting a little bit between the two. At the bottom of the program, add the following statements:
| Visual Basic & BASIC syntax | C & Java Syntax |
Do
LED.TurnOn
Delay = 500
LED.TurnOff
Delay = 500
Loop | Do{
LED.TurnOn;
Delay = 500;
LED.TurnOff;
Delay = 500;
} |
The first statement sets up a loop.
The second statement tells the LED Object to turn on.
The third statement tells the program to wait 1/2 second before going on.
The fourth statement tells the LED Object to turn off.
The fifth statement tells the program to wait 1/2 second before going on.
the sixth statement tells the program to go back to the first statement.
Did you notice again that there is a period between the object's name "LED" and the words "TurnOn" and "TurnOff" but there are no spaces. Be sure to type this in just like it is shown. Take a look at the first and the sixth lines. They are two parts of a single command called a Do/Loop structure. By using the Do/Loop structure around the other statement, the other statements are repeated indefinitely. The result of this is that the LED will turn off and on once every second. The following code shows the complete code listing written as instructed:
For the ooPIC-S and ooPIC-E Use I/O Line 8 | For the ooPIC-R and ooPIC-C Use I/O Line 5 |
| Bisual Basic syntax | C and Java Syntax |
DIM LED As New oLED
LED.IOLine = 8
Do
LED.TurnOn
Delay = 500
LED.TurnOff
Delay = 500
Loop | oLED led = new oLED;
LED.IOLine = 8;
Do{
LED.TurnOn;
Delay = 500;
LED.TurnOff;
Delay = 500;
} |
| BASIC syntax | |
LED As oLED
LED.IOLine = 8
Do
LED.TurnOn
Delay = 500
LED.TurnOff
Delay = 500
Loop | |
|
| Visual Basic syntax | C and Java Syntax |
Dim LED As New oLED
LED.IOLine = 5
Do
LED.TurnOn
Delay = 500
LED.TurnOff
Delay = 500
Loop | oLED led = new oLED;
LED.IOLine = 5;
Do{
LED.TurnOn;
Delay = 500;
LED.TurnOff;
Delay = 500;
} |
| BASIC syntax | |
LED As oLED
LED.IOLine = 5
Do
LED.TurnOn
Delay = 500
LED.TurnOff
Delay = 500
Loop | |
|
As you can see, there isn't much difference between the different languages. |
Connecting the Hardware
|
| | Now that the program has been written, the hardware needs to be connected. If you have an ooPIC-R, C or E Board, the LED is ready to go, so you can skip this section and go to the next step.
However, If you have an ooPIC-S Board that looks like this - you will need to add your own LED. |  |
For the application to work, the LED needs to be connected at two places.
The negative side of the battery. This connection is called Ground.
- A point that will provide the LED with power. For this project, this connection will use I/O line 8.
Both I/O Line 8 and Ground can be found on the ooPIC-S's 40-Pin I/O connector. Caution: When any one of the I/O lines on the ooPIC is on, the output voltage of it is 5 Volts. This is too much power to properly drive an LED without damaging it. For this reason, a 220 Ohm resistor will be used to limit the amount of power going to the LED. Connect the LED together with the resistor to I/O Line 8 and Ground:
- Twist together either prong of resistor and negative (short) prong of LED
- Attach positive (long) prong of LED to I/O line 8 (pin 20) using test jumpers
- Attach unoccupied prong of resistor to Ground (pin 24) using test jumpers
NOTE: Make sure that you pay special attention to the following items:
- The LED is polarized. That is to say, it will not work if it is connected backwards. Make sure that you have attached the LED with the positive side (long prong) connecting to I/O line 8 and the negative side (short prong) connecting to the resistor.
- The I/O line number is not the same as the Pin number. Make sure you have connected the hardware to the correct pins. Looking at the ooPIC in such a way that the 40-Pin connector on the top, I/O line 8 is the 11th pin from the right on the top row and the Ground line is the 9th from the right on the top row.
- Some LEDs produce infrared light which is totally invisible. Make sure that the LED that you are using produces visible light.
- Exceeding the maximum voltage ratings for your LED will burn it out. Make sure that you do not test your LED by connecting it to a power source that exceeds its maximum voltage ratings such as the ooPIC's 5 Volt power connections or the battery.
The schematic for this circuit looks like this:
The symbols on the schematic represent the different parts of the circuit. Lines that are drawn between the symbols are used to represent the wired connections between the parts. From this schematic, we can tell that I/O line 8 is connected to the positive side of the LED. The negative side of the LED is connected to one side of the 220 Ohm resistor while the other side of the resistor is connected to Ground. |
Downloading and Running the Application
|
| | Pressing the F5 key will compile the program and then download it to the ooPIC. If any errors are encountered, they will be reported and the compilation will stop. If this is the first time the program is being compiled, the compiler will ask you for a program name. In this case, call the program FirstApp. Type "FirstApp" in the "File name" area of the "Save" 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. |
Additional Exercises
|
| | Looking back at the description of the oLED Object we see that it has brightness control. Lets do something with that. Change the program so that it reads this way:
For the ooPIC-S and ooPIC-E use I/O Line 8 | For the ooPIC-R and ooPIC-C Use I/O Line 5 |
| Visual Basic syntax | C and Java Syntax |
Dim LED As New oLED
LED.IOLine = 8
LED.TurnOn
Do
LED.Brightness.Inc
Delay = 50
Loop | oLED led = New oLED;
led.IOLine = 8;
led.TurnOn;
Do{
led.Brightness.Inc;
Delay = 50;
} |
| BASIC syntax | |
LED As oLED
LED.IOLine = 8
LED.TurnOn
Do
LED.Brightness.Inc
Delay = 50
Loop | |
|
| Visual Basic syntax | C and Java Syntax |
Dim LED As New oLED
LED.IOLine = 5
LED.TurnOn
Do
LED.Brightness.Inc
Delay = 50
Loop | oLED led = New oLED;
led.IOLine = 5;
led.TurnOn;
Do{
led.Brightness.Inc;
Delay = 50;
} |
| BASIC syntax | |
LED As oLED
LED.IOLine = 5
LED.TurnOn
Do
LED.Brightness.Inc
Delay = 50
Loop | |
|
The first two statements create and set up the hardware interface.
The third statement tells the LED to turn on.
The fourth statement sets up a loop.
The fifth statement tells the LED to increase its brightness.
The sixth statement tells the program to wait 1/20 second before going on.
the seventh statement tells the program to go back to the "do" statement.
Once again we have parts where there is a period between two words with no spaces. But this time we have three words together on the 5th line. As before, be sure to type this in just like it is shown.Press the F5 key to download this to the ooPIC. |
Summary
|
| | The rest of the programmer's guide goes into more detail about each aspect of the programming. But this first application gives an overview of what programming an ooPIC is all about. You have probably figured out by now how easy it is to program an ooPIC. Simply create objects and then tell them what to do. There are several different objects that you can use to control electronic devices and each one is just as easy as the LED was to control. For instance, look at the oSoundgin Object.
| | Object | Description | A1 | A2 | B1 | B2 | C1 |
 | oSoundgin | Controls a Soundgin Sound Effects Engine / Voice Synthesizer chip. | x | x | x |
|
This object knows how to control a Soundgin™ with statements such as:
Voice.Say("The distance is") Voice.SayNumber(x) Voice.Say("miles")
When you get a chance, take a look at the complete Object List. |
|