1
\$\begingroup\$

I'm completely new to MCU programming. I have an ATmega168 and a 20 MHz oscillator. My question is, how can I "tell" the controller to use this oscillator? I tried to read the datasheet, but that didn't help a lot!

My guess is that I should set the "fuse bits"!

Is it possible that I use this oscillator and run the controller with a lower frequency say 16 MHz?

By the way, I'm using Atmel Studio 6 to program this.

UPDATE

After dextorb's comment, I've uploaded this image:

myAVR ProgTool screen

In the datasheet Table9-5 they say that for a 20MHz frequency range CKSEL3..1 should be 011! Where can I put this value and after setting it, does it mean that I'm good to go with 20 MHz?

\$\endgroup\$
  • \$\begingroup\$ Presenting a question with good puntuation, grammar and formatting is half the battle to getting a decent answer. Re-read what you've written and then honestly tell me that you've spent more than 30 seconds writing this down! You expect answers that are well thought out and not 30 seconds of baffling rubbish. \$\endgroup\$ – Andy aka Nov 1 '13 at 19:24
  • \$\begingroup\$ Ugh, yeah. C'mon dude. You set the fuse bits to change the clock divider. Sounds like you already know where to look for this info... \$\endgroup\$ – dext0rb Nov 1 '13 at 19:30
  • \$\begingroup\$ @Andyaka thanks for your comment sorry about that I tried to edit the question I hope that will help ! \$\endgroup\$ – Engine Nov 1 '13 at 19:30
  • 1
    \$\begingroup\$ Maybe this would be helpful? --ladyada.net/learn/avr/fuses.html \$\endgroup\$ – mikeY Nov 1 '13 at 19:36
  • 1
    \$\begingroup\$ 9.2 Clock sources, page 28. Shows the fuse bit settings for clock select. There is a CLKDIV8 fuse which will divide the clock by 8. There is also a clock prescaler register: pg 37, 9.12.2 CLKPR – Clock prescale register, which can divide your clock by various values. Can you take 20MHz and divide to 16MHz? No. You could do 10MHz with a divide-by-2. \$\endgroup\$ – dext0rb Nov 1 '13 at 19:40
5
\$\begingroup\$

The clock source in AVR is set by fuses that can be changed by an external programmer (the same one you use to load the HEX file), they can set the clock source to internal RC, external clock, crystal etc. Here is a fuse calculator that may come handy http://www.engbedded.com/fusecalc

After setting the clock source fuses you can change the clock prescaler to get a lower core frequency, ATmega48/88/168/328 have a CLKDIV8 fuse that controls the default loaded value to CLKPR (Clock prescale register) to either

  • clk/1
  • clk/8

Apart from that default loaded value you can change the clock prescale at will during runtime, all you need is to include the following header

#include <avr/power.h>

and call the following function with the appropriate value you want to use

   clock_prescale_set(clock_div_1),    // divide by 1, for example with 20MHz clock result      20MHz
   clock_prescale_set(clock_div_2),    // divide by 2, for example with 20MHz clock result      10MHz
   clock_prescale_set(clock_div_4),    // divide by 4, for example with 20MHz clock result       5MHz
   clock_prescale_set(clock_div_8),    // divide by 8, for example with 20MHz clock result     2.5MHz
   clock_prescale_set(clock_div_16),   // divide by 16, for example with 20MHz clock result   1.25MHz
   clock_prescale_set(clock_div_32),   // divide by 32, for example with 20MHz clock result  0.625MHz
   clock_prescale_set(clock_div_64),   // divide by 64, for example with 20MHz clock result  0.312MHz
   clock_prescale_set(lock_div_128),   // divide by 128 for example with 20MHz clock result  0.156MHz
   clock_prescale_set(clock_div_256),  // divide by 256, for example with 20MHz clock result 0.078MHz

power.h manual

\$\endgroup\$
  • \$\begingroup\$ I wasn't aware that the ATmega48/88/168/328 series has a software pre-scaler for the main clock. And its a decent answer to the question. +1 \$\endgroup\$ – Rev1.0 Dec 10 '13 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.