Timers inside pin interrupts

I see that various delay and other counting functions won't work within an interrupt

But can I start a timer within an interrupt and check the timer incrementing value within a loop? Note I would not be setting the timer up as an interrupt. There are also no other interrupts to deal with.

What I am trying to do is measure pulse width of incoming Manchester encoded serial data.

The interrupt is a trigger where the data should be time wise. The encoding data clock is also available so that I can detect polarity reversals mid clock period.

Normally you would extract the clock from the Manchester encoded data stream. But I am lucky here to have the actual clock source available that encoded the data. This ensures very low jitter in the decoding process.

You could save time stamp (millis, micros) to variable within interrupt and check that in loop.

Are you referring to a hardware timer, or one you implement in you program?

Well the clock is 511,363.6khz.
Each data cell or clock cycle is 1.9us wide.
The target hardware is a Uno R3 at 16mhz.

Data is encoded as polarity changes within a clock period.

So on each clock edge I want to start a timer that runs for 1.75us while monitoring the data pin for a polarity change which can happen at 1.5us or not. This is how we read ones and zeros.

This would be happening inside the interrupt in an outer loop loop for 24 clock periods. Meaning the interrupt is held on for the entire data window and the recovered data (3 bytes) is placed in a buffer array.

The system is fully synchronous.

We know:

  1. Where the data starts.
  2. We have a hardware recovered clock signal which encoded the data.
  3. We know there will be 28 clock periods within the data window, 24 of which are the data cells.
  4. We also know when the data stream window closes.

These various control signals are provided from an FPGA that counts down from device master clock of 14,318,180mhz.

So this allows for a much simpler and accurate software Manchester decoder versus dealing with an asynchronous Manchester data stream.

I want to start one of the hardware timers within the interrupt and poll the count. Note that ALL interrupts will be disabled for the duration of the ISR function.

What board / processor? You should look into Input Capture mode.

Uno R3. Actually the final hardware will the an ATmega328 just programmed with the HEX file. But I could use another processor if needed.

unless you are planning on doing all this in ASM, the Uno won't be fast enough. It takes longer than that just to read a pin using digitalRead() and if you are using micros() for timing, it only has a 4us resolution on the Uno.

If you look at the Teensy line of Arduino boards, they run at 96MHz and may be a better choice.

1 Like

or an ESP32 possibly using the Remote Control Transceiver (RMT)

Hmm, too slow? I do know ASM from my Apple II days but have not really played with it since the mid 1980s on 4mhz Z80 processors.

Or just do it with hardware inside the FPGA which is already there to provide the timing pulses. No big deal to have 3 bytes worth of registers in hardware that can be read out via SPI or even a parallel transfer if enough pins on both ends.

I should mention that while this data capture must happen within 63us, the data does not appear again for 33ms.

For those who are analog video savy these numbers should be familiar. 63.5us is the length of a single scan line in old analog television. And 33ms is the length between full frames. I am trying to read some custom data encoded on lines 16-17 from some vintage broadcast quality video tapes. Similar to closed caption information on line 21 but using different data encoding.

1 Like

Teensy 4.1 wouod be the way to go. It runs at 600mhz, overclockable to 1ghz

You can either use digitalreadfast, or do a direct register read to read an input pin (couple of clock cycles only)

You can also directly read the uint32_t arm clock cycle counter (1 clock cycle for a read - it overflows every 17 seconds) for a fast accurate timer

It does run on 3.3v however.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.