0

In my project I need highest avaiable UART baudrates between Atmega328p and BTM-222 so I have to use 18.432.000 Mhz crystal to achieve 460.8 kbps transfer without erros.

But same time I need high accuracy time measurement. Previously when I was using 16Mhz crystal I was using this method: Timer0 with 64/ prescaler. So it took 4us to incrament counter register.

(1/16000000)*64 = 4us

4us was my measure resolution Overflow counting: counter overflow occurs every 1024us

4us * 256 = 1024us

Then to receive time scaled in us there is equation:

time(uint32) = current_counter * 4 + overvlow_counter * 1024

Everything is easy because multiplying x4 and x1024 are bit shifts. I am using long integer so longest time i can measure is

2^32 * 1ms = 71 minutes.

Excelent!

Then when I use 18432000 MHz crystal there is (1/18432000)*64 = 3,47(2) so it cant be calculated using my simply previous code.

Maybe you have some ideas what can I do? I have one: use 1MHz timer0 external clock source coming from crystal. My question is: how to connect 1MHz crystal to T0 pin and provide clock source for timer0?

Pierwiastek
  • 134
  • 9

1 Answers1

0

There are a few terms here that you seem to have confused: Accuracy, Precision, and Convenience.

Your question asks how to make your code convenient, allowing simple bit-shifts.

But you sacrifice precision by using the bit-shifts to make the intervals larger.

If you really want accuracy as you say in the title to your question, then you should use the most accurate crystal, which looks to be the 18.432 MHz, and do the arithmetic in your code. If you want more precision, then use more variables to hold the timing results. If you want accuracy, then set up the clock to count to 18432 before increment a millisecond counter.

The worst solution is to hook up and additional crystal. It costs more and is less precise and accurate.

UncleO
  • 8,299
  • 21
  • 29