Thursday, March 15, 2012

Scalextric Car ID Sensor

Of course I also have to build a car id sensor for my digital scaley track.
On the same great Electric Images site both electrical schema and PIC assembler program are provided for you to build your own.

My goal is to have support for at least four id sensor on the track. Two at the start/goal line of course,  and one at each end of a future pit lane.

Building a test setup

I've only tested the id sensor on my breadboard so far, but I had some problems getting it to work (as usual...).
After getting the components needed and the circuit built, I ran some first tests just to see if the confidence LED blinked, but no, nothing happened... I started analysing the assembler code to find out how it works. I measured the timing of the IR signal from the car with my oscilloscope and created a stimulus profile in MPLAB to simulate the same car. The simulation showed no signs of problems, so I just tried fiddling with the span for each car id that the program accepts. Suddenly I got some flashes from the confidence LED and thought I had solve the problem.


Sending the serial data to the computer

After getting the LED to show signs of successful id sensing I connected the PIC's 7th pin to the powerbase's USART port and modified its code to send the data over the USB cable so I could verify that the correct car id is actually sensed. After some struggeling with the USART configuration I got some garbage data sent to the computer. I blamed the PIC18F for the garbagage data and got a RS232 chip from the radio schack to be able to connect the PIC directly to my computer's serial port instead. What a disappointment when I realized that more or less the same garbage showed up through the serial port too. Now I tried to find the fault in the serial sending part of the code and did some more debugging in MPLAB, just to come to the conclusion that it seemed to work. The only strange thing was some undocumented OR:ing with the hex value of 0x30 and 12 bits package size, which in itself hadn't anything with the garbage data to do.

Finding the cause of the problem

I realized that I had to start debugging at some really basic level, so I modified the program to send the hex value of 0x55 (10101010) continuously on the serial out pin. I hooked up my oscilloscope and measured the bit timing. AHA! Now I finally found something that wasn't right. The bit timing was way off. It should have been 17.6uS (@ 57600bps) but it was something in the 25uS region. Now the question was, why?!
As the PIC contains a built in clock, it had to be something wrong with it...and yes, at the program memory address 0x3FF where the factory calibration value is saved, I did only find the value of 0x00. Not good....

Fixing the clock problem

Luckily I'm not alone with the problem, and after a short trip to google I soon found sites describing the issue and how to fix it. At this page a complete circuit schema and assembler program are provided to recalibrate the built in clock. Soon I had built yet another circuit and got a new calibration value. Using the tip at this page on the same site, I marked the pins on the PIC according to the binary value of the calibration value, to (hopefully) never have to the recalibration again =)
This time the original id sensing program worked for real and the data sent over the serial line worked perfect!

 
In the video the car is programmed with id 3.


Next thing is to figure out a way to support 4 of these serial data lines on one cable, preferably the same USB cable as to the power base.