ATtiny analog pins - best practice

Hi all,
i'm working on my first project on the ATtiny85, using the ATTiny Core from Spence Konde.

I want to read an analog signal on Pin 3 (PB4), convert it to a digital signal and send a pwm-signal on Pin 6 (PB1).

For generating the pwm-signal i write directly to the registers.

Now i'd like to know, what's best practice for reading the analog pin.

Should i use Arduinos analogRead() or read into the corresponding register like described in this tutorial?

Same for the pwm-signal. Would it be better or worse to use digitalWrite() instead of writing to OCR1A?

Thanks!

PWM uses the analogWrite() function. I would just use analogRead() and analogWrite() unless there is a compelling reason to use the registers (ie speed faster than 100us for a read or changing PWM frequency).

"Best practice" is hard to define without knowing, in detail, what you are trying to do.

groundFungus:
PWM uses the analogWrite() function.

Of course... :cold_sweat:

groundFungus:
I would just use analogRead() and analogWrite() unless there is a compelling reason to use the registers (ie speed faster than 100us for a read or changing PWM frequency).

"Best practice" is hard to define without knowing, in detail, what you are trying to do.

As i'm changing the pwm frequency to 20KHz, i have to use registers. The pwm-signal is used to control a 4-pin dc fan.
So, would you write to TCCR1 and use analogWrite() along or stick to writing to registers?

Is there any significant difference in using the Arduino functions over writing to registers or even mixing both up?

Thanks and regards!

Philipp

Have you tried something like:

analogWrite(PB1,analogRead(PB4) / 4);

No, not yet.
As i'm also changing the TOP-Value to 199, this equation wont work.
I would use map() to assign the values.

Any reason to use such a particular frequency for just a fan?

septillion:
Any reason to use such a particular frequency for just a fan?

Yes.
4-Pin fans with a separate pwm-wire need a frequency of ~25KHz (Intel specs).
The fan is use, accepts a frequency of 18-30KHz.
Anything below 20KHz is hearable and results in a humming motor.

That spec is not exact at all :wink: So I would just use the closest full scale PWM frequency.