Go Down

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

johnamon

#5
Jan 26, 2013, 07:33 pm Last Edit: Jan 26, 2013, 07:37 pm by johnamon Reason: 1
Thank you for your reply dc42,

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?

Thanks again for your help :)

John

dc42


Thank you for your reply dc42,

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


Yes, the parameter to setFrequency is in Hz.


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.

johnamon

#7
Jan 26, 2013, 08:26 pm Last Edit: Jan 26, 2013, 08:28 pm by johnamon Reason: 1
Thanks again dc42,

I can see from the datasheet that I want to set the prescaler bits to 1001 or something equivalent:


Do you know how I correctly write that 1001 row into the prescaler instruction?

Coding Badly

I am using (what I believe is your) own arduino-tiny core,


Got it.

Quote
and your knock-bang debugging too - I am in your debt already!


Excellent.


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?

johnamon

Hi Coding Badly,

Thanks for taking another look.  I am using millis for button debouncing, but I can adjust the software timings to suit if I end up speeding up / slowing down the timer clock.

I am currently working on a breadboard, so pins are completely flexible.

Thanks again,

John

Go Up