The joy of interfacing

Prototype interfacing. Not pretty, but it works.

So anyway, get yourself into your time machine and set it for sometime around 1986. Once you get there, pop on your invisibility cloak, find someone who looks spoddy and follow them into the dining room. See that BBC Micro in the corner? Pop over and give the top a quick tug. Chances are it’s not screwed down.

That’s because, in the old days, computers were for hobbyists with soldering irons, and they were forever taking the tops off to install new circuit boards they’d made.

Doesn’t happen much anymore, of course – you might pop the side off once a year to install a new graphics card, but most people wouldn’t consider actually building new hardware to go inside their computer. And for very good reason: the insides of a modern PC are massively more complex and to build an add-on part yourself that would actually be any use is more or less impossible.

Thing is though, designing and connecting your own hardware to a PC, while unlikely to win you admiring glances from the opposite sex, is bloody good fun. Fact. I’ve been tinkering with the whole area again for the past year or so – for reasons I may document at some point – and it turns out that there’s a massive array of components that are relatively easy to interface to a PC and do interesting things with.

So here’s what I’ve done: I’ve interfaced a Microchip MCP3202-C analogue-to-digital converter IC to the parallel port of a PC. And voila, I can use the computer to directly measure any analogue voltage between 0 and 5 volts. In fact I can measure two lots of voltages since the 3202 is a dual-channel device.

Now, the ability to measure some voltage or other doesn’t sound terrifically interesting per se. But it is! Because there’s a vast array of sensors and transducers out there, which measure all sorts of fascinating things about the real world like temperature and pressure and position and humidity and everything. And guess what their output is? Yes! Very often these devices produce an analogue voltage, and very often it’s between 0 and 5V (since 5V is kind of a universal logic voltage).

It’s certainly not as easy as it used to be to connect your computer to the real world. The BBC Micro, for instance, was specifically designed for interfacing and you didn’t even need any external circuitry to measure analogue voltages – you just used the analogue input ports on the back and read the voltage directly from BASIC with the ADVAL statement.

To get my 3202 ADC chip talking to a PC, I first had to add a little bit more buffer circuitry, to get the anaemic voltage levels coming out of its parallel ports to look a bit cleaner. But that’s only a single logic chip, costing about 20p (the 3202 itself currently costs £2.25 from Farnell if you’re only buying one or two).

Next, I abandoned Windows and installed Fedora Linux. Why? Because Linux has the joy of a predictable, stable tool-chain for programming, and it comes with a C compiler and everything you need for programming. It’s a geek’s OS and it’s set up for geeks to tinker with straight away. And if everything you need isn’t there in your particular installation, chances are all you’ll need to do is issue a command something like this:

sudo apt-get install gcc

And off Linux will go, ferreting out the gcc C compiler and all the necessary extra components. Also, of course, it’s free. And I wouldn’t dream of installing Windows on a PC when I didn’t have the correct license.

With that done, I had to decide on a programming environment. Java is my language of choice – it’s the best language on the planet and those people who talk about its nightmarishly complex class libraries are all wrong, honest – so Sun’s brilliant (and free) Netbeans IDE was the only sensible choice.

You may be seeing the problem looming. Java is a high-level language deliberately abstracted from the hardware it runs on, meaning that getting the low-level access I needed to interface with my ADC chip wasn’t going to happen with Java alone. That meant one thing: JNI, the Java Native Interface, which allows you to write native C code and glue it to a Java method. That means fast, native access to the hardware combined with a lovely high-level language to write the graphical front-end for my app.

Only problem with JNI is that it’s hideous. I mean truly hideous. It’s badly implemented and appallingly documented and it took me a week of trial and error actually to mangle my C code for reading the ADC chip into a form that JNI could work with. But eventually I got there.

There’s nothing quite like getting really deeply into the binary operation of a chip. The 3202 IC uses a simple serial protocol called SPI to communicate with the outside world. So I had to sit down with the datasheet in hand and write a routine in C that would directly communicate with the chip in binary, by waggling the voltage level of three of the PC’s parallel-port pins between 0 and 5V. Fairly amazingly, once I’d sorted out the JNI thing it worked first time. My Java graphical front-end can get the voltage levels and display them any way I like, and the hardware can manage a sample rate of about 2,000 readings per second, if I happen to want to measure something that fast.

Not as easy as with a BBC Micro then, but here’s the thing: BBC Micros used to cost about a month’s wages, so the chances of dedicating one to measuring the temperature in the greenhouse were always going to be pretty slim. A little Mini-ITX motherboard – which is what I’m using for my project – is only about £100. That means you can use one as a hardware appliance and dedicate it to the task of measuring, well, whatever it is you want to measure. And it’s the matter of an evening or two’s work to configure a web server and write some code so that you can communicate with it from anywhere and see what it’s been getting up to.

All a little bit pointless? Well, maybe. Fun? Depends on your proclivities I suppose. But it makes me happy.

So, who wants a feature in PC Pro about how to do it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.