NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Is it possible to scale the crystal clock down to 8 MHz?

May 14, 2011
by rboggs10
rboggs10's Avatar

I want to scale it down to 8 MHz.

May 14, 2011
by esoderberg
esoderberg's Avatar

RBoggs,

Yes. See page 27 of datasheet. The chip has an 8MHz internal oscillator that can be used in lieu of an external source.

Eric

May 14, 2011
by rboggs10
rboggs10's Avatar

Thanks Eric.

May 14, 2011
by rboggs10
rboggs10's Avatar

But how do I use this internal Oscillator?

May 14, 2011
by Noter
Noter's Avatar

To use the internal oscillator you have to change the fuse settings on the chip. Changing fuse settings can not be done via the bootloader and another type of programmer is required. The most popular is the usbasp type that can be purchased in kit form. Ricks library article on installing the bootloader is a good place to find more info on using the usbasp and setting fuses.

Just curious, why do you want to use the 8MHz clock?

May 15, 2011
by rboggs10
rboggs10's Avatar

I want it to be 8 MHz because I am doing a test with PWM to try and play musical notes through it. If I have an 8 MHz clock then by my calculations each PWM count should be an even 1us instead of the 0.5425us with the 14.7456 MHz clock. This even clock count would make it much easier for me to play exact musical notes.

void pwm_init() {
  // setup Timer1 for Fast PWM mode, 16-bit
  // COM1B1 -- for non-inverting output
  // WGM13, WGM12, WGM11, WGM10 -- for Fast PWM with OCR1A as TOP value
  // CS11 -- for CLK/8 prescaling

  OCR1A = 36864;    // sets PWM to repeat pulse every 20.0ms
  pwm_set(PWM_START);
  TCCR1A = (1<<COM1B1) | (1<<WGM11) | (1<<WGM10);
  TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11);

  // each count is 8/8000000 = 1us.
  // so 1.0ms = 1000
  //    1.5ms = 1500
  //    2.0ms = 2000
  //   20.0ms = 20000
}
May 15, 2011
by Noter
Noter's Avatar

Could you use a function to set the OCR1A value? Something like

void setOCR1A(double ms_value){
    OCR1A = (ms_value/1000)/(14745600/8);
}

Then you would use the function to set OCR1A to the correct value - setOCR1A(20.0);

Or make a function where you can give a frequency. It may not be as perfect as using the 8MHz clock but maybe your ear won't know the difference. I think crystals are accurate to 100ppm and the max error in calculation would be another 100ppm.

May 16, 2011
by rboggs10
rboggs10's Avatar

That sounds pretty good I will give it a try and if it doesn't work out the way I want it then I have an 8 MHz Crystal on it's way.

May 16, 2011
by Rick_S
Rick_S's Avatar

Just so you know, (in case you don't already), if you change the crystal you will no longer be able to load a program with the bootloader and NK hardware without replacing it with the original crystal.

Rick

May 16, 2011
by bretm
bretm's Avatar

Is 8MHz actually better for music than 14.7456MHz? For equal-tempered scale you have:

                     14.7456MHz    8 MHz
Note    Frequency    Clock cycles  Clock cycles

C4      261.63       56360.51      30577.53
D4      293.66       50213.17      27242.39
E4      329.63       44733.79      24269.64
F4      349.23       42223.18      22907.54
G4      392.00       37616.33      20408.16
A4      440.00       33512.73      18181.82
B4      493.88       29856.65      16198.27

I'm not clear why one is better than the other. The relative error if you round to the nearest clock cycle count is actually smaller, on average, for the faster clock.

April 16, 2012
by RogerFL
RogerFL's Avatar

For applications that don't require the speed or time accuracy, it would be nice to eliminate 3 parts on a PCB (crystal and its two caps) but still have a working serial programming solution.

Any idea on what it would take to get the bootloader to support the internal oscillator? Ideally, the oscillator speed is a macro so we change it to 8000000. Change the fuse setting. Recompile and reload.

Would we also need to slow down the serial connection?

Anyone tried it?

April 16, 2012
by Ralphxyz
Ralphxyz's Avatar

Of course you could allways use a programmer and load the .hex directly without a bootloader.

If the mcu will be dedicated to this app it does not need a boot loader plus you get more programming space.

Search the forum for ISP Programmer.

Ralph

May 08, 2012
by RogerFL
RogerFL's Avatar

Started working on this again. Got the bootloader to support 8MHz internal for 2 out of 3 steps for loading. It reads, writes, but always fails when reading flash in final step (I guess to verify). Fails with error msg: butterfly_recv(): programmer is not responding

Post a Reply

Please log in to post a reply.

Did you know that two resistors can be used to make a voltage divider? Learn more...