Sunday, February 28, 2010

Ground control to Major Tom

Debugging microcontrollers has always been a challenge for us, mere mortals. You can of course buy an in-circuit debugger, but this will cost you a few hundred bucks. Given that most microcontrollers have a serial connection (USART) built-in, a cheaper solution is to build a cheap, simple RS232 interface to your computer. Two problems :1) modern laptops don't have an RS232 connector anymore. 2) We build robots and they move around making a cable a bit of a nuisance, so we wanted to have a wireless connection to our PC. Several options exist : Wifi, Zigbee/Xbee, FM, ... but we decided to build a bluetooth connection using a BlueSmirf module (Sparkfun). This modules has a serial interface at TTL level and can be directly interfaced with the microcontroller and emulates a virtual COM port on the PC where you can read/write to it using a terminal program.

The hardware implementation was straigtforward : connect the +5V, Ground, connect RX to TX, TX to RX and you are good to go.

The pairing was straightforward : let your PC search for new Bluetooth devices, enter the password and Windows will do the rest. All you now need is a terminal program (e.g. Hyperterminal or Putty), choose the correct virtual COM port, set the baudrate and other settings correctly. That's all. Sadly enough that leaves you with a blank screen. One more step ...

The last part is the hardest : write the code for your microcontroller. Luckily there are very good examples to be found. Like this one for the Arduino. I decided to use a Microchip PIC (18F4550) and write the code in MPLAB C18 (the student version). C18 has a library to interface with the hardware USART module, making it easy to write programs. Reading can even be done via an interrupt routine or simply by polling the input queue. An afternoon of searching, trying and coding and ... tada ... I could send messages from my PIC to PC and vice versa.

One drawback is that I need to power-on the PIC (with BlueSmirf) first, then PuTTY on the PC. At that moment the PIC can send messages to the PC. To allow the PC to send messages to the PIC I need to reset the PIC after PuTTY has started. Not too much of a problem, but something to keep in mind. Some other wireless systems are less sensitive to the order in which the programs are started.

Anyway, not bad for an afternoon of fooling around, even if I say so myself !

0 comments:

Post a Comment