- Published: Friday, 04 June 2010
- Written by Jon Chandler
- Hits: 8869
I recently purchased a couple of the Parallax RFID readers (#28140) that were on sale at Radio Shack. These are similar to the one Graham used to develop his code, so I wanted to see if the code would work. I'm happy to report that the code does work for this RFID reader with no changes, but some bumps along the way provide some useful troubleshooting tips.
- Parallax RFID reader and 2 RFID tags
- TAP-28 board with PIC18F242 and 20 MHz crystal
- (2) PICkit 2, 1 for programming via ICSP and 1 used as UART tool (a single PICkit 2 could be used for both)
The Parallax RFID reader has a serial output at TTL level with a fixed speed of 2400 baud. The RFID reader was connected to power, ground and its output was connected to the TAP-28 UART input. Additions, the RFID /OE (output enable) was connected to ground on the TAP-28 board.
Note the slash one the /OE designation. The slash indicates the the output is enabled when the line is low, or connected to ground. If the designation was just OE without the slash, that would mean that the output is enabled when the line is connected to VCC. On some components, the output may be enabled if the OE line is left floating - check the documentation to be sure.
The second PICkit 2 was connected to ground and to TxD on the TAP-28 board so that the output could be monitored on the PC.
I happened to check this connection when I was setting it up. It's easy to confuse TxD and RxD (transmit and receive) pins on the UART. I knew the code already in the TAP-28 put out a message at 9600 baud, so I toggled to power and observed the UART tool screen to verify the connections were correctly made. The expected message appeared so I knew this was ok.
I copied the RFID module code from the Digital-DIY web page into Swordfish and compiled the module. Then I copied the RFID reader test code and made several changes to conform to the TAP-28 and RFID reader. These changes included the DEVICE and CLOCK statements, and I added a line to set the baudrate to 2400 baud as required by the RFID reader.
I added one more line to the code - a USART print statement at the beginning of the code loop to see that everything was working. I expected I'd need some changes to the RFID start and end codes, so I didn't expect I'd be able to read a code on the first try.
I compiled the program and loaded it into the TAP-28. Then I set the UART tool for 2400 baud and toggled the power. Instead of the expected "Here we go" message on the UART tool screen, all I saw was a question mark. Hmmmm. There's no place in the code that prints a question mark. UART garbage usually means a mismatch in baudrates. 2400 baud in the program. 2400 baud in the UART tool. Crap.
As a quick check, I set the baudrate to 9600 in the program, re-compiled and re-loaded it. Set the UART tool to 9600 baud and up popped the "Here we go" message. That means all the connections are ok. Somehow, Swordfish doesn't like 2400 baud. I tried 4800 baud and it didn't like that either. Crap.
Guess what time it is? Yes. Time to check the documentation!
From the Swordfish help button, I found the following:
Enables the High Speed Baud Rate select bit. Enabling BRGH can reduce the baudrate error for serial communications, even for low baudrates. However, if a slow baudrate is selected when using a high clock frequency you may need to disable this option. You should refer to the MCU datasheet for more information regarding BRGH. For USART2, the option is USART2_BRGH. By default, this option is set to true.
Links in code to support 16 bit baud rate generator. You should read the device datasheet to ensure that BRGH is supported. For USART2, the option is USART2_BRGH16. By default, this option is set to false.
I pasted these option statements into the code before the include statement and recompiled. Well, attempted to recompile but got errors. The Swordfish documentation is less than clear in the exact format of statements.
Ok, disable the first option.... by default it's set to true. So I'll bet the format should be something like:
#option USART_BRGH = false
and the second should be true....
#option USART_BRGH16 = true
Re-compiled, re-loaded and....and....success!
When I toggled the power, "Here we go" appeared on the UART tool screen!
Now to scan an RFID card. Waved the card in front of the reader, and up popped the ID. Perfect. No changes were needed to the RFID code at all.
The code listing and a picture of the UART tool screen are shown below.
The initial test after copying the code and making the required changes to conform to the hardware didn't result in success. By adding a statement to verify UART communications independently of RFIDreader operation, it was possible to narrow down the problem quickly. Once the problem was identified as a baud rate issue through some simple tests, consulting the Swordfish documentation allowed a rapid resolution. A little trial and error was required to get the syntax right, but looking at other applications with option statements, the solution became clear.
If I hadn't placed the USART print statement at the start of the program loop, it would have been all-to-easy to jump to the conclusion that the problem was with the RFID reader. When troubleshooting, check the easy things first.