ATMEGA 4809 PF with external crystal @ 16 MHz

Hello,

My environment is Win10 with IDE 2.2.1

I am using ATMega4809PF on a breadboard,
Bootloader is Optiboot(UART1 default pins)
Using MegaCore-X, I am aware about:

QUOTE, from MegaCore-X Readme file:
Compared to older AVR families they also have more advanced and accurate internal oscillators which can provide base frequencies of 16 and 20 MHz. These can then be divided down internally to reduce the processor speed and power consumption. This means in most applications an external clock isn't necessary anymore. You can read more about clocks and clock frequencies in the Supported clock frequencies section.
UNQUOTE

I want to measure and compare frequency stability with internal oscillator vs external crystal.

Working with 328PU the method of "sniffing" frecuency without touching crystals pins works well with my Tectronix.

The question is:

If I install an external 16 MHz crystal following the schema used in UNOWiFiREV2V4.0_sch.pdf
and selecting Clock: External 16 MHz in the IDE, will it work ?

I attach the crystal area picture from UnoWiFi.

I am interested in good frecuency accuracy, not in high speed for my project.

Thanks for all your help!

UnoWIFI_CrystalControlled

No. The 4809 doesn't support a high speed external crystal. Only a low speed crystal or an "external oscillator." see section 10 of the data sheet.

Thanks for your answer,

Some doubts:

  1. When you mention "low speed crystal", which frecuency do you think I may use ?
    And, in that case, how to configure software to do so ?

  2. In the UNO WiFi, that clearly has a 32768 crystal connected in the same simple way I use it in AT328PU, why it is feasible?
    And why not in a standalone 4809 ?

  3. Is a limitation of MegaCore-X implementation ?

  4. The IDE offers 6 esternal frequencies from 1 to 20 KHz, perhaps it refers to external clocks instead of external crystals ?

In short: may I get a simple scheme for external frecuency control of a standalone 4809PF?

I am worried by this concept in the Datasheet, Section 10:

10.3.4.1.2 32 KHz Oscillator (OSCULP32K)
The 32 KHz oscillator is optimized for Ultra Low-Power (ULP) operation. Power consumption is decreased at the cost
of decreased accuracy compared to an external crystal oscillator.

Thanks again for all the help,

Attachments: Ide screenshot, MegaCore-X clocks


32768Hz. This would normally be used to operate some times, and maybe auto-calibrate the internal clock. I don't think you'd want to operate the CPU at 32kHz, and there isn't any PLL to multiply up the 32kHz to a more reasonable rate (as you see on ARM chips, usually.)

  1. In the UNO WiFi, that clearly has a 32768 crystal connected in the same simple way I use it in AT328PU, why it is feasible?
    And why not in a standalone 4809 ?

That'd be fine, but it only runs at 32768

  1. Is a limitation of MegaCore-X implementation ?

Nope. Hardware. (I guess it could be a limitation that it doesn't support running at 32kHz. But that's pretty much "not useful" for Arduino SW.))

  1. The IDE offers 6 esternal frequencies from 1 to 20 KHz, perhaps it refers to external clocks instead of external crystals ?

Yes.

In short: may I get a simple scheme for external frecuency control of a standalone 4809PF?

Use an external clock circuit rather than just a crystal. Beware that many of those simple chip-style "crystal oscillators" are pretty power-hungry (~20-30mA) compared to the current consumption of an AVR.

Something like a ds3231 is the way to go and forget about making a clock out of the 4809 itself .
It might help to know the project and where this issue arises in case a better solution exists .
Meanwhile if you have a gps chip you could count clock pulses against the gps 1hz signal .

  • don’t forget temperature drift etc .

Hello, thanks for all your help and suggestions.

I have read many documents, and speciallly ATmega4809 – 40-pin from Microchip.

My approach is tu study and evaluate all the posibilities starting with the simplest (just one crystal and two capacitors, as I do with my propietary PCBs using AT328PU, before going to more complex ones, like DS3231 (that I know has excellent stability) or external oscillators that have active components. Both requiring additional energy, labor, space and cost.

The reasons to proceed so are:

  • Less space in PCB
  • Less cost
  • Less energy involved.

If possible, I want to still use IDE 2.2.1. I am familiar with it, and is extremely simple and straight forward.

This this ideas in mind, I would appreciate any reading or examples about how to implement, both from hardware, software and IDE points of view, an experimental board with this crystal + two capacitors.

This block diagram is from Microchip, so is for sure feasible, and uses only one crystal and two caps attanched to the Clock Generation Block:

Block Diagram
ATmega4809 – 40-pin
© 2019 Microchip Technology Inc. Datasheet Preliminary DS40002104A-page 5

Attach:

Thanks again for all your help and patience !

I don't know what else to say. A low-frequency crystal oscillator (32kHz), and a high frequency crystal oscillator require significantly different electronics. An ATmega328 has two oscillator circuits to handle either one. The 4809 has ONLY the electronics to support the low speed crystal.
See section 10.3.4.2 in the datasheet on "External Clock Sources":

image

Hello,

Thanks so much for your time, knowledge and patience.

General information:

Answer to @hammy:

My app is a small system running in my farms to control automatic cattle grazing and related data logging, following this idea:

"The 32.768 kHz clock is often used in applications that require precise timing, such as timekeeping, event scheduling, or periodic data logging."

System is running nice actually with ATMega 328 PU, but new data logging features, humidity, temperature and barometric logging require more memory. That is the reason to upgrade to 4809.
Other reasons are that a UnoWifi R2 costs 60 bucks, and 4809 plus pcb and a few components are by far cheaper, about one sixth.

Alternative 1:

I will implement a prototype with a crystal in this configuration (from Microchip datasheet)
Table 4-14:

Block graphic:

ATMega4809ClockGeneration

Question for Alternative 1:

I think MegaCore-X does not support this low frecuency operation. If I am right, what software may I use within 2.2.1 IDE to run this configuration and experiment at low frecuencies ?

Alternative 2:

To use the "External Crystal Driver" controller by X1 and 74HC04.

Circuit by MegaCore-X:

OptionalCrystalDriver

In this case I think X1 frequencies can be any of the 5 supported frecuencies mentionned in Readme.md file, paragraph "Supported Clock Frequencies" of MegaCore-X, which are: 1, 8, 12, 16 and 20 MHz.

I am right in this ?

Thanks in advance, and hace a very nice day.

Whilst MegaCoreX may not "support" running the 4809 at 32.768kHz, there's nothing to stop you using XOSC32K as a clock source by directly programming the Main Clock Control A register.

However, you would need to be aware that altering the main clock manually would affect timings of other peripherals within the chip that are derived from the main clock. The obvious ones to check would be the baud rate generator(s) for the hardware serial ports and the timers - if you make use of the delay() function in your code.

Thanks a lot Mark!

Any example about how to do this?

I never did a register programming in this environment.

I am using a bootloader that is:
Optiboot UART1 Default pins.

Thanks a lot.

I may need correcting here, but I think you can directly access any of the 4809 registers just by using their names from the datasheet. In the case of the Main Clock Control A register, the datasheet gives it the name MCLKCTRLA.

To switch to using an external 32.768kHz crystal as the clock source, you would do something like:

MCLKCTRLA = ( MCLKCTRLA & 0x80 ) | 0x02;

That preserves the value of the CLKOUT bit.

Now, down in the bowels of somewhere (!) there are the register names that the compiler knows about for each chip along with the bit names as well. If you can locate them, then it makes for better reading than simply producing hexadecimal numbers out of thin air!

Great @markd833,

This information allows me continue learning, and is very useful for other newbies like me, i suppose.

I live in my farm, north of Entre Ríos Province. New week I will travel to Buenos Aires, and get crystals of various frecuencies and other components to experiment.

Thanks for your effort, have a wonderful day!

Note 1: The CPU and most of the peripherials will run at 32kHz (not the ADC and the USARTs only if the Baudrate is obtainable).

Note 2: Even a watch with a 32768 crystal AND an attenuation circuit may run off by possibly minutes per year.

Note 3: The internal oscillator of the 4809 is pretty good (factory calibrated to 2%).

Note 4: If you want to change the clock settings yourself you’ll need the function protectedWrite(.,.) and you need to calculate register values for the Baudrate you want (if even obtainable). If you pick a clock speed from the list of MegaCoreX, the IDE will do that for you.

Advice 1 (the easy solution): Pick 1Mhz from MegaCoreX. The power consumption of the 4809 will be reduced to less than 1mA.

If you want to delve deeper, you will NEED to READ the datasheet and (amongst others) iom4809.h (try C:\Users\NAME\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\avr\iom4809.h or search for it).

Advice 2 (tricky bizz): Use the SLEEP function and Interrupts. The CPU will wake up every millisecond for standard Arduino timekeeping functions; after Return From Interrupt the CPU will always execute at least 1 instruction. Organize the loop() function to question the time and do what needs to be done and go back to SLEEP as soon as possible.

Advice 3 (if you really need more accuracy): Use a 32768 crystal and the RTC for timing.

The correct statements would be:

protectedWrite(CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_XOSC32K_gc);  // Select crystal oscillator
protectedWrite(CLKCTRL.MCLKCTRLB, 0); //  Disable prescaler

iom4809.h doesn’t support Clock Out, but you might use

protectedWrite(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 0x80); // Activate CLKOUT

Combined it would make

protectedWrite(CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_XOSC32K_gc | 0x80); // Crystal oscillator and CLKOUT

Correction: iom4809 does support CLKOUT, so it becomes

protectedWrite(CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_XOSC32K_gc | CLKCTRL_CLKOUT_bm); // Crystal oscillator and CLKOUT

Thanks again to everyone for all this great work and documentation!

I already bought the parts today and I will receive them next week.

Kind regards!

Another corrrection: it's not protectedWrite(), it's

_PROTECTED_WRITE(reg, value)

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