Thursday, April 26, 2007

Overview so far...

PLEASE NOTE: This is not supposed to be a finished thing! Just a way to control the picaxe, that's all. A simple way, made up by a newbie! I am sure there are a million better ways to do the same thing.

My code is rough and the hardware is dodge and this way of doing things feels like it's all been gaffed together. But the point is this idea came to me, and I decided to try and make it work, that's all.

Please keep in mind that I have only been working with the picaxe and my own microcontroller programs for a few weeks. Nonetheless, everything that is described here has been actually tested (ie. it's not just theory
), albeit at a *very* slow transfer rate. It is a start. And if that is all that this idea can do, then so be it. It will suit at least some of my needs.


Picaxe microcontrollers (specifically the 18X) are suited to send data to other devices such as over the I2C protocol, one wire interfaces, MIDI output and generic serial data send at baud rates of 300 to 19200 bd.

MIDI input appears to be quite difficult to achieve, and a successful attempt has not yet been documented. Another approach is needed. The aim in either case is to control some aspect of the microcontroller and potentially the physical world from a standard personal computer host system.

Another Approach
A possible next approach in controlling / communicating with the Picaxe is via an audio line that transmits data via a square waveform with a modulating frequency or amplitude. Using an audio line to transmit control data via an audio connection presents certain advantages as well as disadvantages.

The main advantage of using audio to transmit data is that complex and extremely long ‘control programs’ can be recorded and played back from CD audio devices and portable audio devices such as an MP3 player. Furthermore, from a philosophical point of view, the concepts of decontexualisation and symbolism (due to the use of an audio signal as data transfer) can be interesting.

The main disadvantages include the possible slow speed of transfer due to the use of certain commands and constraints and the use of resources that include the engagement of at least one digital to analogue audio converter (a feature that is standard on modern computer systems). However, data transfer / communication via a MIDI connection requires a standalone / external MIDI converter (and therefore represents an additional cost for the user).

1-Bit Data Send
A basic approach to sending some sort of control data to the Picaxe chip is via a simple on/off type command triggered by a change in frequency of a square waveform. Such a setup is perfect for controlling devices with two states, such as relays and LEDs, or for turning a motor off and on at a fixed frequency.

A suitable waveform for such a 1-bit data send is as follows:

The minimum length for Period A and B is approx. 20 to 30ms. The BASIC command SERIN (Serial Input) is used to analyse and differentiate between these two states. The waveforms produce ‘pseudo data’ due to the phasing between the period of a single cycle of the square wave versus the expected data rate by the microcontroller. The highest baud rate at 4.000 MHz is used (2400 bd). 1000Hz returns the value 169 while 1500Hz returns the value 75.

The following BASIC code is suitable for reading a 1-bit data stream:

SERIN 0,T2400,b0
IF b0 = 169 THEN go_low
IF b0 = 75 THEN go_high
GOTO main

GOTO main

GOTO main

Single Transition, Multi State Encode
By using additional square wave frequencies, more than just two states can be encoded into a single transition. It appears as though the highest value that can be encoded is a total of eleven states. Other frequencies either return overlapping values with other frequencies or return values that fluctuate between more than two numbers.

The following table contains information based on preliminary experimentation based around the use of different frequencies. The Primary Value and Secondary Value both refer to the data as interpreted by the SERIN command at T2400, 4.000MHz. The term Secondary Value refers to byte values that are returned less often than Primary Values for a given frequency.

Frequency (Hz) Primary Value Secondary Value Logical
3000 153 217 0
2700 199 135 1
2500 255 - 2
2200 192 224 3
2000 28 60 4
1800 102 - 5
1500 75 - 6
1300 165 181 7
1200 85 - 8
1000 169 - 9
700 178 50 10

In order to deduce the logical value of a received serial byte value, a lookdown table comparison is required. Two LOOKDOWN commands are required to compensate for the appearance of the secondary values.

The BASIC code for such an operation is as follows:

SERIN 0,T2400,b0
LOOKDOWN b0,(153,199,255,192,28,102,75,165,85,169,178),b1
LOOKDOWN b0,(217,135,255,244,60,102,75,181,85,169,50),b1

9-Bit Data Encode and Receive
As an extension of the previous two examples, a number between 0 and 511 (9 bits’ worth of data) can be encoded and sent by the computer and received by the microcontroller using three ‘oct’ bits.

To encode the data, a division and modulus chain is used to convert base 10 numbers into three-digit base 8 values. These are then compared with a lookup table that retrieves the correct frequency for a given state between 0 and 7 (ie. base 8).

Furthermore, an ‘on’ or ‘read’ state of $FF as a serial value (a frequency of 2.2KHz) is placed before the three octs. The three oct byte is also followed by an ‘off’ or ‘stop’ frequency of 700Hz.

To reconstruct the original number, the three octs must be multiplied by 64, 8 or 1 (depending on their position in the byte) and then the resultant values must be summed.

The reason for using a three oct byte as opposed to a nine bit byte is to keep the serial transmission as efficient in terms of time and delay as possible. Also, a base 8 number is easier to work with than a base 11 number (which would require a bit more maths on the mcu).