Go Down

Topic: ATtiny45 PWM Frequency Selection (Read 4 times) previous topic - next topic

dc42

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.

johnamon

#11
Jan 26, 2013, 11:02 pm Last Edit: Jan 26, 2013, 11:15 pm by johnamon Reason: 1
wow, I don't know how to do any of that!

32kHz is roughly 8MHz/256 - so I believe that I want a prescaler of 256??  Or is there a halving of the frequency going on somewhere that I'm not aware of which means I require a prescaler of 128?

If there is the option to have fast mode PWM running at 32kHz and maintain millis and delay working normally, I'd sure be grateful for the how-to! :)

Coding Badly

#12
Jan 27, 2013, 12:05 am Last Edit: Jan 27, 2013, 12:15 am by Coding Badly Reason: 1

In addition to what @dc42 said, timer 1 on the X5 family is designed for doing what you are trying to do.  It has lots more prescaler options and the base clock can be changed to 64 MHz (or 32 MHz).

I suggest swapping the timers so timer 1 is available to use as you please...
http://arduino.cc/forum/index.php/topic,142948.msg1073712.html#msg1073712

Bear in mind, that changes the core.  All your ATtinyX5 applications are affected.

Coding Badly


The Tiny Core includes a (partial) hardware veneer that should make changing the "user timer" a bit easier.  Give this a try...

Code: [Select]
#include <UserTimer.h>

void setup( void )
{
  UserTimer_SetToPowerup();
  UserTimer_SetWaveformGenerationMode( UserTimer_(Fast_PWM_FF) );
  UserTimer_ClockSelect( UserTimer_(Prescale_Value_1) );
  // Frequency is F_CPU / (1 * (0xFF+1))
  // (8 megahertz) / 256 = 31.25 kilohertz
}

void loop( void )
{
}


The code changes timer 0 when millis is on timer 1 and vice versa.

johnamon

#14
Jan 27, 2013, 12:52 am Last Edit: Jan 27, 2013, 01:01 am by johnamon Reason: 1
Thank you very much Coding Badly - I really appreciate your help.  

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?  

Go Up