Attiny85 set clock 8Mhz=>1Mhz

Hello

Please tell me, is it possiple clock speed 8Mhz to 1Mhz in run time with code?

I need to 1Mhz in normal operation but I need 8 Mhz for Software Serial operation .

is it possible?

Best Regards

Use the source Luke. Read the data sheet

particularly sections 6.3 and 6.5.2. Short answer is the clock can be changed under program control.

1 Like

Thank you for your reply.

Please, share if you have changing frequency code for Arduino IDE.
I found message about changing frequency but I couldnt write code correctly.

Why? Why not run at 8MHz for normal operation?

You can use Software Serial at 1MHz if the right library is used. For example

Why are you using Software Serial? The attiny has a USI module which allows you to do Serial using hardware.

1 Like

the clock is easy enough to change, via the Clock Prescale Register (CLKPR)

But I'm pretty sure that the Arduino environment doesn't support changing the clock on the fly - if you've configured your sketch to run at 1MHz, changing CLKPR to 8MHz won't affect the softwareSerial Library's behavior (it'll still be assuming 1MHz), and things like millis() will be returning the wrong numbers (and will continue to be wrong in an absolute sense, even after your switch back.)

1 Like

I cofession, I dont have any idea until you have talked about USI. :slight_smile:
I checked USI now and I can send data with via Serial . But, I think that it is one way communication.
isnt it?

t1
t2

I am willing to a pedometer with 3000mAh battery. So I think that Attiny85 elegible for this work.
and I use a adxl345.

I assume that if i reduce clock speed i can decrease attiny85 power current.
So I try to work in 1Mhz Attiny85.

You are right Arduino IDE and PlatformIO doesnt support clack changing on run time.

But in PlatformIO in platform.ini file

; change MCU frequency
board_build.f_cpu = 10000000L

if i use like that attiny85 works on 1Mhz.


t4

I can adjust clock speed only before upload code

Please stop posting images which contain only text. Copy the text you want to share and paste it into your post between code tags.

1 Like

If you can use sleep modes, this will save even more power.

1 Like

Here is some code to change the clock speed at run time. If you do that the internal peripherals will run at the new speed. This will mess up routines that don't know the clock speed has changed!

In the following code the delay() will be 8 times as long in slow mode because it didn't get the memo that we changed the base clock. The code alternates between flashing an LED fast and slow.

/* 
 *  Change clock speed between default and 1/8 as fast 
 *  
 *  An LED is on digital pin 4 and the blink rate will change depending 
 *  on the clock speed
 */

void setup() {
     pinMode(4,OUTPUT);  // set pin 4 as output
}

void fastClock(){
  noInterrupts();
  CLKPR = 0x80; // enable clock prescale change
  CLKPR = 0;    // no prescale
  interrupts();
  
}
void slowClock(){
  noInterrupts();
  CLKPR = 0x80; // enable clock prescale change
  CLKPR = 3;    // divide by 8 prescale
  interrupts();
  
}
bool clockFast = true;  // start out in regular mode

void loop() {
   for (int i = 0;i<3;i++){
    digitalWrite(4,HIGH); 
    delay(200);      // Actual delay will depend on clock speed
    digitalWrite(4,LOW); 
    delay(200);      // Actual delay will depend on clock speed
   }
   if (clockFast){  // are we in fact mode?
     slowClock();   // go to slow mode
     clockFast = false; // now in slow mode
   } else {
     fastClock();  // speed it up
     clockFast = true; // now in fast mode 
   }
}
1 Like

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