Go Down

Topic: CPU Clock Speed issues (Read 165 times) previous topic - next topic


I want to program my Uno WiFi 2 in assembly. This is actually not as big of a hassle as with other Arduinos thanks to the included mEDBG.
I use Atmel Studio 7 and made a little timer which when triggered inverts the state of an LED. I got both Timer 0 and 1 working but it seemed like the frequency of them was off.
After investigating in the I/O view I found that the "CPU clock control prescaler" was set to 6 and enabled, even immediately after a reset. When I turned the prescaler off it seemed like my code worked like expected, creating an interupt with a frequency of 2Hz.
Is this prescaler activated by default on the board or is it even enabled by default on the ATMega8409?

Tldr: Does the Arduino Uno WiFi 2 run at 16/6 = 2.66MHz or is mine just not set up correctly?


May 12, 2019, 07:30 pm Last Edit: May 12, 2019, 07:31 pm by nikilase
Here is the code I use for Timer 0.
Note that I "redefined" VPORTE_OUT as PORTE and VPORTE_DIR as DDRE in the "m4809def.inc" file.
When started it blinks with a frequency of about 1/6Hz.
Code: [Select]
; Interrupt for normal start/reset
.ORG 0x00
rjmp anf

;Interrupt TCA Channel 0
.ORG 0x12
rjmp i_9

;Load adress on Y
ldi YL, 0x00
ldi YH, 0x0A

;store 0Fh on CTRLA for Enable and Prescaler 1024
ldi r16, 0x0F
st Y, r16

;store 10h on CTRLB for Compare 0 enable
ldi r16, 0x10
std Y+0x01, r16

;store 10h on INTCTRL to enable Interrupt Compare 0
ldi r16, 0x10
std Y+0x0A, r16

;store 2000h on CMP0
ldi r16, 0x20
std Y+0x29, r16
ldi r16, 0x00
std Y+0x28, r16

;Data direction Port E
ldi r16, 0xFF
out DDRE, r16

e: rjmp e

i_9:in r25, SREG
;invert LED state
in r16, PORTE
ldi r17, 4
eor r16, r17
out PORTE, r16

;Reset Timer and Interrupt
ldi r16, 0x08
std Y+0x05, r16
out SREG, r25


After taking a look at the blink sketch, the clock prescaler(MCLKCTRLB) is set to 6 but the enable bit is not set.
Does this mean everytime I want to program using assembler I have to reset the enable bit first? Is this standard practice and if so, why enable it at startup in the first place?


Set the datasheet wrt the clock at reset.
Unlike older chips, the 4809 always divides by 6 after reset, and you have to explicitly initialize it in code to do otherwise.


Wow that's a bit strange. Thanks for helping me out. Do you have any idea why they do this?
The 4809 really is a much more different chip compared to the older ones


It prevents the chip from being "bricked" if you set the fuses wrong.
It's pretty common to have to do "significant" clock configuration in modern microcontroller.
Take a look at the early config of a samd20...


Ok, that makes kinda sense to me. Could my arduino get bricked by setting the clock to 16MHz and setting a wrong fuse and if so, which fuse.
Sorry, I am kinda new to the ATMega4809, only programmed some ATtiny25 at University.
Is there a specific section in the datat sheet on how to set it up correctly? The one for samd20 was ok but it would be nice to see it specifically for the ATMega4809.
Also, did I set up the Interrupts correctly? I know from the ATtiny25 that there is an interrupt table/vector but when i did the interrupt table like in the ATtiny25 for the ATMega it didn't work, so I did it like it is in the code snippet, which worked.
I really appreciate your help :D


Could my arduino get bricked by setting the clock to 16MHz and setting a wrong fuse and if so, which fuse.
The ATmega328 (and earlier) Arduinos can be "bricked" by setting the fuses for an external crystal or eternal clock, when there isn't one.   You can usually fix such a chip by "injecting" a clock, but it's a step up in complexity.
did I set up the Interrupts correctly?
The vectors look OK.  There's typically confusion between byte address, word address, and vector number, so it would be nice to use a named constant defined by the .inc file.   I haven't used the Atmel assembler in a very long time, so I'm not sure whether such a thing exists.
Your ISR doesn't preserve any of the registers that it uses, even though they're also used in the main loop.  That's a bug...


Thanks, again I really appreciate your help.

Your ISR doesn't preserve any of the registers that it uses, even though they're also used in the main loop.  That's a bug...

I dont have a main loop, well it is only
Code: [Select]
e: rjmp e

Go Up