For the paramagnetism project, I need to measure the frequency of the oscillator accurately. I did that with a 16F628 PIC last time, but I am after an easier life, and the obvious choice here is Arduino. I get serial output, a way to use I2C OLED displays rather than bit-banging a Hitachi character display 1980’s style, and all that extra wiring, what’s not to like?
The lack of consistency, that’s what. I count the number of 16MHz Arduino UNO R3 clock cycles that have elapsed with every rising edge on D2 in an interrupt service routine – the code is at the end of the post. Once I have seen 16 rising edges pass, I copy that value over, set TCNT1 to zero and start over. The first result will be garbage, after that it should be OK. Every half a second I print a copy of the last result out. This measures the period, in 1/16MHz segments. I collect 2300 results, of which the first 10 are like so
freq,cnt
4793.92, 53398
4794.01, 53399
4795.35, 53400
4794.37, 53396
4794.01, 53400
4794.01, 53400
4794.01, 53400
4794.01, 53400
4793.92, 53401
4793.83, 53402
and you can see right from the get-go that there is trouble in Paradise. These are two crystal oscillators. One is the 16MHz in the Arduino board, the other is a SEI 2.4576MHz oscillator block, which I divided by 512 using a CD4060 to get 4.8kHz

Okay, so the oscillator1 is 24 years old. Just to eliminate this going bad I ran a scope on the 1/512 output, in persistence mode

It was OK, even on many more cycles in persistence mode.
Continue reading “Arduino millis() interrupt frequency measurement blues”