Skip to content

Incorrect timings on ATmega/ATtiny with internal clocks #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
carlosefr opened this issue Sep 2, 2020 · 1 comment
Closed

Incorrect timings on ATmega/ATtiny with internal clocks #360

carlosefr opened this issue Sep 2, 2020 · 1 comment

Comments

@carlosefr
Copy link

carlosefr commented Sep 2, 2020

The delay, micros and millis functions don't work properly when using the internal clocks on ATmega and ATtiny:

  • On an ATmega 168/328p running at 8 MHz, the sketch below blinks the LED once a second as expected, but running at 1 MHz it blinks the LED faster (around 8 times a second);
  • On an ATtiny 85 running at 8 MHz, the sketch below blinks the LED slower (around once every 8 seconds), but running at 1 MHz blinks the LED about once a second as expected.

I'm using the following configurations, which used to work correctly at least for 1, 8, and 16 MHz (external and internal clocks) the last time I checked (which was a while ago):

On top of this, the delayMicroseconds doesn't seem to work for any clock speed. But that's the case for a regular Arduino running at 16 MHz, so I suspect either this is another issue entirely or I'm not doing something right.


void myDelay(unsigned long ms) {
  unsigned long end_ms = micros() + (ms * 1000);
  
  while (1) {
    if (micros() >= end_ms) {
      break;
    }
  }
}

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED, LOW);
  myDelay(1000);
}
@carlosefr
Copy link
Author

the last time I checked (which was a while ago)

This part was the key. I just checked the fuses and noticed they weren't as I expected. I'd completely forgot that from the Arduino IDE one has to "Burn Bootloader" to update the fuses after selecting a different clock speed from the menu.

Sorry about the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants