[SOLVED] changing TCCR0B in loop

I’m making a pwm fan controller on an ATTINY85 @ 8mHz. But there is a library i’m using that uses

Does it pose a problem when I change Timer0 (TCCR0B) often?

I’m using

TCCR0B = TCCR0B & 0b11111000 | 0x05;
to get high frequency pwm
and then
TCCR0B = TCCR0B & 0b11111000 | 0x03;
for the millis critical part.

are these settings stored in SRAM? if not can i change them as often as i like?

this is the complete code:

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 0
#define TEMPERATURE_PRECISION 9
int fanpin = 1;

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int fanStartTemp = 32;
int fanStopTemp = 30;
int fanFullSpeedTemp = 80;
int currentTemp;
int minimalPWM = 30;
int maximalPWM = 70;
byte calculatedPWM = 0;

void setup(void)
{
  pinMode(fanpin, OUTPUT);
  sensors.begin();
  digitalWrite(fanpin, HIGH);
  delay(5000);
  digitalWrite(fanpin, LOW);
}

void loop(void)
{ 

  sensors.requestTemperatures();
  currentTemp = (int)(sensors.getTempCByIndex(0)+ 0.5); 
  if (currentTemp < fanStopTemp) calculatedPWM = 0;
  if (currentTemp >= fanStartTemp && currentTemp < fanFullSpeedTemp) calculatedPWM = minimalPWM + ((255 - minimalPWM)/(fanFullSpeedTemp - fanStartTemp) * (currentTemp - fanStartTemp));
  if (currentTemp >= fanFullSpeedTemp) calculatedPWM = 255;

if(calculatedPWM > 0)
{
  TCCR0B = TCCR0B & 0b11111000 | 0x01;
  analogWrite(fanpin, calculatedPWM);
  delay(38400000);//10 minutes
  TCCR0B = TCCR0B & 0b11111000 | 0x03;
}
else
{
  delay(600000);//10 minutes
}

In a technical sense you can change it as often as you like. Whether that makes logical sense is a different question altogether. I haven’t taken the time to study the datasheet to figure out what the changes you propose do. However if you upset the timing for millis() in order to make PWM work you can’t simply switch the value back and expect miilis() to behave as if nothing happened. The count will be off.

…R

So you mean this won't damage or end the life off the attiny any sooner?

My project is a fan controller that reads a temperature and controls fan speed

Basicly I'm using PWM on pin 0 and 1, but this uses the same timer as millis and delay.

Onewire and the DS18b20 lib uses delay.

So this is what I do:

1. Read temperature from ds18b20 and calculate pwm duty cycle
2. change to  high frequency pwm
3. analogWrite the calculated duty cycle to fan pin
4. wait some time 
5. digitalWrite low to fan pin
6. change to regular frequency pwm

The disadvantage is that step 1 takes about 1 second to perform. So it is audible in the fan

Is there some reason not to use Timer1?

Never mind.

This question was a non-issue. TCCR0B isn’t used by delay on the Attiny.

I just thought it was used the same way as on the atmega328.

The code below runs fine :

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 0
#define TEMPERATURE_PRECISION 9
int fanpin = 1;

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

float fanStartTemp = 32;
float fanStopTemp = 30;
float fanFullSpeedTemp = 80;
float currentTemp;
float minimalPWM = 50;
float maximalPWM = 100;
float calculatedPWM = 0;

void setup(void)
{
  pinMode(fanpin, OUTPUT);
  sensors.begin();
  TCCR0B = TCCR0B & 0b11111000 | 0x01;
}

void loop(void)
{ 

  sensors.requestTemperatures();
  currentTemp = sensors.getTempCByIndex(0); 
  if (currentTemp < fanStopTemp) calculatedPWM = 0;
  if (currentTemp >= fanStartTemp && currentTemp < fanFullSpeedTemp) calculatedPWM = minimalPWM + ((maximalPWM - minimalPWM)/(fanFullSpeedTemp - fanStartTemp) * (currentTemp - fanStartTemp));
  if (currentTemp >= fanFullSpeedTemp) maximalPWM = 255;

  if(calculatedPWM > 0)
  {
    analogWrite(fanpin, calculatedPWM);
  }
  else
  {
    digitalWrite(fanpin, LOW);
  }
}