OBSERVATIONS FROM SCOTLAND                            28 May 2008                                GM1SXX

Simple computers for Amateur Radio tasks.

'An 'embedded computer for a tenner.'

As radio amateurs many of us wish for things that just are just not available off the shelf as ready-made products.  Examples might be remote antenna switches that could be controlled by an RS-232 or RS-485   serial, or USB port, or antenna rotators that will interface to common tracking programs.  Other applications might include machine generated CW for a beacon or similar.  Radio-Amateurs are a resourceful lot. We have to be, but sometimes it's difficult to find a means to perform a task that needs just a bit of electronic 'intelligence'.

In the past, the PC has been one means of remotely controlling things in the shack. Others have gone down the route of 'embedded systems', simple CPU based systems that can be programmed to perform a specific task. Although very useful, these embedded controllers have tended to be a little bit expensive for general amateur radio use.

Enter the 'new breed', The ATMEL and Arizona Microchip micro-controller families that are essentially self-contained single-chip computers that need virtually no external support in order to perform a useful task.

For this OBS, I'll stick with the Arizona MICROCHIP family of devices.  I've worked with ATMEL chips too, and very good they are, but the 'PIC' range from Arizona Microchip have long been favourites of mine.

My 'apprenticeship' with these devices started quite a few years ago with the 16C 'family' of chips. These are fabricated in CMOS and many of the development parts are of the type that are 'windowed' like EPROMS and are programmed in a similar manner with a dedicated PIC programmer. While useful, these have one big disadvantage, in development work, you need to make frequent changes to code and the need for a UV EPROM eraser and the time overhead associated with erasing them just to reprogram them again makes software development a slow process.

I later changed to a system by Forest Electronic Developments which used an 'on-chip' interpreter that accessed an external EEPROM memory holding the development code. This was a lot more efficient, since EEPROMs are quick to program AND erase.  I used this system until fairly recently. The code when bug free could be compiled (optimising compiler from the same company) and burned into a target chip for inclusion in a finished project.  The disadvantage of this system is mainly in needing to use a dedicated development system with the interpreter burned in to the chip.  It also was a rather simple version of BASIC.  FOREST still do development systems and compilers but they seem to have dropped BASIC altogether in favour of C.  

More recently I came across a more sophisticated system. This one is a compiler from Elektronika http://www.mikroe.com/en/compilers/ that allows the program to be written and compiled then ported across to a FLASH PIC for testing.

The MIKROBASIC IDE in action.

FLASH PIC's use the same sort of re-write-able (non-volatile) memory that's used in those incredibly popular USB memory sticks that you see in use everywhere you find computers! FLASH memory is very fast to program AND erase and is good for many thousands of programming cycles. 

ARIZONA MICROCHIP (yes, they do have the rights to the name!) were quick to take advantage of FLASH technology, incorporating it into their F series of micro-controllers.  As a result, there are now a plethora of devices from the humble 8 pin devices and 'intelligent' swipe cards all the way to powerful micro-controllers with all manner of 'peripherals' on board.

I decided I'd have to standardise on just one or two models and as a result, almost all my projects use the 16F877A device. These are mid-range devices, powerful yet cheap. Best of all, being FLASH micro-controllers, they can be programmed or erased in a few seconds using a very simple 'programmer' connected to my PC.

The 16F877A is a 40 pin chip. It comes in various packaged such as DIL and SMD but I use the DIL types because of their cheapness and ease of use. The PIC is a very unfussy chip and once you have your software working, you can build a prototype with it 'ded-bug' style. No need for a PCB or anything like that. 

A basic circuit for the 16F877A

 

PIN-OUT of the common 16F877 device.

The 16F877A has an identical pin-out

As a minimum, to build a viable circuit, pins 1 (reset circuit), 13&14 (clock circuit) 12+31 (GROUND) AND 11+32(+5v) need to be connected. 

You need to provide a DC supply at around 5V. Pins 12 and 21 are GROUND and pins 11 & 32 go to +5V. An 100N ceramic across the supply as close to the pins as possible provides decoupling.  You need a clock so a 4 or 20Mhz xtal (either crystal frequency is common) is connected across pins 13 & 14 and a pair of 27PF ceramics connect from these two pins to ground. Last of all, you need some sort of re-set circuit connected to pin 1. This need not be any more fancy than a 10K resistor to the +5V line and a 10uF electrolytic to ground.  That's it!  In short, building a complete system using a PIC is simpler than working with many basic logic IC circuits.  It's the code inside that really makes them powerful. 

Here's one I prepared earlier.

If you are wondering why the photo quality is rubbish, that's because both of my digital cameras have quit working and I had to use a hand-held webcam to take the photo.

Total cost...  about a fiver if you don't use a textool socket. The components to the left of the chip are the clock xtal and its two 27pf ceramics while the resistor and capacitor strung vertically are the power-on reset circuit. The components on the right side are a resistor and LED (to do the flashing!) and although very difficult to see, there is a 100N ceramic strung between the two red wires carrying DC power to the PIC.  It's safe to say that this is the simplest computer circuit I've ever built. You'll have to take it from me that the LED actually DOES flash on and off once a second.  I just caught it at a bad moment!

If you are really mean, you can even omit the xtal and the two ceramic caps and just use an R/C clock!

You can see from the photo above exactly how few parts are requited to support a PIC micro-processor.  I used this with the short demo program below to make an LED flasher, an exceedingly trivial application for such a powerful IC!

'******************************************************************************
' microcontroller : P16F877A
' Project: Blink
' This project is designed to work with PIC 16F877A
' with minor adjustments, it should work with any other PIC MCU.
' This code demonstrates blinking of diodes connected on PORTB.
' Diodes go on and off each second.
'*****************************************************************************
program Blink
main:
TRISB = 0 ' Configure pins of PORTB as output
PORTB = %11111111 ' Turn ON diodes on PORTB
while true
PORTB = not PORTB ' Toggle diodes on portb
delay_ms(1000)
wend
end.

The 16F877A comes with a serial USART on board making it very easy to arrange serial communications with the outside world via pins 25 and 26. You can add one of the MAX232 IC's (Maxim) to translate the TTL levels of the PIC into true RS-232 voltages.  These can be found as ready-made units on Ebay. Alternatively, go buy a MAX232CPE from Maplin, add a few 1uF Electrolytics and build your own.

Here's the little board I use to provide RS-232 communications with the chip ... purchased, yup, you guessed it, from EBay.

Some other models of Flash PIC's have USB. Consult Microchip's website for more info.  Want to drive an LCD display? Libraries are included to do that. You can even drive certain models of Graphics LCD's (GLCD's).  Not enough I/O pins?  Microchip do port expander chips that allow you to add more ports. Lots of ports should you really need them.

There is even a family of PIC's with Digital Signal processing built in... the DsPICs.

I usually build most projects on a small plastic 'solderless breadboard' (Maplin  Electronics BZ-13P) and get it tested and working before transferring it to a scrap of printer-circuit-board.   Glued upside-down 'ded-bug' style, to a scrap of PCB, the various connections are made and the whole assembly (what little of it there is) tested.  It can then be potted or placed in an enclosure.    I'm way too embarrased to put a ded-bug PIC on this page.

Solderless breadboards are so cheap that a PIC circuit built on one can simply be 'embedded' into equipment. Not even a soldering iron is then required to construct a working computer circuit.

Verostrip is also an excellent material on which to build PIC circuits. 

 

Programming

For anyone familiar for C, Pascal or BASIC, I'd strongly recommend the compilers from Mikroelektronika as being powerful and easy to use. They are powerful and easy to use while not being tied to a Dongle type key like some other products. My experience of dongles in my work as an ICT Engineer has been entirely negative.   There's a good online forum for each of the Mikroelektronika  various languages and their compilers come with very extensive online help and libraries for most things you can think of.   These are not simple integer-only languages like some of the early offerings. They are powerful and flexible with many libraries and good support via the user base.

MikroBasic provides an excellent  set of libraries which simplify the production of working code.

Best of all... the MIKROELEKRONIKA Compilers may be freely downloaded and used to produce working code but with the restriction of being limited to 2K Bytes of compiled code.  This may seem useless, but I can assure you that useful work can be done with 2K of compiled code.  It's more than enough to evalute the usefulness of these compilers.


Perhaps you'd like to see the PIC BASIC version of the the infamous 'Hello World program?

Here goes! Just don't get too excited!

'******************************************************************************
' microcontroller P16F877A
' Project Hello_World
' This project is designed to work with PIC 16F877A
' This code demonstrates displaying text on LCD (4 bit interface)
' and use of LCD library procedures and functions.
'******************************************************************************

program Hello_World

main:
Lcd_Init(PORTB) ' Initialize LCD connected to PORTB
Lcd_Cmd(LCD_CLEAR) ' Send command to LCD "clear display"
Lcd_Cmd(LCD_CURSOR_OFF) ' Send command cursor off
Lcd_Out(1,1,"HELLO ") ' Print txt to LCD, 1nd row, 1st column
Lcd_Out(2,1,"WORLD!") ' Print txt to LCD, 2nd row, 1st column
end.

Not exactly very difficult!  The above program makes use of a cheap 16 character by 2 line LCD display. These are now cheap and are excellent as a display to use with the PIC micros.  I just bought a load of NOS ones (New old stock) from Greece for £2 each.

The compiler takes the source code and compiles into a HEX file the programmer can accept. The compiled HEX code is what the PIC actually runs.

I use a simple PIC programmer bought from a nice chappie in Hong Kong called Kenny Wong.  Bought it on EBay for buttons (he means really cheap... LA2QAA).  It arrived from Hong Kong in just a few days and the build quality is superb. It cost £2 plus £7:50 for postage.

An 'embedded computer for a tenner'?

Well with PIC's this is entirely possible. In fact the 16F877A is available for around 4 pounds each in the UK.  Add a cheap 4 or 20Mhz stock xtal (60-80 pence), an electrolytic capacitor, three ceramic caps and a resistor and I doubt if the all-up price would exceed a fiver.  It's entirely possible to build a working PIC computer with this few parts.  You may want it to flash an LED... (add a few pence more), or play musical notes(£1 for a peizo speaker), display a scrolling message on an 16*2 character LCD panel (maybe  few quid extra), or measure the temperature and send it over an RS232 link.  In simple applications, why even bother with a PCB?  Just program and test your system then fold the pins out horizontal and solder the connections together in 'spaghetti' fashion.  Wont look pretty but it will almost certainly work OK.

I've just checked EBay and discovered one vendor selling these programmers for £2 each (Fixed price...Buy Now!). Postage will be around £7:50 from Hong Hong but hey, a device programmer for a tenner sounds good to me.  Here's a photo of the one I use.  It cost about the same amount, and yes you DO get a 40PIN ZIF socket as part of the deal.  The D connector on the programmer can be simply plugged in to the serial port on your PC. Alternatively a 9 pin male to female extension cable may be used.

I checked the price of these superb ZIF sockets in the UK. I'd be embarassed to say how much they cost here, let alone one that comes with a programmer 'thrown in'!  Using a ZIF socket ensures that the pins of your chip don't get damaged by all that handling during program development.  You'll notice the row of pins facing you on the picture above. These allow a connection to be made between the programmer and the development circuit in order that the chip may be programmed in-situ.  What a great idea.  It's called ICSP (in-circuit-serial-programming) and is a feature of the PIC Flash family.

I hope this short article has whetted your appetite and that you think about trying out PIC microcontrollers for yourself.  Weather stations, automated rotators, remote antenna switches, beacon keyers, tone generators, the control of PLL's or DDS's, 'intelligence' for your next radio project, digital frequency meter.  All of these things are possible with a PIC. Just a few ideas that could be implemented with a £5 computer.  Remember you can download and try out any of the compilers mentioned above for free. If you need to do larger applications, the cost of the license is not exhorbitant. Mine is a licensed copy. I don't at all mind paying for good things!

In many cases you would want to use a serial communications link between the PIC and a host computer for data transfer or whatever. This is most easily done by using a Maxim MAX232 IC as a level translator to change the PIC's TTL levels to the RS-232 voltages.  See how at http://sodoityourself.com/max232-serial-level-converter/

A number of companies in the UK produce excellent prototyping PCB's for various models of PIC CPU.  I've used some of them including the ones sold by Taylec (below), but often, an old bit of tripad stripboard is more than adequate.  Just remember to use a socket for the PIC!

The Dutch 'One Man Company' http://www.voti.nl/dwarf/index.html run by Wouter van Ooijen, does some very interesting PIC products including what he calls 'Dwarf Boards". This is a means of adding peripheral devices  like keyboards, LED's LCD's and other devices to a main board carrying a PIC micro-controller.  Ideal for those who like to keep things neat.

For those who like to know such things...my only connection with MIKROELEKTRONIKA is as a happy customer. Of course we don't actually need to say this sort of stuff in the UK, but I thought I'd point it out anyway.  I really enjoy using their excellent compiler.

Oh, and don't do doing what I did earlier today and feed 12V DC into a PIC. They REALLY don't like it.  These babies are meant to run on 5V DC.

Some useful links

http://www.mikroe.com/forum/

http://www.mikroe.com

http://www.voti.nl

http://www.mecanique.co.uk/index.html

http://www.mecanique.co.uk/products/proto-boards/board40.html

http://www.taylec.co.uk/acatalog/index.html

http://members.home.nl/b.vandam/lonely/pagina000.html

http://www.fored.co.uk/html/devboard.HTM

http://www.nomad.ee/PIC/

http://www.microchip.com

PIC16F87XA Datasheet

http://fiacopetti.it/pic16f877db12_en.htm

73  AL

GM1SXX  AMSAT-UK #3286.