Electronic – Arduino: programmatic disable crystal/change system clock prescaler

arduinoavr

I have a ATtiny2313 board which does not have a crystal installed. According to documentation it looks like it then runs by default on 1MHz. Because this board only allows ISP programming I thought about first testing the timer-related code on Arduino (with ATmega 328, but using only features which would also be available on the ATtiny2313).

Questions:

  • How to disable the Arduino crystal (temporarily) without having to set fuses (don't want to send my AVR to hell)?
  • Is it possible to change the system clock prescaler by code, e.g. to change the system clock of the ATtiny2313 from 1MHz to 8MHz?

Best Answer

Yes, look at setting the fuse bits to use the internal oscillator rather than an external crystal. Using the OSCCAL register you can even fine tune the internal RC oscillator on the arduino to mimic your ATtiny2313. Once you have done this

If the Internal Calibrated RC Oscillator is used as chip clock source, PB7. . .6 is used as TOSC2...1 input for the Asynchronous Timer/Counter2 if the AS2 bit in ASSR is set.

See page 3 and page 84 of the datasheet for the ATmega328P which is the microcontroller on your arduino.

Response to Comment Oh, I might be misunderstanding your question. The fuses are 'one-time' with respect to your program in that once they're set and your program is running they can't be changed anymore until you reprogram. They are used to select which clock source is being used.

However, they can be set many times if you are trying to simulate your program at different clock frequencies. Oh and to the last part, I don't believe there is a way to change the fuses with an USB/FTDI programmer. For more information on fuses, see here.

If you're asking if there is a way to change the fuse settings through the arduino board I believe the answer is no. You need to use an something like the STK-500 or AVR-ISP mkII. Also note, these programmers also plugin to your USB port on your computer, but they do not use the FTDI chip like the arduino board does.