Easy PC to PICAXE Programmable Serial
Comms
I've been using
PICAXE microcontrollers for several years now on my DIY
projects. The PICAXE range has several microcontroller chips
with a wide range of capabilities - with the more recent
ones capable of running at clock speeds up to 64MHz.
As a newcomer to microcontroller use I found
PICAXE's particularly attractive because of their wide range
of capabilities, and because they are easy to program using
a BASIC type language and their free programming software.
However one area where programming can be more difficult is
in achieving programmable serial communications between your
PC or Laptop and the PICAXE chip on your project. This can
be a key element in many DIY projects in which
communications between the PC and microcontroller is
required to exercise control over the chip behaviour, or to
receive and act on real-time data sent from the chip.
Such serial communications can be achieved
using a range of programming languages and
this thread from the PICAXE forum provides information
on a number of them. With a view to helping beginners in the
area I've tried to add to the available options by making up
a PC-PICAXE Serial COMMS API for
Auto Hot
Key (AHK). AHK is a fairly easy to learn FREE scripting
language with huge capabilities for general programming as
well as for keyboard/mouse key scripting etc. All the motion
platform drive software, robot control software and force
feedback software seen on the site is written in AHK.
BFFCOMMS - Serial COMMS API
Download BFFCOMMS.zip
package here
The BFFCOMMS API is in the form of a small
library of pre-scripted functions that can be called in your
AHK scripts to implement serial port communications - ie to
read and write data to devices connected to your PC by a
physical serial port or by USB/Serial converter cable.
You make the library functions available in
your own AHK scripts by adding an "#Include"
statement near the top of the script, ie add...
#Include BFFCOMMS_Func.ahk
.. in your script and place the
BFFCOMMS_Func.ahk library file in the same directory as
your own script.
There are a small number of functions in the
library:
-
BFFCOMMS_Open() - Open the COM Port
-
BFFCOMMS_Close() - Close the COM Port
-
BFFCOMMS_Send() - Send bytes to the COM
port
-
BFFCOMMS_Get() - Read bytes from the COM
port
-
BFFCOMMS_Purge() - Purge the read buffer
The Open() function allows the port number,
baud rate and read timeouts to be set and the port opened
with 8,N,1 serial format suitable for PICAXE communications.
The Send() function can send both ascii and raw data bytes
to your PICAXE and the Get() function can read both ascii
and raw data bytes from the microcontroller with or without
qualifier bytes or read timeouts for you to process in your
script.
More details of the functions are in the
BFFCOMMS Reference
text file.
Sample Serial Communications Scripts
To help you get started I've written a number
of example AHK scripts with matching PICAXE 20X2 chip .bas
programs. The samples are included in the download package
and illustrate a number of ways of using the API. I've used
a 20X2 chip set for 64MHz operation and with "hserial"
programming through its hardware serial pins, however it
should not be difficult to modify the .bas programs for use
with other PICAXE chips and frequencies and with normal
serin/serout programming on any suitable pins.
The samples use COM1 at 115200 baud, you can
change this by altering the scripting - open the relevant
.ahk file in a text editor and have a look. If you change
the baud you will also have to change the matching .bas
microcontroller program to ensure the bauds are the same
at both PC and PICAXE ends of the chain.
The code samples are:
-
Case 1 - simple write and read return
data (message box output)
-
Case 2 - simple write with read return
with serial string qualifier bytes (message box output)
-
Case 3 - send G, A or R keystrokes to
light green, amber or red LED's (GUI window output)
-
Case 4 - continuously send PC joystick
axis positions to vary LED light brightness (GUI window
output)
-
Case 5 - continuously monitor ADC word
data sent by PICAXE at approx 30Hz (GUI window output)
To use the samples you will need to download
and install the AHK package from the
AHK web site. Once
installed you can run any AHK script directly by double
clicking on it, or by compiling it to a .exe program and
running that. At the microcontroller end you will need your
PICAXE microcontroller active and programmed with the .bas
program that matches the Case .ahk script you are trying.
The programmed PICAXE 20X2 will need to be connected to the
PC COM port specified in the .ahk script through its hserial
pins and a correct download circuit and
download cable.
Let me know if you find the API useful, or if
there are additional functions that might be added.
The BFFCOMMS API is NOT PICAXE chip or
project hardware specific - ie it can be used to read and
write serial data to any PICAXE chip which is programmed
properly for serial communications and connected through a
suitable download circuit. You are free to use it in your
own projects with your own hardware.
The .bas programs included in the samples
package are however written for a 20X2 chip with 115220 baud
comms through its hserial pins and they do suit the 64SPU-1
microcontroller card that is available on the site.
64SPU-1 Microcontroller Card
Although
designed for DIY closed loop servo drives the 64SPU-1 card is
suitable for general project use and allows the on-board
20X2 chip to be programmed in-situ as well as providing working data
serial comms via the hserial pins. The chip is socket
mounted and can be swapped out if required.
The
card provides screw terminal connections to a number of the
20X2 chip's input/output pins, I2C ready connections, three
coloured LED's and serial comms through the serial download circuit. Click the image for details of the
20X2 pin mappings. The card requires a regulated 5V supply
and is 80mm x 80mm in size. The serial connection is
suitable for use with PICAXE
AXE026 and AXE027 download cables.
The layout includes a 100uF stabilising
capacitor near the supply input, and decoupling caps near
the chip. 0 and 5V pass-through connections are provided for
powering ancillary items such as potentiometers or LED's
from the 5V logic supply to the card.
The un-programmed 64SPU-1 is available for
£29.95 + P&P from the
order page. This un-programmed card will require you to
program the on-board 20X2 chip with your own .bas program to
suit your own projects. Do not order this version of the
card if you wish to use it in the Motion Platform drive.
© This site is
copyrighted, If you'd like more information or have any
comments please contact me at