A Basic Stamp (or other microcontroller) is just the ticket for many control applications. However, sometimes you really want to control the real world from a PC. Our latest kit, the GP3, can provide practically any PC with 8 digital I/O pins, 5 10-bit A/D converters, a hardware PWM output, and a hardware counter. It connects directly to the PC's serial port and -- this is the interesting part -- we provide ActiveX libraries so you can use the GP3 with Visual Basic, C++, Delphi, Active Server Pages, or any language that supports ActiveX.
If you don't want to program, you need to check out GP3EZ which is our "point and click" software that lets you create applications with the GP3 with no programming. In many cases you can even download your application to the GP3 and run it without a PC.
If you want the ultimate control, however, you'll want to use one of the libraries and the langauge of your choice.
The ActiveX control (or DLL) supplied makes it simple to control the GP-3. Here's a summary of the functions available:
high/low - Set output pin high/low
a2d - Read A/D channel
inp - Read one input pin
pins - Set output pin state
tris - Set direction of I/O pins
eeprom - Read/write EEPROM
portopen - Connect or disconnect
pwm - Output PWM
toggle - Toggle output pin
count - Count pulses on input pin
led - Set state of indicator LED
pulsein - Read pulse
freq - Output a frequency
pulseout - Output a single pulse
rctime - Measure an RC discharge time
setcounter - Configure timer/counter
counter - Read hardware timer/counter
ok - Check for connection
errflag - Check for errors
comport - Set com port
ready - Check status
How easy is using the GP-3? Easy! Here's a simple Visual Basic event handler that reads an analog input:
RawVolt.Caption=GP3IO1.a2d(0)
If you want to convert the raw counts to a voltage and format the data, that's easy to do in Basic:
Volt0.Caption = Format(GP3IO1.a2d(0) / 1023 * 5#, "0.##")
Want to turn on an LED from an ASP Web page? That's easy too:
<%
set io = Server.CreateObject("AWCGP3DLL.GP3DLL")
io.commport=1
io.portopen=true
io.led=Request("LED") ' set LED based on user request
io.portopen=false
%>
You can do the same sort of thing from an Excel spreadsheet (using VBA).
If you want to use the GP3 from a different platform our portable C library will work with nearly any standard C compiler and includes porting examples for Linux, Cygwin, and Windows. You can browse the online documentation for the library, or download a local copy for yourself.
You can also use the board from Java. We have an example of interfacing the Java library to JavaScript (using the Rhino JavaScript library). This is great for interactive debugging. Here's an example of a JavaScript that exercises the board:
// Test script for GP3 // To run: java GP3Script gp3test.js
io=createGP3();
io.openPort("COM7"); // change to match your COM port!
function readvolt(n)
{
return io.a2d(n)/1023.0*5.0;
}
for (i=0;i<1000;i++)
{
io.toggle(0);
print(readvolt(0)); // print voltage on analog in 0
io.setLED(i%2);
pause(500);
}
Suppose you want to monitor a temperature with your PC. If the temperature exceeds a certain range, you want to turn on a light, beep an alarm, and turn on a fan. What's more, you want the fan to spin faster as the temperature rises higher above the limit.
Of course, while you are monitoring the temperature, you could store the data in a database, a spreadsheet file, or serve it over the Internet. But for now, let's just focus on the basic task at hand. Speaking of basic, Visual Basic is a good language for this sort of program. The GP3 works well with Visual Basic and it can easily read an LM34 (this is a three-terminal sensor that reads 10mV/degree F). So the basic connections are simple:
For testing purposes, you can connect an LED to the PWM output and observe the brightness change instead of the actual fan speed. Here's what the final program will look like:
Using the GP3 ActiveX control is very simple. First, you use the Project | Components command in Visual Basic. From the list, pick AWCGP3 as the control to add to your project. If you don't see it, you probably haven't installed the control. Once you are through, you'll see an icon in your tool box that has a large number "3". This is the control. Place it on your form in the usual way. This control is like the timer control -- it is only visible during design time. You can't see it while the program is running.
To use the control you have to set the COM port that the GP3 uses. You can do this during design time, but I wanted to make the program allow you to set it. Once you set the COM port (the commport parameter) you have to set portopen to TRUE to make the control connect. From there it is easy to manipulate the GP3's ports. For example, to read analog channel 0 (assuming you've named the GP3 control IO):
volts=IO.a2d(0)
That's it! Of course, the real program averages 5 readings and converts it to a temperature:
' average 5 samples adv = 0 For i = 1 To 5 adv = adv + IO.a2d(0) Next adv = adv / 5 t = adv / 1023# * 5 * 100 Temp.Caption = Temp.Tag & Format(t, "#0.0") & " F"
The program above uses a timer to periodically scan the temperature and compares it to the limit field. Then it determines if the temperature is over limit:
If t > l Then ' t is temperature, l is limit deltat = t - l
' Turn the fan on harder as deltat increases Select Case deltat Case Is < 2 dc = 10 ' 10/255 (4%) Case Is < 10 dc = 64 ' 64/255 (25%) Case Else dc = 255 ' 255/255 (100%) End Select
Temp.ForeColor = vbRed IO.high 0 ' turn on alarm light IO.pwm -1, dc, 5000 ' hardware PWM at 5kHz IO.freq 1, 1000, 250 ' make a 250mS beep on the speaker Else
' Everything is OK so stop everything Temp.ForeColor = vbGreen IO.low 0 IO.pwm -1, 0, 5000 End If
You can download the entire project in one zip file. You'll also need the library files for the GP3. It would be nice to add some hysteresis, and you can probably think of many other enhancements.
The serial protocol between the PC and the GP3 is efficient and fast (57600 baud). However, sometimes you want things to go as fast as possible. Here is a simple "oscilloscope" (actually more like a strip chart recorder) that I wrote.
Each screen refresh needs to draw the A/D samples as fast as possible. The GP3 has a repeat mode for several commands (including a2d) so you can have the output repeat some number of times. Here's the code for a screen refresh:
IO.repeat xmax + 1 n = IO.a2d(0) For xpos = 0 To xmax If quit Then busy = False Exit Sub End If ' n between 0 and 1023 Dim y As Single Dim volt As Single Dim yint As Integer y = ymax y = y * n / 1024 If xpos Mod 64 = 0 Then volt = (n / 1024#) * 5 OScope.Caption = "Last = " & Format(volt, "0.00") & "V" End If yint = ymax - Int(y) If xlast <> -1 Then OScreen.Line (xlast, ylast)-(xpos, yint) xlast = xpos ylast = yint If xpos <> xmax Then n = IO.readword Next
The xmax variable contains the maximum x coordinate of the screen. The repeat command tells the GP3 to repeat the next command (in this case, a2d). After the program reads the first sample, it uses readword to get the subsequent readings. This saves overhead of having to ask for the sample hundreds of times.
You can download the complete scope project in a single zip file.
Microsoft's Web server (IIS) supports ASP (Active Server Pages). ASP scripts can access ActiveX DLLs so... Yep, you can use the GP3 with ASP! Look at this:
This Web page was created with the following ASP script:
<HTML>
<HEAD>
<META HTTP-EQUIV=REFRESH CONTENT=10>
</HEAD>
<BODY BGCOLOR=CORNSILK>
<%
set io = Server.CreateObject("AWCGP3DLL.GP3DLL")
io.commport=7
io.portopen=true
io.led=Request("LED")
tmp=io.a2d(0)
io.portopen=false
t=tmp/1023*5*100
t=FormatNumber(t,1)
%>
<H1>GP3 DEMO</H1>
<P>Current temperature=<%= t %>F
<P>
<A HREF=io.asp?LED=1>LED ON</A> <A HREF=io.asp?LED=0>LED OFF</A>
</BODY>
</HTML>
Notice the META tag causes the page to refresh every 10 seconds. The Server.CreateObject call creates a copy of the GP3 library. I've hard coded the COM port (yes, I have 8 ports on my PC). In addition to the temperature display, the page also has links to turn the LED on the board on and off. Of course, you would have to worry about multiple users fighting for control of the GP3, but that's just the Web for you.
Using the Windows Scripting Host, you could even control the GP3 from a JavaScript (or VBScript) "batch file" (for more about Windows Scripting Host, read this from Microsoft). Here's a sample script:
obj=WScript.CreateObject("AWCGP3DLL.GP3DLL");
obj.commport=7;
obj.portopen=true;
obj.freq(0,1,10000);
obj.portopen=false;
This "batch file" outputs a 1 Hz sine wave for 10 seconds (you can see this very nicely with an LED connected to pin 0).
You can even use the ActiveX DLL from Excel.
You can use the GP3 anywhere you need to sense or control the real world with a PC. In fact, the device is serial so it will work with any computer or operating system if you control it properly. Need to run a machine tool? Monitor analog sensors? Control a test fixture? The GP3 makes it a snap to interface the world to your PC. Although the examples here use Visual Basic and VBScript, you can use the GP3 with C++, C#, Delphi or practically any language (We have a simple C++ example available). Today, the ActiveX libraries provide many options and we will release other libraries for other development tools as demand warrants. For example, if you prefer to use a standard DLL, you can download the non-ActiveX DLL. You can also use the board from Java and we even include an example that lets you program it with JavaScript. You can download the Java files in this zip file.
Site contents © 1997-2018 by AWC, Houston TX (281) 334-4341