Hi,
I am writing a program which will be uploaded to an atmega4809 microcontroller. The environment the device will be operating in could be of high temperature and I need to switch to an external crystal oscillator.
A 32.768 kHz Crystal Oscillator is already connected to the pins 34 and 35 (PF0, TOSC1 and PF1, TOSC2) and it is the one I want to use instead of the internal clock on the microcontroller.
Background:
I am using visual studio micro to create the Program.ino.hex file. Then I program the microcontroller from a Raspberry Pi4 8GB via UPDI with the pymcuprog library in python.
In visual micro the settings is set to:
- Board: ATmega4809 (MegaCoreX-master_4809)
- Option1: Clock Internal 20MHz
The datasheet for atmega4809: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega4808-4809-Data-Sheet-DS40002173A.pdf
I have never done this before and my attempts after reading everything I can find on the internet (including the datasheet) have failed. At first I tried to switch to the external crystal oscillator during startup. The code I used is the following and I call it from startup():
void activateExternalClk() { // Enable the external clock on TOSC1/TOSC2 _PROTECTED_WRITE(CLKCTRL.XOSC32KCTRLA, CLKCTRL.XOSC32KCTRLA | 0x01); // I also tried to activate RUNSTDBY before switching to the crystal clock _PROTECTED_WRITE(CLKCTRL.XOSC32KCTRLA, CLKCTRL.XOSC32KCTRLA | (1 << 1)); _PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | CLKCTRL_CLKSEL_XOSC32K_gc); // Here I have tried different registers to read to controll if // the crystal clock is stable, but nothing I tried have worked. // For example: while (~(CLKCTRL.MCLKSTATUS & (1 << 6))) { ; } }
After several attempts I tried to set the FUSE-bits. I can only manage to set the fusebit-settings for the internal clock. I have tried to choose "Option1: External clock 20MHz" and connected 20MHz to the EXT_CLK pin ( and can't even switch to the external clock at the EXTCLK pin PA0.
Is it possible to switch to the crystal ocillator in the software or do I have a problem when I am writing the program in arduino code? When I read the datasheet as it should be possible, but I am unsure how.
If you know a way to do this, I would really appreciate your reply!
Thanks in advance.