Arduino Mega 2560 Rev 3 default Clock Source. Is it 3 running at 1Mhz or 16Mh?

Hi All,

I have an Arduino Mega 2560 Rev 3 board. Labelled Arduino.CC A000067. I can upload my sketch to the board. ( Using Windows 10 ).

Below is my simple program.

The pin 13 when I measure the period of HIGH is 6 microsecs and the LOW is 6microsecs.

My question is why is the board so slow. If the Mega2560 Rev 3 is running at 16MHz shouldnt the HIGH and LOW be in the range of 62ns or near.

Please advise where can I find official link to read documentation about the default "Clock Source" of Mega 2560 Rev 3 board.


int led = 13; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }

// the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

}

No, because when you call the digitalWrite() function it executes a lot of code that you don't see. digitalWrite() is actually quite slow.

You were told how to check for clock accuracy in your other thread on the topic. Specifically this:

srnet: Configure the Blink sketch to flash the LED once per second.

Which is not what you've done here. The code you should try looks like this:

int led = 13;
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
}

This will blink the LED on for one second and then off for one second. The timing won't be nanosecond accurate because of the way the delay() and loop() functions.

torrd25: Please advise where can I find official link to read documentation about the default "Clock Source" of Mega 2560 Rev 3 board.

The default clock source is clearly shown and labelled in the schematic for the Arduino Mega.

Hi Henry,

Thank you for your time. Appreciate your effort in helping to clarify. I did tried the delay(1000) and delay(50) loop. Yes, both cases I can get 1sec and 50msec. Plus the fact that the board can talk to my PC and I can load my sketch to Mega board. That confirm the Mega is running at 16MHz.

My actual problem comes when I use SPI function to send out bytes of data. There is a "gap" of delay of approx 6microsecs between the bytes of data.

How can I reduce or eliminate this "delay" between bytes of data on the MOSI pin?

const int slaveSelectPin = 53;

void setup() { // set the slaveSelectPin as an output: pinMode(slaveSelectPin, OUTPUT); // initialize SPI: SPI.begin(); SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));

}

void loop() { digitalWrite(slaveSelectPin, LOW); SPI.transfer(0xAA); SPI.transfer(0x00); SPI.transfer(0xFF); digitalWrite(slaveSelectPin, HIGH); SPI.endTransaction();

}

It may be that you can’t get rid of the gap. The SPI data doesn’t start transmitting on the exact clock cycle that you call SPI.transfer()- there is code that needs to get executed, so the delay is likely unavoidable. You could try using SPI.transfer16() but I wouldn’t be surprised if you still got the delay between bytes.

Hi Henry,

You are right, even the SPI.transfer16() has that delay between bytes. I have to drop the SPI method.

Actually I am trying to use my Mega board to control WS2812b RGBwithIC. It requires me to send 3 bytes of data to determine what color on the LED.

I came across code as below:

"

PORTB = ((RGB & B10000000) && B10000000) | ExistingPort; * asm("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");//these are NOPS - these let us delay clock cycles for more precise timing* * PORTB = ExistingPort;//okay, here we know we have to be LOW regardless of the 0/1 bit state* * asm("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");//minimum LOW time for pin regardless of 0/1 bit state* " Manipulating the bits directly at PortB with this method can it allows switching time of below 1microsecs? The RGB+IC LED requires clock period of 300ns.

torrd25: Actually I am trying to use my Mega board to control WS2812b RGBwithIC.

Is there any reason that you're not using one of the already-existing libraries that does exactly what you want?

Hi Henry,

I was not aware that there is a library for it. Anyway, after your comment, I did try out FastLed library. Even though the RGB led that I am using is not in the list of chip but it matches in timing requirements to one of them.

The FastLed code works well.

Anyway, thank you very much for yr time and effort in helping me.

torrd25: Hi Henry,

I was not aware that there is a library for it. Anyway, after your comment, I did try out FastLed library. Even though the RGB led that I am using is not in the list of chip but it matches in timing requirements to one of them.

The FastLed code works well.

Anyway, thank you very much for yr time and effort in helping me.

That's good to hear, I'm glad you got it working :) As an aside, the FastLED (assuming it is the one that you got from fastled.io) does say that it works for the WS2812B. See here for a full list of supported chips.