Close

Divide by 360

A project log for Anti-Papparazzi Hat

A wearable optical surveillance detector

tomas-holmqvistTomas Holmqvist 08/20/2014 at 23:290 Comments

This will document some decisions on the divide by 360 function in the mcu.

I wanted the reaction time to a full 360 count to be as small ass possible, to minimize the jitter to the PLL comparator that will introduce oscillation in the feedback loop.

As the value 360 is greater than 8-bit max (255) an 8-bit timer would need some software bit extension to make it a needed 9-bit, with some cumbersome reloading of counters. Also the Capture/Compare/PWM module is connected to timer 1 and that looked from the onset as a function that would be useful.

That is why I choose to implement the counter on timer 1, as it is 16-bit wide. It is configured in external synchronous clock mode. I would like to use asynchronous mode (again to reduce jitter) but it is not recommended for use with the compare module.

To count 360 pulses the Capture/Compare/PWM module is set in compare mode, with an interrupt generated when timer 1 is equal to 360. The hardware is responsible for resetting timer 1 to 0.

An alternative would be to preload the counter with 35535-360 and use the counter overflow, then the CCP-module could be set to capture mode upon receiving a detection, but I couldn't get it to work that way. It would also mean that timer 1 would have to be reloaded on every overflow and perhaps the value trimmed to account for the delay of reloading the timer.

To further minimize jitter the compare interrupt is the only interrupt. Serial communication is bit-banged in a bussy-wait loop. The "interrupt-on-change" function is used as a SR-latch on the detector input but isn't allowed to actually interrupt the processor.

Discussions