MCUdude / MightyCore

Arduino hardware package for ATmega1284, ATmega644, ATmega324, ATmega324PB, ATmega164, ATmega32, ATmega16 and ATmega8535
Other
637 stars 181 forks source link

10MHz external clock support #271

Open rtek1000 opened 1 year ago

rtek1000 commented 1 year ago

Hi,

Is it possible to add support for 10MHz external clock?

I am in need of a DS1642 programmer, and because it is a parallel memory with RTC, I need an MCU with more pins, so I remembered that I have an Atmega16 stored here.

Since the DS1642 has an RTC clock calibration adjustment, I thought I'd use a high-precision frequency counter to be more sure.

I found an example of a frequency meter that uses a 10MHz crystal, it can be seen here: https://www.instructables.com/High-Resolution-Frequency-Counter/

The crystal oscillator is a TXCO for HackRF One, which I found on Aliexpress (10Mhz hackrf metal, 3v3): https://www.aliexpress.com/item/32808097349.html https://f1atb.fr/index.php/2020/05/26/tcxo-installation-on-hackrf/

And the access library in RAM memory is this: https://github.com/Andy4495/ParallelEEPROM

P.S.:

The DS1642 does not provide calibration access, but the M48T02/M48T12, which is compatible, shows how to do it.

CLOCK ACCURACY The DS1642 is guaranteed to keep time accuracy to within +/-1 minute per month at 25°C. Dallas Semiconductor calibrates the clock at the factory by using special calibration nonvolatile-tuning elements. The DS1642 does not require additional calibration and temperature deviations will have a negligible effect in most applications. For this reason, methods of field clock calibration are not available and not necessary.

https://www.analog.com/media/en/technical-documentation/data-sheets/ds1642.pdf

Byte 7F8h is the clock control register. This byte controls user access to the clock information and also stores the clock calibration setting

Calibrating the clock The M48T02/12 is driven by a quartz-controlled oscillator with a nominal frequency of 32,768 Hz. A typical M48T02/12 is accurate within 1 minute per month at 25°C without calibration. The devices are tested not to exceed ± 35 ppm (parts per million) oscillator frequency error at 25°C, which equates to about ±1.53 minutes per month. The oscillation rate of any crystal changes with temperature. Figure 8 on page 15 shows the frequency error that can be expected at various temperatures. Most clock chips compensate for crystal frequency and temperature shift error with cumbersome “trim” capacitors. The M48T02/12 design, however, employs periodic counter correction. The calibration circuit adds or subtracts counts from the oscillator divider circuit at the divide by 256 stage, as shown in Figure 9 on page 15. The number of times pulses are blanked (subtracted, negative calibration) or split (added, positive calibration) depends upon the value loaded into the five-bit calibration byte found in the control register. Adding counts speeds the clock up, subtracting counts slows the clock down. The calibration byte occupies the five lower order bits in the control register. This byte can be set to represent any value between 0 and 31 in binary form. The sixth bit is the sign bit;

'1' indicates positive calibration, '0' indicates negative calibration. Calibration occurs within a 64 minute cycle. The first 62 minutes in the cycle may, once per minute, have one second either shortened by 128 or lengthened by 256 oscillator cycles. If a binary '1' is loaded into the register, only the first 2 minutes in the 64 minute cycle will be modified; if a binary 6 is loaded, the first 12 will be affected, and so on. Therefore, each calibration step has the effect of adding 512 or subtracting 256 oscillator cycles for every 125,829,120 actual oscillator cycles; that is +4.068 or –2.034 ppm of adjustment per calibration step in the calibration register. Assuming that the oscillator is in fact running at exactly 32,768 Hz, each of the 31 increments in the calibration byte would represent +10.7 or –5.35 seconds per month which corresponds to a total range of +5.5 or –2.75 minutes per month. Two methods are available for ascertaining how much calibration a given M48T02/12 may require. The first involves simply setting the clock, letting it run for a month and comparing it to a known accurate reference (like WWV broadcasts). While that may seem crude, it allows the designer to give the end user the ability to calibrate his clock as his environment may require, even after the final product is packaged in a non-user serviceable enclosure. All the designer has to do is provide a simple utility that accesses the calibration byte. The second approach is better suited to a manufacturing environment, and involves the use of some test equipment. When the Frequency Test (FT) bit, the seventh-most significant bit in the day register, is set to a '1,' and the oscillator is running at 32,768 Hz, the LSB (DQ0) of the seconds register will toggle at 512 Hz. Any deviation from 512 Hz indicates the degree and direction of oscillator frequency shift at the test temperature. For example, a reading of 512.01024 Hz would indicate a +20 ppm oscillator frequency error, requiring –10 (WR001010) to be loaded into the calibration byte for correction.

Note: Setting or changing the calibration byte does not affect the frequency test output frequency. The device must be selected and addresses must be stable at address 7F9 when reading the 512 Hz on DQ0. The FT bit must be set using the same method used to set the clock: using the WRITE bit. The LSB of the seconds register is monitored by holding the M48T02/12 in an extended READ of the seconds register, but without having the READ bit set. The FT bit MUST be reset to '0' for normal clock operations to resume.

Note: It is not necessary to set the WRITE bit when setting or resetting the frequency test bit (FT) or the stop bit (ST). For more information on calibration, see the application note AN924, “TIMEKEEPER ® calibration.”

https://www.st.com/resource/en/datasheet/m48t02.pdf

mcuee commented 1 year ago

You can try urboot here: https://github.com/stefanrueger/urboot.hex/tree/main/mcus/atmega16a/watchdog_1_s/external_oscillator_x/10m000000_hz