"Correct sign is found experimentally"

Designer's notes #2 - Home - Prev - Next
Řyvind Teig,  Trondheim,  Norway (http://www.teigfam.net/oyvind/

Summertime blues: move the clock spring-ahead and fall-back - so when was half a year since?

Back at the university one of the professors stated, after having gone through a lot of mathematics and found that the sign of the final expression was wrong, that "the sign is found experimentally". How many times have I proven him right?
In one of our applications there is a sigma/delta A/D converter delivering data over a high speed serial line into a DMA machine, which fills up a DSP's memory with samples. At the same time, a pulse input signals when the sensor is at certain places. This pulse is used for accurate time-stamping of the A/D samples. It needs to measure time to less than a microsecond. The DSP has two timers. One of them delivers system tics to the runtime system, counting up the concurrent language occam's TIMER step every 1ms. The other timer is free. So we set it to increment every 100ns requiring 10.000 tics per ms. The timer is 16 bits, so it will count to 65536, we are guaranteed that it will not overflow. The interval we need to track is within half a minute, so we have to use both timers to give required range and resolution. We increment a software 

timer by 10.000 every 1ms, for final stamping. Then, we add the number of 100ns ticks passed since last 1ms timer interrupt whenever a pulse input interrupt is happening. The 1ms interrupt has a lower priority than the pulse interrupt, which means that we have to read the 1ms timer interrupt interrupt pending flag to see if there should have been a 1ms interrupt. We get a critical code segment in there, so we read the flag twice and compared. If the 1ms timer had overflowed I naturally added 10.000 to the time. It cost me days to spot this error. Yes, it was an error. When the final A/D curves were shown in the client's window, we saw that with minutes in between, the curve appeared too far to the left. One of the pulses we read is a reference pulse, so getting it wrong would position the curve wrongly. The curve was 15.5deg wrong, and I could not spot the error until I saw that this corresponded to suspicious exactly 2ms. I then started to look inside the timer/counter & DMA code, expensive code it took me weeks to program. I showed it to a colleague, 

I had subtracted in a similar situation, but at this place I added, because the test program had required no times to become negative. My colleague suggested that if you subtract 1ms instead of add 1ms, you have 2ms? I tried this, and it worked! Now comes the missing previous chapter: Sigma/delta converters have a group delay, where every sample is delayed before they enter the DMA machine. There too, I had subtracted 25 sample times to get it right, until I found out that I had to add that time instead. When this was fixed, I understood the reason for the first mentioned problem, that I had to add to keep time from becoming negative. My problem was that I hadn't figured out and done the group delay compensation when I wrote the counter/timer logic. Had I done this, I just might not have gone into any of these (de)sign/coding errors. The next time I will try to find code which does what I need, or try to have my university teacher's word more mentally present. But then, maybe he is right, that in any case, the sign must be established experimentally?


Other publications at http://www.teigfam.net/oyvind/pub/pub.html