4
\$\begingroup\$

I am looking at programming an AVR chip as a possible avenue due to some problems I'm having with 555 circuitry, but I need to be able to adjust the frequency on the fly with a pot. Is it possible to setup an AVR chip with variable frequency so that the program execution speed can be adjusted right on the board with a simple pot and without having to go back and reprogram the chip?:

\$\endgroup\$

3 Answers 3

5
\$\begingroup\$

As others have already stated, nearly every AVR developer picks some fixed frequency for each application, and runs the AVR chip at that one fixed frequency. Just this week I wrote some code that runs on an AVR that runs at fixed 16 MHz (which is pretty common), but it generates two variable-frequency outputs independently controlled by two pots. (The pots are actually connected to another microcontroller, which reads the pots, and sends their position up a 400 ft RS485 serial cable to the AVR UART).

However, there are a few AVR developers who vary the frequency of their AVR chip at run time, such as Spritesmods (a) (b); cloudscapes (a); K0LR (a); etc.

Using an external RC oscillator

A few AVRs support an "external RC oscillator" option, which is extremely similar to a 555 RC oscillator -- you hook a resistor (such as a pot) and a capacitor directly to the AVR. Just like the 555 RC oscillator, when you turn the pot, it changes the resistance R of the pot, which changes the frequency. "Application Note AVR051: Setup and use the external rc oscillator" (a), (b)

Alas, many popular AVR chips do not support an "external RC oscillator" option. Even those that do, the frequency is limited to the range 100 kHz - 12 MHz. (The ADC doesn't work right at low frequencies, and the UART is going to be difficult to use if the frequency keeps changing).

driving the AVR at some frequency generated elsewhere

If you have some clock source that already generates the frequency you want, you can connect its output to the AVR and program the AVR to use "External Clock" option. This approach works with any AVR and any frequency from zero Hz (0 Hz) to 8 MHz. (i.e., you can go much slower than the "external RC oscillator" approach). (The ADC doesn't work right at low frequencies, and the UART is going to be difficult to use if the frequency keeps changing).

You program the AVR fuse bits to use "External Clock" option if some external source (that oscillates by itself) is driving a squared-off signal into the AVR clock input pin. (Even if technically that external clock is an RC oscillator). This external oscillator could, for example, be a 74HC132 Schmitt NAND and a capacitor and a pot, or a 555 oscillator with a capacitor and a pot, or a huge variety of other oscillators.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks David. Superb answer! I wish I could mark everyone's answer as the accepted answer. LOL All these are great! \$\endgroup\$
    – JamesHoux
    Commented May 9, 2013 at 6:22
  • \$\begingroup\$ So if I understand correctly, the AVR fuse bits could be programmed to use External clock and then the clock input pin could be connected to a square signal from a 555 or other RC oscillator that I adjust on the fly with a pot. And the result would be that the AVR program would then run at given frequency of the square wave going into the clock input. It could be adjusted while the AVR is running without problem? \$\endgroup\$
    – JamesHoux
    Commented May 9, 2013 at 6:28
  • 1
    \$\begingroup\$ Yes. Like most processors, the AVR does a little bit of work after each clock edge. As long as the clock edges are far enough apart that its work is done before the next clock edge, it works fine. The time between edges can be different every time -- that is what makes dynamic frequency change possible. \$\endgroup\$
    – davidcary
    Commented May 9, 2013 at 17:23
5
\$\begingroup\$

It's not clear, but it seems from your question that you want to have a variable frequency waveform that responds to a Pot value for adjustment.

Just staying in the AVR mode (there are many different ways to do this) you could also do this without changing the operating frequency of the device. You would just need to read the POT value with the ADC input and use that value to adjust the PWM output accordingly.

\$\endgroup\$
3
\$\begingroup\$

While in principle I don't see a problem you're limited to a range of 100KHz to 12MHz when using an external RC oscillator. You'd need to use an additional resistor to make sure those constraints are met so you don't get erratic operation and possible damage to the AVR. The long wires and the pot itself will probably also radiate quite a bit of RF interference.

The usual way would be to use the pot as a voltage divider and read the value using an analog input. Then use that value to vary the frequency of your timing loop while the CPU keeps running at the same clock frequency.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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