I am not experienced in the under-the-hood details of microcontrollers and I wondered if anyone could help me? I have settled on a nice RC filter arrangement to produce a 'true analogue' voltage, with the RC calcs based on 32kHz PWM frequency.

I would like to set my 8MHz ATtiny45's PWM frequency to approximately 32kHz, but I'm not sure if a guide that I have found is quite correct....

Reading the guide below, the author suggests that he has achieved 32kHz PWM with the following like

That all looks great, but the post here suggests that the line above will only result in a PWM frequency of 15.6kHz... Unfortunately I don't have access to an oscilloscope to perform my own confirmation

I also don't understand why there is a 'b' at the end of the Timer set line (my lack of understanding showing through). By looking at the datasheet I'd have thought that the Timer select line should read something like:

In order to use your code, should I supply the frequency in hertz, like '32000'?

Also, your Off function appears to turn the pin off, whereas I would like variable PWM duty cycle, it is a throttle that I am programming and the idea is that I can set 0v to 5v over PWM using analogWrite() - is it still possible to use analogueWrite with 255 representing 5v after i've used your code to turn the pin on?

Also, your Off function appears to turn the pin off, whereas I would like variable PWM duty cycle, it is a throttle that I am programming and the idea is that I can set 0v to 5v over PWM using analogWrite() - is it still possible to use analogueWrite with 255 representing 5v after i've used your code to turn the pin on?

I'm sorry, I didn't read your original post properly. The code I supplied is intended to provide a variable frequency square wave from timer 1 rather than for PWM. If you are already using Coding Badly's core, then I think all you need to do is to set the prescaler of the timer you are using for the PWM output to 128. That should give you a PWM frequency of 8MHz/256 = 31.25kHz. However, if the timer you are using for PWM is the same one used for the micros() function, then this would mess up the values returned by micros().

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

By default, millis is on timer 1. If you alter the timer 1 prescaler, millis (and its ilk) will not work correctly. Does your application use millis, micros, delay, or delayMicroseconds?

The millis timer is configured for "fast PWM". The other timer is configured for "phase-correct PWM". This detail is important when calculating the output frequency. Fast PWM is much more appropriate for a regulator.

How easily can you change the the output pin? Do you have a circuit board made? Are you working on a bread board?

Of the two timer/counters, only #1 has a prescaler option of 128. So one option is to use timer 1 to generate your PWM signal and move the millis function to timer 0. Alternatively, switch timer 0 to Fast PWM mode and set its prescaler to 256.

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

So to be clear, I should change Prescale_Value_1 in your code below to 255 (255+1 = 256)?

How does the timer change affect the pin mapping? I was (arbitrarily) going to use physical Pin5 (or Pin0) for my PWM source, would this still be the pin to use? The datasheet shows all the PWM pins having a OC0A or 0C0B reference making it not clear to my inexperienced eyes as to which PWM timers are mapped to which pins?