Nano with 19MHz clock, possible?

Hello there,

[Edit: Note the frequency may be 9.5MHz instead of 19MHz]

I have a Nano i have been using today for some tests on i/o pulse timing. I was looking for specific timing so i decided to do a few measurements.

What was surprising, to say the least, was that two different tests i had performed seem to show that the crystal or whatever is used on this board to generate the clock frequency for the CPU must be around 19MHz not 16MHz. Is this even possible?

The first measurement was with a timing program that outputs a 250kHz square wave pulse. i have read other posts on this site that specify the timing when using the PORTB= instruction and PORTB|=B00100000 and the like, and have inserted several of these instructions that create a high and low signal which when measured on digital pin 13 measures really close to 250kHz. In fact, on two different meters it reads 250.0kHz. On the CRT scope (old guy) it measures close to 250kHz but the time axis is not that accurate so i trust the meter readings more in this case, although the scope should not be off by more than about 10 percent, plus or minus.

The second measurement was using a short wave radio which quiets very noticeably when the radio is tuned to exactly 19.005MHz and stays a little quiet at 19.010MHz, but does not quiet anywhere near 16MHz as my Nano does. My Nano was quiet at 15.800MHz or something like that, which is understandable, but 19MHz seems way too far off.

Could it be that the manufacturer (probably Chinese) put in the wrong crystal or resonator?

Also, would the serial communications still work even if the crystal was the wrong frequency?

Thanks.

If the clock were really 19 MHz, the bootloader and serial comms would not be working (unless custom software was already loaded on the chip).

You can use any crystal or resonator frequency you like, as long as the frequency is within specifications (see the processor data sheet) for the operating voltage.

The compiler has to be notified of the actual clock frequency for the boot loader, any other timing operation and serial communications to work properly.

Very bad problem specification.

The first measurement was with a timing program that outputs a 250kHz square wave pulse

A program that you declined to share with us, and only told us what it did, not what it was supposed to do. There is nothing in your post explaining your chain of reasoning from "250 kHz" to "19 MHz".

And I have no idea what you're talking about with the short-range radio. Trying to measure the EMI?

MrAl: Hello there,

[Edit: Note the frequency may be 9.5MHz instead of 19MHz]

I have a Nano i have been using today for some tests on i/o pulse timing. I was looking for specific timing so i decided to do a few measurements.

What was surprising, to say the least, was that two different tests i had performed seem to show that the crystal or whatever is used on this board to generate the clock frequency for the CPU must be around 19MHz not 16MHz. Is this even possible?

The first measurement was with a timing program that outputs a 250kHz square wave pulse. i have read other posts on this site that specify the timing when using the PORTB= instruction and PORTB|=B00100000 and the like, and have inserted several of these instructions that create a high and low signal which when measured on digital pin 13 measures really close to 250kHz. In fact, on two different meters it reads 250.0kHz. On the CRT scope (old guy) it measures close to 250kHz but the time axis is not that accurate so i trust the meter readings more in this case, although the scope should not be off by more than about 10 percent, plus or minus.

The second measurement was using a short wave radio which quiets very noticeably when the radio is tuned to exactly 19.005MHz and stays a little quiet at 19.010MHz, but does not quiet anywhere near 16MHz as my Nano does. My Nano was quiet at 15.800MHz or something like that, which is understandable, but 19MHz seems way too far off.

Could it be that the manufacturer (probably Chinese) put in the wrong crystal or resonator?

Also, would the serial communications still work even if the crystal was the wrong frequency?

Thanks.

An Arduino board from the "factory" (whether it be genuine Arduino or cheapo-clone) will have a 16 mhz. resonator for the clock. A few are sold with a 20 mhz resonator or crystal, but the majority are 16.

Trying to measure the crystal frequency with an oscilloscope may or may not work, depending on the probe's input impedance and capacitance. My particular scope shows a nice, clear sine wave with a slight "hump" in the positive going part, but the scope says "16.000" for the frequency (or 22.1184 for my "custom" boards).

I can't imagine that anyone put a 19-something oscillator on your Arduino. As far as using a shortwave radio to try to hear the oscillator as a radio signal, that will certainly work... and usually you will hear a really strong "carrier" at 16.0 and some spurs at predictable places above and below.

Where "19" comes from is beyond me.

Another way to test it is to setup a timer in "CTC mode, Toggle Pin" and set the divider for a known frequency and then use the silly-scope to see if it's right. Since it seems like you already did that and came out with 250 khz (which is I suspect what you expected to see), I would trust that reading more than the scope which may have been triggering poorly and getting a wrong reading.

As far as frequencies you can use... anything you want from the khz range up to the chip's rated speed. Atmel makes 20 mhz parts, and these (heck even the 16 mhz parts) run fine at 22.1184, 27.125, etc... (I've run them up to 30+ mhz and they worked fine). They work much better if you set the oscillator fuse bits to "full swing" crystal as opposed to the standard "low power" crystal.

Have fun.

jremington: If the clock were really 19 MHz, the bootloader and serial comms would not be working (unless custom software was already loaded on the chip).

You can use any crystal or resonator frequency you like, as long as the frequency is within specifications (see the processor data sheet) for the operating voltage.

The compiler has to be notified of the actual clock frequency for the boot loader, any other timing operation and serial communications to work properly.

Hi,

That's what i was thinking, but the two measurements seem to show the same thing so i am not sure what is actually true yet.

Jiggy-Ninja: Very bad problem specification.A program that you declined to share with us, and only told us what it did, not what it was supposed to do. There is nothing in your post explaining your chain of reasoning from "250 kHz" to "19 MHz".

And I have no idea what you're talking about with the short-range radio. Trying to measure the EMI?

Hi there,

Yes, that's true, i was hoping someone might know about this issue a priori but i will post my code in a post that follows, thanks.

Krupski: An Arduino board from the "factory" (whether it be genuine Arduino or cheapo-clone) will have a 16 mhz. resonator for the clock. A few are sold with a 20 mhz resonator or crystal, but the majority are 16.

Trying to measure the crystal frequency with an oscilloscope may or may not work, depending on the probe's input impedance and capacitance. My particular scope shows a nice, clear sine wave with a slight "hump" in the positive going part, but the scope says "16.000" for the frequency (or 22.1184 for my "custom" boards).

I can't imagine that anyone put a 19-something oscillator on your Arduino. As far as using a shortwave radio to try to hear the oscillator as a radio signal, that will certainly work... and usually you will hear a really strong "carrier" at 16.0 and some spurs at predictable places above and below.

Where "19" comes from is beyond me.

Another way to test it is to setup a timer in "CTC mode, Toggle Pin" and set the divider for a known frequency and then use the silly-scope to see if it's right. Since it seems like you already did that and came out with 250 khz (which is I suspect what you expected to see), I would trust that reading more than the scope which may have been triggering poorly and getting a wrong reading.

As far as frequencies you can use... anything you want from the khz range up to the chip's rated speed. Atmel makes 20 mhz parts, and these (heck even the 16 mhz parts) run fine at 22.1184, 27.125, etc... (I've run them up to 30+ mhz and they worked fine). They work much better if you set the oscillator fuse bits to "full swing" crystal as opposed to the standard "low power" crystal.

Have fun.

Hi,

Yes i tired measuring with a meter but it wont respond to the lower level signal i guess even though it is a 20MHz meter, and the scope is old and only goes up to 10MHz. I turned to the short wave radio and that quiets at 19MHz but it could be 9.5MHz because it quiets there too.

The real thing i dont know is how Arduino translates a few different instructions into machine code, or machine cycles, so i can accurately calculate the timing. I need to know how many cycles a 'nop' takes and so on, and there could have been a mistake in the code i read on another site, but i will post the code next and explain a little more.

Thanks for the informative reply.

the two measurements seem to show the same thing so i am not sure what is actually true yet.

The measurements are either wrong, or you are misinterpreting the results.

Hi,

Here is the entire code. Note that the "//2" shown in the code was to indicate that the instruction takes 2 cycles, but that may not be true (found on another site) because when i replace one of those with 'nop' instructions it takes 4 nop's to replace one of those instructions to get the same 250.0kHz output on digital pin 13.

If someone knows how many cycles these instructions take that would help i think, and i guess 1 cycle means 62.5ns at 16MHz?

I would try this on my Uno but i have that set up for something that is running right now and i dont want to have to stop it, disconnect stuff, and upload new code, etc.

/*
  Test Arduino timings 01/02/2017
*/

// Pin 13 has an LED connected with series resistor (on board)
int led = 13;
byte PB=0;
byte w=0;



void setup()
{                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
  DDRB=DDRB|B00100000; //direction reg dig pin 13, set manually
}



void loop() 
{
  delay(5000); //not needed anymore, will be removed

  cli();

  // 250.0kHz as measured, roughly 50 percent duty cycle
  
  Repeat: //the goto to this label must take 8 clocks?
  {



    PB=PORTB|B00100000; //2
    PB=PORTB|B00100000; //2
    PB=PORTB|B00100000; //2
    ////PB=PORTB|B00100000; //2  (remmed out to compare with asm 'nop'

    __asm__("nop\n\t"); //total of 15 of these
    __asm__("nop\n\t"); 
    __asm__("nop\n\t"); 
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");
    __asm__("nop\n\t");
    __asm__("nop\n\t");
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");     
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");     
    


    PORTB|=B00100000;   //2 [16] 'ON'

    PB=PORTB&B11011111; //2
    PB=PORTB&B11011111; //2
    PB=PORTB&B11011111; //2
    PB=PORTB&B11011111; //2

    __asm__("nop\n\t");  //total of 15 of these
    __asm__("nop\n\t"); 
    __asm__("nop\n\t");  
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");   
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");  
    __asm__("nop\n\t");
    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");    
    __asm__("nop\n\t");     

    PORTB&=B11011111;   //2 [16] 'OFF'
  }
  goto Repeat;
  

}

MrAl:
The real thing i dont know is how Arduino translates a few different instructions into machine code, or machine cycles, so i can accurately calculate the timing.
I need to know how many cycles a ‘nop’ takes and so on, and there could have been a mistake in the code i read on another site, but i will post the code next and explain a little more.

You can convert the generated code to assembly (I need to look up how exactly) and consult the datasheet of the microcontroller to find out how much time each instruction takes. A nop is a single machine cycle if I’m not mistaken.

nop is indeed one. Most instructions are one or two clock cycles. End of the datasheet list how many for each.

Hi,

Thanks for the additional replies. So a nop is 1, that's nice to know, so that is 62.5ns at 16MHz which is very good to know.

The main question came up because of trying to figure out the Arduino timing, as it pertained to asm code, not how asm code converts to clocks, until after the asm code is know for a given Arduino instruction. So i have to know how a given Arduino instruction turns into asm before i can look up the data sheet and count the clocks.

What my main problem was was that i was unable to turn an Arduino instruction like: PB=PORTB|B00100000;

into clock cycles, and when i looked on anther web site they stated that was 2 clocks, which would be 62.5ns times 2 so 125ns, but that led to incorrect results, and i also did not have the time for a while loop or a 'goto' label, so it was a little tricky. What i found out was that when i replaced one of the above instructions with nop's, it took 4 nop's to equal one of those, so that must have taken 4 clock cycles not 2 as found elsewhere. That made the clock frequency seem off.

Now after considering that and on another website i found that the instruction: PORTB=B00100000;

for example takes only 1 clock (double checked that with a second one, then replaced that with a nop) i think i found that the crystal really is 16MHz as originally expected because when i add nop's now i can change the frequency to lower values and get a meter reading with accuracy of 4 significant digits. That seems like proof enough for me right now, but the final test will be when i go to use this for the original application.

Only one pseudo question remains, and that is about the "goto label" instruction. When i set the time for that (in my formula) to equal 2 clock cycles, i get accurate meter readings. Thus, for now i assume it is 2 clocks, but what i dont know is if that will change as i add code because i read it might be doing a relative jump and dont know if that could change or not in the future.

So everything is looking much better, with that one last question remaining.

MrAl: So i have to know how a given Arduino instruction turns into asm before i can look up the data sheet and count the clocks.

I don't now if that is a question or not, If so, the command is in reply #55 in your mod() topic ;)

If it was not a question because you already know this, I did not say anything ;)

I think that you are over complicating this issue.

Can't you just write a sketch that uses a standard timing function such as millis(), micros(), delay(), or delayMicroseconds(), and then check whether it runs at the right speed?

For example, does delay(60000) take 1 minute to run, or has it finished after 50 seconds, as you suppose?

Another alternative is to use only asm and then the timing is very easy to find.

The following code toggles port B5 every 8 cycles precisely. It will produce a 1MHz square wave (on B5) for a 16MHz clock.

BTW. If you’re not sure how to include an ASM (.S) file then just see the full sketch folder attached.

#include "avr/io.h"
.global main

#define iDDRB   DDRB-0x20         // Define port IO addresses
#define iPINB   PINB-0x20
main:
cli
sbi iDDRB,  05                    // Set port B5 as output
pulse:
sbi iPINB,  05                    // 2 clock cycle (Use ports toogle function)
nop                               // 4 x nop = 4 clock cycles
nop
nop
nop
rjmp        pulse                 // 2 clock cycles

// Loop timing = 8 clock cycles per toggle = 1 MHz on port B5

freqTest.zip (523 Bytes)

JohnLincoln: I think that you are over complicating this issue.

Can't you just write a sketch that uses a standard timing function such as millis(), micros(), delay(), or delayMicroseconds(), and then check whether it runs at the right speed?

For example, does delay(60000) take 1 minute to run, or has it finished after 50 seconds, as you suppose?

Hi John,

Yes i'll have to try that too, sounds like a good idea. I dont think the boot rom could alter that although i am not perfectly sure yet.

What i am sure about now though is that it really is 16MHz. The whole problem started because another site i checked gave the wrong time for some instructions. That started the whole mess. If it was not for that, i would have probably never had to ask this question.

What i have not determined yet is what caused the short wave radio silencing at 19.005MHz, which would have been super close to the estimated frequency based on the 'wrong' timings given. That's quite a coincidence. It may be that the program code was so tight that it altered the harmonics coming off the Nano board. I dont know if i will look into this too deep, but i will check it again later when i have a real program loaded. The Uno board quiets at 16MHz or very close to that.

stuart0: Another alternative is to use only asm and then the timing is very easy to find.

The following code toggles port B5 every 8 cycles precisely. It will produce a 1MHz square wave (on B5) for a 16MHz clock.

BTW. If you're not sure how to include an ASM (.S) file then just see the full sketch folder attached.

#include "avr/io.h"
.global main

define iDDRB  DDRB-0x20        // Define port IO addresses

define iPINB  PINB-0x20

main: cli sbi iDDRB,  05                    // Set port B5 as output pulse: sbi iPINB,  05                    // 2 clock cycle (Use ports toogle function) nop                              // 4 x nop = 4 clock cycles nop nop nop rjmp        pulse                // 2 clock cycles

// Loop timing = 8 clock cycles per toggle = 1 MHz on port B5

Thanks i will try that too. This problem seems to be solved now that i have found that the original site gave the wrong timing.

Did the Nano board also silence the radio at 16 MHz, or just 19? It could be some harmonics doing funky things to the IF mixer. Mixing isn't a clean process and produces an ton of harmonics (sum, difference, and various multiples of those) that have to be band-pass filtered out. One of the harmonics emitted by the Nano board may be creating an "image" after being ran through the mixer that shows up in the IF band when the radio is set to 19 MHz.

Jiggy-Ninja: Did the Nano board also silence the radio at 16 MHz, or just 19? It could be some harmonics doing funky things to the IF mixer. Mixing isn't a clean process and produces an ton of harmonics (sum, difference, and various multiples of those) that have to be band-pass filtered out. One of the harmonics emitted by the Nano board may be creating an "image" after being ran through the mixer that shows up in the IF band when the radio is set to 19 MHz.

Hi,

The Nano silenced at 19.005MHz (radio tunes in increments of 5kHz) and a little at 19.010MHz but not at anything near 16MHz, while the Uno silenced at 16MHz (close to that) but not at anything near 19Mhz.

Both of these have different programs running. The nano had a short program that kept repeating with an exact repetition rate which means it may be emitting a strong harmonic, but i did not look into this anymore. The Uno has always silenced at 16MHz but i always use a 'regular' program code that does all kinds of stuff that only repeats at low frequency.

It's funny too because the silencing is a very distinct silence not just a little lower, and the carrier signal strength goes up too. Also, when i turn the Nano off (by disconnecting power to it) the 19MHz carrier is no longer detected by the radio, and when i power it back up, the 19MHz carrier again silences the radio. That led me to believe that it might be emitting 19MHz and because the incorrect timing calculation led to the same frequency, i was starting to think it was a 19MHz crystal. It just happened to be a carnival of errors that's all. After checking for sub harmonics, i found them at 9.5MHz and 4.75MHz, and the lower was the strongest so that may be the true frequency being emitted by the Nano which overpowers the crystal frequency for some reason. It's just amazing that the error in timing calculation matched the detected carrier even though it may be just a harmonic, because it was so very close to the detected carrier within 0.03 percent (that's 3 hundredths of 1 percent, not 3 percent).

Don't forget that the Nano also has an FT232 chip on it that will be running at its own clock frequency. An Uno uses an ATmega16U2 as the serial converter instead of the FTDI chip. The FTDI chip (or whatever cheap clone the board is actually using) may be the source of the dirty EMI being emitted by the board.

The fact that it just so happened to match the value from your incorrect clock frequency calculation is just hilarious though. Two wrongs made you think you had it right. :grinning: