Go Down

Topic: How to set and test clock speed of Nano Every board. (Read 41 times) previous topic - next topic

maninthehighshed

Aug 01, 2020, 01:16 pm Last Edit: Aug 01, 2020, 04:03 pm by maninthehighshed
The Nano Every is advertised as being able to run at 20Mhz, yet it is set to run at 16MHz out of the box.

A Google search on how to change the speed to 20Mhz shows numerous posts, many of which are complaining that once the setting is changed to 20Mhz, timer-based functions no longer work as expected.

After a bit of experimenting and a rummage though the ATmega4809 datasheet, I have found that there are two settings that need to be changed in Boards.txt not one.

The following additions to the good old blink sketch show the speed the processor is actually running at, either 16MHz or 20MHz, and the F_CPU value, which is the value used by timers, delay and serial functions to calculate how many clock cycles are required for their task. If these two values are mismatched, then timing errors will occur, and data sent to the serial monitor will be corrupted as the bit/baud rate on the Nano will be set incorrectly.

Code: [Select]
void setup() {
  char tString[ 10 ] ;
 
  Serial.begin( 9600 ) ;
  Serial.print( "F_CPU: " ) ;
  Serial.println( ltoa( F_CPU, tString, 10 )) ;

  Serial.print( "Clock Speed, 1=16MHz, 2=20MHz: " ) ;
  Serial.println( ltoa( FUSE.OSCCFG, tString, 10 )) ;

  pinMode( LED_BUILTIN, OUTPUT );
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}



Run the sketch and time how long it takes for 10 state changes to occur to the led. If it's 10 seconds, give or take a tenth, all is well, if it's much longer or shorter than that, then the entries in Boards.txt are mismatched, and you'll get gibberish on the serial monitor.



For 16 MHz operations these lines in Boards.txt need to be set:
Code: [Select]

nona4809.build.f_cpu=16000000L
nona4809.bootloader.OSCCFG=0x01


And for 20MHz they need to read:

Code: [Select]

nona4809.build.f_cpu=20000000L
nona4809.bootloader.OSCCFG=0x02


On windows, Boards.txt is found in:

C:\Users\xxxxx\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6

Where xxxxx is your login name.

Miths


almytom

Time based functions do continue to work. PWM frequencies are 25% higher. USART baud rate range is higher as well moving the minimum rate from 1200bps to 2400 bps.

Go Up