ATTINY85 internal clock frequency problem

Hi

I have a strange problem with internal clock frequency on a ATTINY85. If I select between 1Mhz, 8Mhz and 16Mhz the faster the clock frequency the slower my code runs

I am using the attiny by David A Mellis in boads manager

I can imagine how this could happen, but please post a link to the hardware you are using, the code you are uploading and describe how you are changing the internal clock frequency. Are you clicking "burn bootloader" after changing the frequency?

If the code you upload believes the clock frequency is faster than it really is, and your code uses delay(), millis(), micros() etc. then the code will run slower than expected.

Please try to read the fuse byts. Incorrect fuse byts lead to abnormal behaviour of AVR chips.

How would I read the fuses.

I am using a UNO as a ISP that is attached to the ATTINY85. I am selecting the options bellow.

This is the code that I am uploading.

#include "PinChangeInterrupt.h"
#include <SwitecX12.h>

// Define X12 control pins
const int A_STEP = 4;
const int A_DIR = 1;
const int RESET = 0;

// Define the potentiometer pin
const int POT_PIN = A3;

// Define the RPM sensor pin
const int RPM_PIN = 2;

const int STEPS = 265 * 12;  // max movement 265°( 12 steps/deg )

// Create a SwitecX25 object
SwitecX12 motor1(STEPS, A_STEP, A_DIR);  // steps for 265-degree movement

volatile int rpmCount = 0;
unsigned long lastTime = 0;
unsigned long currentTime = 0;
int rpm = 0.0;

//Test purpose only
static bool forward = true;
static int position1 = STEPS * 3/4;
static int position2 = STEPS * 1/4;


void setup() {

   // Attach the new PinChangeInterrupt and enable event function below
  attachPCINT(digitalPinToPCINT(RPM_PIN), rpmISR, RISING);

  pinMode(RPM_PIN, INPUT);

  // Initialize rest pin of X12
  pinMode(RESET, OUTPUT);
  digitalWrite(RESET, HIGH);

  // Initialize motor
  
  motor1.zero();
  motor1.stepTo(STEPS); 
  motor1.stepTo(0); 
 
  
}



void loop() {

  currentTime = millis();
  // Calculate RPM
  if (currentTime - lastTime >= 1000) {  // Calculate every 1 second


    detachPCINT(digitalPinToPCINT(RPM_PIN));
    rpm = (int)(rpmCount * (60.0 / 0.66644) / 6);  // Convert to RPM
    rpmCount = 0;
    attachPCINT(digitalPinToPCINT(RPM_PIN), rpmISR, RISING);
    lastTime = currentTime;


    // Read potentiometer value for fine adjustment
    int potValue = analogRead(POT_PIN);
    int fineAdjustment = map(potValue, 0, 1023, -500, 500);      // Adjust between -500 and 500 RPM
    int adjustedRPM = constrain(rpm + fineAdjustment, 0, 5000);  // Constrain to 0-5000 RPM



    // Map RPM to motor position (0 to 260 degrees)
    int motorPosition = map(adjustedRPM, 0, 5000, 0, STEPS);  // Adjust the steps according to your motor's range
    motor1.setPosition(motorPosition);
  }

  // Update motor movement
  motor1.update();
}

// RPM sensor interrupt service routine
void rpmISR() {
  rpmCount++;
}

Under Tools, after you selected ATtiny 25/45/85, drop down to processor and select ATtiny85, then drop down to clock and select 1, 8, or 16mhz internal.

This is the problem I'm have when selecting the internal clock. The higher the clock the slower the code runs

That would be the case if "selecting the internal clock" only tells the sketch what frequency the clock is running at, but doesn't actually change the clock speed.
My experience from a few years ago, using IDE 1.8.13, is that ATtiny85s come with the internal clock set to 1MHz by default. Using the 'burn bootloader' option changes the internal cock to 8MHz. To set other clock options I think you would have to change the fuses with avrdude.

I have burned the bootloader and it seems to be running at 8Mhz now.

New problem now is when the revcounter code is running if I set it to 1000rpm the motor moves to 500rpm stops for a second then moves to 1000rpm. The same with 4000rpm, stops at 3000rpm for a second then moves to 4000rpm.

So your original problem is fixed. You are welcome.

How are you doing that?

Your code only calculates the RPM once every second, so it will only update once every second.

Could it be that the RPM is increasing from 0 up to 1000 in that first second, making the average RPM 500 over that time?

Thank you.

I am feeding a squarewave pulse into the ATTINY85, at 1000rpm it is 66.6Hz

Is there way to make calculating the RPM quicker

Before you make that selecton, you have to be sure that the relevant fuse bit(s) are properly programmed/unprogrammed. A fresh ATtiny85 has been configured at the Factory for 1 MHz system clock (8 MHz internal clock followed by divide by eight).

If one chooses the ATTinyCore, the sketch would be running at the correct speed. The millisCounter will be working alright.

I am using IDE 2 and ATTinyCore doesn't seem to work

You will have to explain that. There is currently a known issue that the certificate for the site where the json is hosted has expired; you can find a workaround on drazzy.com ssl cert expired ? · Issue #1059 · SpenceKonde/megaTinyCore · GitHub. For anything else you should provide the details.

Can you read the Fuse Bit values of your ATtiny85 using an AVR Programmer or Parallel ROM Programmer and post it here?