ATTiny85 running at half speed

I have programmed an ATTiny85-20U with the below code. Basically toggle the output once every 1000Hz.

However for unknown reasons, if I run it at either 8MHz or 1MHz (and adjust Timer1 prescaler from 64 to 8) the output is toggling at exactly once every 2 seconds, not once every 1 second as I would expect.

I am programming it with Xgpro, and the fuses are set as follows:
CKDIV8 = 0 - This is unticked for 8MHz, ticked for 1MHz.
CKOUT = 0 - unticked
SUT1 = 0 - unticked
SUT0 = 0 - Ticked
CKSEL3 = 0 - Ticked
CKSEL2 = 0 - Ticked
CKSEL1 = 0 - unticked
CKSEL0 = 0 - Ticked

This would indicate that CKSEL = 0010, giving 8MHz.

const byte OutputPin = 2;

void setup()
{
  DDRB |= (1 << OutputPin);           // Pin to Output
  PORTB &= ~(1 << OutputPin);         // Pin Low

  // Set Timer1
  noInterrupts();
  TCCR1 = 0;                    // Set Control Register to 0 for Normal Mode (Count up only, no Interrupts generated)
  TCNT1 = 0;                    // Clear Timer Counter
  TIMSK = 0;                    // Clear all Interrupt Enables

  TCCR1 |= (1 << CS12);         // Set Prescaler to 8
  OCR1A = 124;                  // Set compare match register for 1000 Hz increments. = 1000000 / (8pre * 1kHz) - 1 (must be <255)
  TCCR1 |= (1 << CTC1);         // Set Clear Timer Compare
  TIMSK |= (1 << OCIE1A);       // Enable Output Compare Register Interrupt
  interrupts();
}


// Start of Timer 1 Output Compare
ISR(TIMER1_COMPA_vect)
{
  static int test = 0;

  test++;

  if (test == 1000)
  {
    test = 0;

    static boolean test2 = false;
    if (test2 == true) PORTB |= (1 << OutputPin);              // Output Inactive
    else PORTB &= ~(1 << OutputPin);              // Output Inactive

    test2 = !test2;
  }
}
// End of Timer 1 Output Compare

void loop()
{

}

should be OCR1C = 124;

Was the code made by ChatGPT? The commenting and the kind of error made suggests so.

Thanks hmeijdam. I had spotted the issue earlier today. The timer code was made by a timer/calculator website. It is working fine now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.