 |
|  |
 |
| Implementation of the GPIO Port |
 |
 |
 |
 |
 |
 |
 |
| Using GPIO port to count up on bit 6, count down on bit 7 and report count result on bit 0-5 |
Here we will show how to use the GPIO port C on the SaJe board.
We have made two examples
- In the first example we show how to setup GpioPins and a GpioField, and how to program so that we can count up and down when connecting the pins with a wire to GND (Ground) on the SaJe bord.
- In the next example we connect the GPIO port C to a light-diode board with two buttons (a count-up and a count-down button) and six light-diodes which will show the value of the counter as a binary number.
In the JEMBuilder:
When you come to Project Drivers: select Port_C.
Implementation of the first example:
Here you can see how to initialize a GpioPin and a GpioField. Then we add a TriggerEventListener to the pin. This event listener is implemented as an anonymous class. class GpioCCounter
{
private GpioField outputPort; // The port to be used to present the count.
private GpioPin countUpPin; // The pin to be used as a "count up" source.
private int count; // The counter.
public GpioCCounter()
{
// IO port C, pin 6 is used as an up counter:
countUpPin = new GpioPin(GpioPin.GPIOC_BIT6);
countUpPin.setPinReportPolicy(GpioPin.REPORT_RISING_EDGE);
// IO port C, bits 0..5 are used to indicate the counter:
outputPort = new GpioField(GpioPin.GPIOC_BIT0, 6);
outputPort.setOutputField(true);
// implement an anonymous class, then register a listener to count up
// and report the count
countUpPin.addReportListener(
new TriggerEventListener()
{
public void triggerEvent()
{
count++;
reportCount();
}
});
}
public void reportCount()
{
outputPort.setFieldState (count); // is not used in this example
System.out.println("*** reportCount: count = " + count);
}
}
Implementation of the second example:
Below you can find an temporary description of the light-diode board (under construction: version 0.9, in danish). The board has 6 LEDs (Light Emitting Diodes) and 2 buttons. With a 8-conductor ribbon cable you can connect it to the GPIO port C (P7). There are 2 wires: one wire to connect to GND (Ground, P14 or P15) and the other to connect to 3.3 V (P17). And at last it has a power input on 9VDC and 450 mA.
If you try to use the light-diode board together with the first program example above, you may get the following output on Charade, when you push the up-counter button: [TEXTIO.0]->*** reportCount: count = 12
[TEXTIO.0]->*** reportCount: count = 11
[TEXTIO.0]->*** reportCount: count = 12
[TEXTIO.0]->*** reportCount: count = 13
Next you may get the following output when you push the down-counter button: [TEXTIO.0]->*** reportCount: count = 12
[TEXTIO.0]->*** reportCount: count = 11
[TEXTIO.0]->*** reportCount: count = 10
[TEXTIO.0]->*** reportCount: count = 9
The problem is that there is some bouncing on the GPIO pin when a button is pushed.
To solve this problem we will instead use the aJile class PushButton. In the documentation of this class you can read that it is smilar in functionality to a GpioPin, but it "controls and debounces a pusbbutton switch wired to a GPIO pin". A PushButton can be declared and initialized in this way: class Board
{
private PushButton countUpButton; // The button to be used as a "count up" source.
public Board()
{
// IO port C, pin 6 is used as up counter:
countUpButton = new PushButton (GpioPin.GPIOC_BIT6);
countUpButton.setTriggerReportPolicy (PushButton.TRIGGER_ON_PUSH);
.
}
.
}
The source code to the two examples and the light-diode board:
GPIO port C counter example. GPIO port C counter example with a light-diode board. The light-diode board (in danish). |
 |
 |
 |
 |
 |
 |
 |
|
 |
|
|  |
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
 |
Implementation of the GPIO Port |
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|