BASIC4PPC COMPILER
PROGRAM EXAMPLE:
D-PAD
The Pocket PC used in this example is an HP IPAQ H2200 PDA with:
64 MB of system RAM
32 MB of system flash ROM ( Dated: 5/22/2003 )
OS Version: Windows CE 4.20
Display Resolution: 240 horizontal pixels by 320 vertical pixels
Processor Type: Intel PXA255
Bluetooth
1 SDIO Card Slot
1 Compact Flash Card Slot
Headset / Microphone Jack
This HP H2200 PDA was manufactured in 2003. Any of the newer Pocket PCs with Bluetooth
capability running the latest version of the Basic4ppc Compiler should be able to run this simple
program example.
The embedded bluetooth hardware module used in this example is a BlueSmiRF module. You can
search the Internet and find a variety of embedded bluetooth module boards and fully packaged
plug-and-go bluetooth dongles.
The microcontroller used in this example is the: Athena. The Athena micro is a PIC microcontroller
that is pre-programmed with a BASIC Interpreter. The Athena can be programmed easily in BASIC
using the IDE software written to run on a Windows compatible desktop PC or laptop.
This program example
will show you how to
use the PDA's D-Pad to
send characters to a
microcontroller with a
wireless Bluetooth
Serial Port link.
In this BASIC4PPC program example, we are going to use the D-PAD joystick button on the
Pocket PC to send "Commands" to a microcontroller across a wireless Bluetooth serial port
connection. We are going to assign an ASCII character to each of the D-PAD's button
positions: UP, DOWN, LEFT, RIGHT and MIDDLE.
The following ASCII characters are assigned to the UP, DOWN, LEFT, RIGHT and MIDDLE
D-PAD button positions.
Assign the ASCII character "U" for the D-PAD's UP button position.
Assign the ASCII character "D" for the D-PAD's DOWN button position.
Assign the ASCII character "L" for the D-PAD's LEFT button position.
Assign the ASCII character "R" for the D-PAD's RIGHT button position.
Assign the ASCII character "M" for the D-PAD's MIDDLE button position.
There is a TextBox called TX which shows the ASCII characters that are being transmitted
by the Pocket PC when a button is pressed.
There is a TextBox called RX which shows the ASCII characters that are being received
by the Pocket PC from an external device across the wireless Bluetooth serial port link.
By using the TX TextBox, the Pocket PC's Soft Input Panel Keyboard and the "Send TX"
button you can create ASCII text strings that can be transmitted to your external hardware.
Enter the text string in the TX TextBox and press the "Send TX" button and the ASCII text
string will be transmitted to your external hardware across the wireless Bluetooth serial
port connection.
This program example uses the BlueSMiRF wireless Bluetooth module to create a serial port
link with the external hardware. The external hardware in this example is the ATHENA
microcontroller.
We can wire up the BlueSMiRF module up in "Loop Back" mode to verify the ASCII
characters that are being transmitted by the program when the D-PAD's buttons are
being pressed. This is a simple troubleshooting method that can be used to verify that
the BASIC4PPC applications that we write are sending the correct ASCII characters to
our external hardware designs.
Connecting the BlueSMiRF module Tx line to its RX line creates a "Loop Back" path
where every character that the BlueSMiRF module receives from the Pocket PC gets
transmitted back to the Pocket PC and the characters will show up in the RX Textbox.
This provides a simple way to test a BASIC4PPC software application that uses Bluetooth
serial port communication.
BlueSMiRF Module Configured in Loop Back Mode.
Pocket PC's D-PAD Joystick Button.
The first step is to Open the Bluetooth serial port
connection between the Pocket PC and the BlueSMiRF
module.
Using the Pocket PC's D-PAD joystick button:
Press the "UP" position on the D-PAD.
This action will send the ASCII character "U" to
the BlueSMiRF and the ATHENA microcontroller.
Using the Pocket PC's D-PAD joystick button:
Press the "DOWN" position on the D-PAD.
This action will send the ASCII character "D" to
the BlueSMiRF and the ATHENA microcontroller.
Using the Pocket PC's D-PAD joystick button:
Press the "LEFT" position on the D-PAD.
This action will send the ASCII character "L" to
the BlueSMiRF and the ATHENA microcontroller.
Using the Pocket PC's D-PAD joystick button:
Press the "RIGHT" position on the D-PAD.
This action will send the ASCII character "R" to
the BlueSMiRF and the ATHENA microcontroller.
Using the Pocket PC's D-PAD joystick button:
Press the "MIDDLE" position on the D-PAD.
This action will send the ASCII character "M" to
the BlueSMiRF and the ATHENA microcontroller.
To send and ASCII text string to the external hardware,
you must first type characters into the TX TextBox using the Pocket PC's Soft Input Panel (SIP) keyboard.
To transmit the ASCII character string to the external
hardware, press the "Send TX" button.
If the BlueSMiRF module is configured in "Loop Back" mode then the ASCII character string that was transmitted when the "Send TX" button was pressed
will be looped back to the Pocket PC where it will be
displayed the the RX TextBox.
To clear the graphic indicator circles and the information in the TX and RX TextBoxes press
the "Clear" button.
When you are finished, press the "Close" button to
shut down the Bluetooth serial port connection between
the Pocket PC and the BlueSMiRF Bluetooth module.
BASIC4PPC Program Listing for this Example.
Sub Globals
portflag = 0
strlabel0="COM 8: 9600,N,8,1"
strlabel1=" [OFF]"
strlabel2=" [ ON]"
strlabeltemp=""
End Sub
Sub App_Start
Form1.show
textbox1.Text=""
textbox2.Text=""
label6.Text = strlabel0 & strlabel1
strlabeltemp = strlabel0 & strlabel1
serial.New2 (8,9600,"N",8,1)
textbox1.FontSize=13
textbox2.FontSize=13
image7.Image="sr.jpg"
Sip=false
Form1.Focus=true
End Sub
Sub form1_keypress(specialkey)
If specialkey=cUpKey Then
image1.Image="sg.jpg"
image2.Image="sw.jpg"
image3.Image="sw.jpg"
image4.Image="sw.jpg"
image5.Image="sw.jpg"
serial.Output ("U")
Textbox1.Text=Textbox1.Text & "U"
End If
If specialkey=cDownKey Then
image1.Image="sw.jpg"
image2.Image="sg.jpg"
image3.Image="sw.jpg"
image4.Image="sw.jpg"
image5.Image="sw.jpg"
serial.Output ("D")
Textbox1.Text=Textbox1.Text & "D"
End If
If specialkey=cLeftKey Then
image1.Image="sw.jpg"
image2.Image="sw.jpg"
image3.Image="sg.jpg"
image4.Image="sw.jpg"
image5.Image="sw.jpg"
serial.Output ("L")
Textbox1.Text=Textbox1.Text & "L"
End If
If specialkey=cRightKey Then
image1.Image="sw.jpg"
image2.Image="sw.jpg"
image3.Image="sw.jpg"
image4.Image="sg.jpg"
image5.Image="sw.jpg"
serial.Output ("R")
Textbox1.Text=Textbox1.Text & "R"
End If
If specialkey=cMiddleKey Then
image1.Image="sw.jpg"
image2.Image="sw.jpg"
image3.Image="sw.jpg"
image4.Image="sw.jpg"
image5.Image="sg.jpg"
serial.Output ("M")
Textbox1.Text=Textbox1.Text & "M"
End If
End Sub
Sub ImageButtonClear_Click
image1.Image="sw.jpg"
image2.Image="sw.jpg"
image3.Image="sw.jpg"
image4.Image="sw.jpg"
image5.Image="sw.jpg"
Textbox1.Text=""
Textbox2.Text=""
Form1.Focus=true
End Sub
Sub ImageButtonClear_ButtonDown
image8.Image="sr.jpg"
End Sub
Sub ImageButtonClear_ButtonUp
image8.Image="sw.jpg"
End Sub
Sub ImageButton1_Click
image6.Image="sr.jpg"
image7.Image="sw.jpg"
If portflag = 0 Then
serial.PortOpen=true
portflag = 1
label6.Text = strlabel0 & strlabel2
strlabeltemp=label6.Text
serial.enableoncomm=true
serial.Output ("*")
End If
End Sub
Sub ImageButton2_Click
image6.Image="sw.jpg"
image7.Image="sr.jpg"
If portflag = 1 Then
TextBox1.Text=""
serial.Output ("^")
serial.enableoncomm=false
serial.PortOpen=false
portflag = 0
label6.Text = strlabel0 & strlabel1
strlabeltemp=label6.Text
End If
End Sub
Sub serial_OnCom
TextBox2.Text=TextBox2.Text & serial.Inputstring
End Sub
Sub ImageButtonSendTX_Click
serial.Output (Textbox1.Text)
Form1.Focus=true
End Sub
Sub ImageButtonSendTX_ButtonDown
image9.Image="sr.jpg"
End Sub
Sub ImageButtonSendTX_ButtonUp
image9.Image="sw.jpg"
End Sub
'The Pocket PC D-PAD joystick button program written
'for the ATHENA microcontroller receives ASCII command
'characters that are transmitted by the BASIC4PPC application
'which runs on the Pocket PC.
'The ATHENA will process the ASCII command characters that
'it has received and perform the programmed function.
'The KRONOS ROBOTICS ATHENA EDITOR Version 2.0.9 was
'used to Write the program code, Test, Debug and Program
'the compiled code into the ATHENA microcontroller.
'Dimension the variables used by the program.
dim a,s2cpu
'Initialize the Send to Cpu Flag equal to Zero.
'When this Flag = 0 No data will be processed
'by the ATHENA microcontroller. When this
'Flag = 1 data will be processed by the ATHENA
'microcontroller.
s2cpu=0
'Initialize Port Pin 0 as an Input. Serial data
'received by the BLUESMIRF module from the POCKET
'PC PDA across the Bluetooth connection will be
'received by the ATHENA microcontroller at its
'Port Pin 0. The TX line of the BLUESMIRF module
'is physically wired to Port Pin 0 of the ATHENA.
input 0
'Set the Baud rate on the ATHENA to 9600:N:8:1
setbaud SBAUD9600
'The following line of code will read the serial data
'being sent to Port Pin 0 of the ATHENA. If no data
'is currently being received at Port Pin 0 then program
'control will jump back to the label ( loop1 ). This
'program statement will continually jump back to label
'( loop1 ) until an 8-bit byte is received at Port Pin 0.
'When an 8-bit byte is received at Port Pin 0, the
'received character byte will be stored in the program
'variable ( a ) and program execution will fall through
'to the next line of program code.
loop1: serin loop1,0,a
'If the application running on the POCKET PC PDA sends
'special character ( * ), then set the ( s2cpu ) flag = 1.
'This will tell the ATHENA to go ahead and process the
'8-bit character bytes that it receives. Once the ( s2cpu )
'flag has been set equal to 1, program control will jump
'back to program label ( loop1 ) to receive the next byte.
if a=42 then
s2cpu=1
goto loop1
endif
'If the application running on the POCKET PC PDA sends
'the special character ( ^ ) then set the ( s2cpu ) flag = 0.
'This will tell the ATHENA not to process any of the 8-bit
'character bytes. Once the ( s2cpu ) flag has been set
'equal to 0, program control will jump back to ( loop1 )
'to receive the next byte.
if a=94 then
s2cpu=0
endif
'When the (s2cpu) flag is set equal to 1, then go ahead and process the received
'8-bit chararacter byte.
if s2cpu=1 then
if a=85 then
' The POCKET PC has transmitted the ASCII character U which is 85 decimal.
' Put the software code that you want to run when the
' D-PAD UP button is pressed here.
endif
if a=68 then
' The POCKET PC has transmitted the ASCII character D which is 68 decimal.
' Put the software code that you want to run when the
' D-PAD DOWN button is pressed here.
endif
if a=76 then
' The POCKET PC has transmitted the ASCII character L which is 76 decimal.
' Put the software code that you want to run when the
' D-PAD LEFT button is pressed here.
endif
if a=82 then
' The POCKET PC has transmitted the ASCII character R which is 82 decimal.
' Put the software code that you want to run when the
' D-PAD RIGHT button is pressed here.
endif
if a=77 then
' The POCKET PC has transmitted the ASCII character M which is 77 decimal.
' Put the software code that you want to run when the
' D-PAD MIDDLE button is pressed here.
endif
endif
goto loop1
Program Code Listing for the ATHENA Microcontroller.
If you hold the D-PAD button in the "UP" position it will
autorepeat and you will see the ASCII character "U"
being transmitted repeatedly.
If you hold the D-PAD button in the "DOWN" position it will autorepeat and you will see the ASCII character "D"
being transmitted repeatedly.
If you hold the D-PAD button in the "LEFT" position it will autorepeat and you will see the ASCII character "L"
being transmitted repeatedly.
If you hold the D-PAD button in the "RIGHT" position it will autorepeat and you will see the ASCII character "R"
being transmitted repeatedly.
If you hold the D-PAD button in the "MIDDLE" position it will NOT autorepeat. The ASCII character "M" will be
transmitted once every time the D-PAD's MIDDLE position is pressed and released.