PIC DAS User Instructions
The software support for the
PIC DAS ranges from native RS232 Prints and Inputs as would be used with a DOS
based BASIC to an ActiveX control [1] that I developed to make Windows
programming more automated. In addition
to control with more traditional programming languages, the PIC DAS may be
controlled with any Terminal Emulator (such as Hyper-terminal, supplied with
Windows).
The PIC DAS has a built in
power switch that is connected to the DTR line of the RS232 connection. When
the DTR is low (as it usually is when a COM port is closed) the power to the
PIC DAS will be off. When the COM port that the PIC DAS is connected to is
opened (which usually asserts the DTR line, even with no handshaking) the battery
is connected to the internal circuits of the PIC DAS powering it up (of course
all previous settings are forgotten when the power is cycled).
In all there are 11 commands
that the PIC DAS understands. Seven of these have to do with the Digital I/O,
two deal with the analog I/O and one is an identification command and one sets
the EOL character to a Line Feed as would be required for UNIX languages (Like
RMB).
The command structure to the
PIC DAS is as follows,
COMMAND
[Parameter 1] [Parameter 2] {CR}
The command is always
required however it may be abbreviated to just the first two characters. A
space character follows the command if required for the command Parameter 1.
Likewise a space is required between parameter 1 and 2. The command is finished
and executes when a Carriage Return is received by the PIC DAS.
Note that not all commands
have parameters. Commands may have none, one or two parameters. If the proper
number of parameters are not received by the PIC DAS the results of the command
will be unpredictable.
In QBasic [2] the Direction
command would be sent as,
PRINT #1 “DIR 255”
When the print command finishes, it terminates
the string with a carriage return and the PIC DAS then parses the command and
executes it. As was stated above, alternative methods of sending the command
are,
PRINT #1 “DI 255”
PRINT #1 “dir 255”
PRINT #1 “di 255”
Remember only the first two
characters of the command are looked at and the commands are not case
sensitive.
The standard mode is for the
PIC DAS to execute the command then send back any data (if any) followed by a
carriage return (or a Line Feed if UNIX Mode is set). Even if no data is sent
back a change return is returned, this can be used to keep a really fast PC
from getting ahead of the PIC DAS.
If the PIC DAS encounters a
command it does not recognize, it sends back the string “UNKNOWN COMMAND”. This
can be useful in debugging code that should be working but isn’t.
The sidebar shows a small
program written in Microsoft QBasic that exercises some PIC DAS Commands.
For Windows programming I
have written an ActiveX control, while it is possible to work directly with the
serial port under Windows, The ActiveX control reduces every PIC DAS command to
a simple single function call. ActiveX controls are like component plug-in’s.
They are reusable libraries of commands that are useable across a wide variety
of Windows applications and programming languages. Even the spreadsheet Excel
can use the ActiveX control to control the PIC DAS and all modern 32-bit
Windows programming languages can use ActiveX technology.
A download package is
available from my home page at,
http://web.soco.agilent.com/~shageman
that has the ActiveX install
package for the PIC DAS [1] and an example VEE Panel that allows standalone
control of the PIC DAS.
DIR num n/a Sets the data direction on
the
digital I/O port. num = 0 to 255.
0 = Output, 1 = Input
BSET pin n/a Sets pin to a logic 1
BCLEAR pin n/a Clears pin to a logic 0
BIN pin n/a Returns the state of pin
1 or 0
IN n/a n/a Returns the entire port as a
byte
Returns the port
value (0-255)
OUT num n/a Sets the entire port to byte
num
PULLUP state n/a state = 1 for pull-ups on
state = 0 for
pull-ups off
AIN ch n/a Reads A/D channel ch (0-7)
Returns the A/D
code read
(0-4096)
AOUT ch val Sets D/A channel ch to value
val
VER n/a n/a Returns a string identifying
the
firmware version
Note: All commands and states are reset when the power is
cycled to the PIC DAS.
Note: The command parser in the PIC DAS actually only
decodes the first two letters in the command string. For example: DIR 255 works
exactly the same as sending DI 255 to the PIC DAS. This is perhaps a less
readable but faster form of communication, especially for the longer commands.
DECLARE SUB Delay (dly!)
'This program should
demonstrate how to use all functions of the
'Hagtronics PIC DAS using
QBasic
CLS
PRINT "Hagtronics PIC DAS
QBasic Demonstration Program"
PRINT "Version: 3Aug00,
By: Steve Hageman"
'Standard open com port
command, you maay need to change the COM1 to
'match the COM port that the
PIC DAS is connected to
'The extra parameters here
disable all the hardware timeouts
'When the COM port is opened
the DTR line is enabled which turns on
'the PIC DAS, to turn off the
PIC DAS, close the COM port again
'Opening the COM port also
clears the TX and RX buffers
OPEN
"COM1:9600,N,8,1,CD0,CS0,DS0,OP0,RS," FOR RANDOM AS #1
'Wait for 1 second for the PIC
DAS to start after the COM port opens
Delay (10000)
'===== Get the firmware version
PRINT #1, "VER"
Delay (4000) 'This line is needed to slow down a 700
MHz Pentium!
LINE INPUT #1, ver$
PRINT "PIC DAS Firmware
Version String = "; ver$
'=====< Digital Functions
>=====
'===== Set the Digital I/O to
all outputs
PRINT #1, "DIR 0"
'Note: Even though the DIR
command returns no parameters we
'still need to remove the CR
that the PIC DAS sent back when
'it completed the command
LINE INPUT #1, ret$
'The following print statements
are optional, they just show
'if something went wrong
PRINT ret$
'===== Set all bits high
PRINT #1, "OUT 255"
LINE INPUT #1, ret$
PRINT ret$
'===== Set MSB on, all other
bits off
PRINT #1, "OUT 128"
LINE INPUT #1, ret$
PRINT ret$
'===== Set LSB on all other
bits off
PRINT #1, "OUT 1"
LINE INPUT #1, ret$
PRINT ret$
'===== Set all bits off
PRINT #1, "OUT 0"
LINE INPUT #1, ret$
PRINT ret$
'===== Set all bits as input
PRINT #1, "DIR 255"
LINE INPUT #1, ret$
PRINT ret$
'===== Set pullups on
PRINT #1, "PULLUP 1"
LINE INPUT #1, ret$
PRINT ret$
'===== Read I/O port, should be
255
PRINT #1, "IN"
LINE INPUT #1, ret$
PRINT "I/O Port (pullups
on) = "; ret$
'===== Read bit 0, should be 1
PRINT #1, "BIN"
LINE INPUT #1, ret$
PRINT "Bit 0 Input
(pullups on) = "; ret$
'===== Set pullups off
PRINT #1, "PULLUP 0"
LINE INPUT #1, ret$
PRINT ret$
'===== Read I/O port, should be
0
PRINT #1, "IN"
LINE INPUT #1, ret$
PRINT "I/O Port (pullups
off) = "; ret$
'===== Read bit 0, should be 0
PRINT #1, "BIN"
LINE INPUT #1, ret$
PRINT "Bit 0 Input
(pullups off) = "; ret$
'===== Set direction to all outputs
PRINT #1, "DIR 0"
LINE INPUT #1, ret$
PRINT ret$
'===== Set bit 0
PRINT #1, "BSET 0"
LINE INPUT #1, ret$
PRINT ret$
'===== Clear bit 0
PRINT #1, "BCLEAR 0"
LINE INPUT #1, ret$
PRINT ret$
'===== Count to 255 on output
port
PRINT #1, "DIR 0"
LINE INPUT #1, ret$
PRINT ret$
FOR i% = 0 TO 255
PRINT #1, "OUT "; i%
LINE INPUT #1, ret$
NEXT i%
'===== Turn I/O port off (all
zeros)
PRINT #1, "OUT 0"
LINE INPUT #1, ret$
'=====< Analog I/O >=====
'===== Set DAC CH 0 to 2.5
volts
PRINT #1, "AO 0 2500"
LINE INPUT #1, ret$
PRINT ret$
'===== Read A/D channel 0, 100
times
'If A/D 0 is connected to DAC
0, it should return
'the code 2500
FOR i% = 1 TO 100
ret$ = ""
PRINT #1, "AI 0"
LINE INPUT #1, ret$
PRINT i%, ret$
NEXT i%
'===== Ramp DAC 0, 5 times
PRINT "Writing to
DAC"
FOR i% = 0 TO 5
FOR code% = 0 TO 4095 STEP 100
PRINT #1, "AO 0 "; code%
LINE INPUT #1, ret$
NEXT code%
NEXT i%
'===== That's all folks!
PRINT "End of test
program..."
'===== Close the COM port
‘Closing the COM port turns off
the
‘power to the PIC DAS and it
naturally
‘forgets all of it’s settings.
CLOSE #1
END
SUB Delay (dly!)
FOR i! = 0 TO dly!
NEXT i!
END SUB
The example above shows a
small segment of code written in QBasic that uses all the PIC DAS commands. The
“Line Input” function is a special input function that terminates the input
command when a carriage return is read, this command is perfect for use with
the PIC DAS as it terminates any string sent back with a carriage return. The
VAL() function is used to convert the string representation of the A/D value
sent back by the PIC DAS to a real number.
References:
[1] The ActiveX control for
Windows (9x and NT), the DOS QBasic examples, the PIC firmware source code and
HEX programming file are available from the ARRL download site as the file
PICDAS.ZIP
[2] QBasic is an enhanced
BASIC that has been in MSDOS since version 5 was released. QBasic is also
supplied with Windows 9x and it will run on NT as well.