# Why does my Atmega328 clock seem to be off by a factor of 2?

I have an Atmega328 chip I am trying to debug. I have a sketch that blinks an LED and echoes back things received over the RX/TX pins. I am using the "Import Arduino Sketch" feature in Atmel Studio 7. I want to use the internal 8 MHz clock.

The timer seems to be too slow by a factor of 2. I tell the LED to blink every second, but it takes two seconds. When I send text over the serial port I receive back gibberish, which is probably due to the baud rate not being the 9600 I want it to be.

I looked at the memory fuse to verify that I am using the internal clock. I also tried setting the clock prescaler to 1 in the sketch code.

Here is the section of the data sheet related to the clock frequency: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

My fuse settings seem to be OK, I disabled the 8x divisor and specified to use the 8MHz internal clock:

The clock prescaler information is on page 47, essentially it says I need to make the most significant bit a 1 to allow changes to the rest of the bits, then I change the other bits to 0 to set a prescale factor of 1. If I try setting it to a factor of 2 the time delay is made worse by a factor of 2 and has a total of 4 seconds delay when the code wants 1 second.

void setup() {
CLKPR = (1 << CLKPCE); // enable a change to CLKPR
CLKPR = 0; // set the prescale factor to 1
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
analogWrite(greenLED, 15);
delay(1000);
analogWrite(greenLED, 0);
}

void loop() {
analogWrite(blueLED, 15);
delay(1000);
analogWrite(blueLED, 0);
delay(1000);
String text;
if (Serial.available()) {
Serial.println("Echo: " + text);
}
delay(30);
}


What other settings could be affecting the internal clock?

• Just to be sure; the LED is on for 2 seconds, and off for 2 seconds? – Justme Aug 29 '19 at 18:16
• Yes, that is correct – Jalconvolvon2 Aug 29 '19 at 18:17
• What is the interface to UART? – Justme Aug 29 '19 at 18:20
• I am using amazon.com/Adapter-Serial-Converter-Development-Projects/dp/… to communicate over Tera Term, and using AVRISP mkII to program the board from Atmel Studio. Could the problem be that I am telling Atmel Studio to import an Arduino sketch, and the Arduino uses a 16 MHz crystal? I would think that programming the fuses to the internal 8 MHz would disregard the standard Arduino frequency – Jalconvolvon2 Aug 29 '19 at 18:25
• Well, that's it then, will post as an answer. – Justme Aug 29 '19 at 18:28