Back in the bad old days every phone company had a proprietary interface and often different interfaces for different phones in their product line. These days, however, almost every phone uses a USB interface, as do many other devices. Unfortunately, implementing a USB interface is not as simple as it is to implement a standard serial interface. Some of the newer PIC chips have USB support built in but they still require a software library to fully implement an interface. A simple solution, that allows us to use the serial interface built into most PIC chips, is to use a cheap USB to TTL converter module.
USB Converter Module
The pictures shown here are typical of the USB to TTL modules available. This particular one uses the CP2102 chip from Silicon Labs. I bought one on ebay for less than $4 (shipped from a US supplier) and that included a 5-wire jumper cable. If you want to wait for shipping from China they can be purchased for even less. Just plug it in and Windows Update should find a driver and assign a COM port.
To do a basic test of the module you can short the TXD pin to the RXD pin, plug it into a USB port, and fire up your favorite terminal program. I used Tera Term and set it up as shown in the screen shots above. The module will just wrap around whatever text you type.
Even though we don’t need a PIC to test the USB module, the diagram above and the supporting software are provided as a learning exercise and building block. As you can see, the USB transmit output (TXD) goes to the receive (RX) input of the PIC and the USB receive input (RXD) goes to the transmit (TX) output of the PIC. In effect the PIC is acting like the short circuit between the TXD and the RXD terminals we used in the previous step. This is the same thing we did with a Bluetooth module in Episode 4.
The software link is listed below. While it is targeted for the 16F688, it is easily ported to other versions of the PIC. Just make sure that you choose one that has the asynchronous serial port capability. You will also need to change the line that identifies the PIC version (LIST=) and the INCLUDE file but those are intuitive changes. The __CONFIG line may also need tweaking just because one or two of the labels used are spelled differently in some of the INCLUDE files.
The software is really pretty simple because we just check for data input from the USB and then send it right back out to the USB. In order to get the maximum baud rate we set the internal oscillator for 8-MHz. Init_Serial configures the port for 115.2 kbaud and enables both the receiver and transmitter for asynchronous operation. Main_Loop just polls the receiver interrupt flag which gets set after receipt of a byte. If the flag is set then the data handler is called to read the byte and then check to see if the transmit buffer is available. If so, then it copies the received byte into the transmit buffer for output. In reality, the data is loaded into the transmit register and the PIC automatically loads it into the output shift register when it becomes available. The transmit interrupt flag will be set if the transmit register is empty, even if the transmit shift register is not empty. In this simple application we poll both the receive and the transmit interrupt flags instead of actually enabling them to interrupt the software. If we had a lot more stuff going on, like time intensive processing of the data, we would want to set up actual interrupt handlers. The Overflow routine is an error handler just in case we get more data bits received before we can read the register and clear the interrupt flag.
As you can see in the picture, I wired mine up on a small prototyping board just to test it out. Power for the PIC is supplied by the USB port. Again I used Tera Term but this time I had to reconfigure the serial port baud rate to 115.2 kbaud. That’s it for this post. Check out my other electronics projects.