Loading...
  Show Posts
Pages: [1]
1  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 27, 2013, 06:24:04 pm
oK - I can see from Page 76 of the datasheet that in Fast Mode PWM the frequency pre-scaler should be 1 as the system clock is already divided by 256.....

Thank you again dc42 - this is a fair bit more complicated than I expected when I was ordering bits on the Farnell website, I couldn't have got here without you guys!
2  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 27, 2013, 03:25:22 pm
Thank you dc42 and Coding Badly.  Due to the amount of information presented in the previous posts I thought I would summarise what I plan to do - if you guys let me know that the steps below are correct, then I will edit the first post in this thread to explain clearly how we got 32kHz frequency PWM from my ATtiny45.


Summary:

I would like the PWM output from an ATtiny45 to produce a variable voltage of 0v to 5v, via a RC low pass filter.  I have calculated that a 32kHz PWM frequency would be ideal, the high frequency allows low ripply and relatively fast 'settle' time.

To achieve 32kHz, I need to divide the system 8MHz clock by 256 (prescaler of 1 in Fast Mode PWM).  However the mills() and delay() programming functions utilise Timer1 - so to maintain programming functionality we will assign Timer0 to mills and delay (assigning Timer1 to PWM is dealt with later in the thread):

Execution:

  • Locate core_build_options.h in the Tiny Core directory
  • Open it in your favourite text editor (I like Visual StudioTextEdit.app  smiley-kiss)
  • Navigate to line 107...
http://code.google.com/p/arduino-tiny/source/browse/trunk/hardware/tiny/cores/tiny/core_build_options.h#107
  • Change TIMER_TO_USE_FOR_MILLIS to zero...
#define TIMER_TO_USE_FOR_MILLIS                   0
  • Save and close the file

Because my analogue voltage requirement is best served by 'Fast PWM', I perform the modification to core_build_options.h:

  • Locate core_build_options.h in the Tiny Core directory
  • Open it in your favourite text editor (I like Visual StudioTextEdit.app  smiley-kiss)
  • Navigate to line 119...
http://code.google.com/p/arduino-tiny/source/browse/trunk/hardware/tiny/cores/tiny/core_build_options.h#119
  • Change FAVOR_PHASE_CORRECT_PWM to zero...
#define FAVOR_PHASE_CORRECT_PWM                   0
  • Save and close the file

Because we have favoured FAST PWM, OC1A is used to control PB1, and also PB0 - so Physical Pins 5 and 6 may be used to generate the PWM signal that is required.



As-yet Timer1 has not had it's frequency set to 32kHz - so in this step we do that...

Code:
#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 )
{
analogWrite(PB0,255); //Gives 5V analogue output
}


Thanks again

John
3  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 07:19:44 pm
Thank you again, your depth of knowledge, and ability to communicate what you know is humbling!  

I don't know why - but I thought you were in Australia....  It's past midnight here and I need to go to sleep!  Anyway I have realised that you are donationware - I'll get on that right away!  smiley-red

Thank you again for 'babysitting' me through this.

What I need to do now is document all this, as I can see a lot of this information has already been posted and I think we've stitched a lot of it together here.. smiley
4  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 06:52:04 pm
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?  
5  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 05:02:31 pm
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! smiley
6  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 03:40:59 pm
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
7  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 02:26:36 pm
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?
8  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 01:33:14 pm
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 smiley

John
9  Using Arduino / Microcontrollers / Re: ATtiny45 PWM Frequency Selection on: January 26, 2013, 06:58:32 am
Hi Coding Badly,

I am using (what I believe is your) own arduino-tiny core, and your knock-bang debugging too - I am in your debt already!  smiley-roll

Thank you for the replies smiley
10  Using Arduino / Microcontrollers / ATtiny45 PWM Frequency Selection on: January 25, 2013, 12:26:17 pm
Hi Guys,

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

http://nicknorton.net/?q=node/7

TCCR0B = TCCR0B & 0b11111000 | 0b001

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  smiley-sad

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:

TCCR0B = TCCR0B & 0b11111000 | 1001

Does anyone have a definative answer?

Thanks in advance

John
Pages: [1]