2

I'm building something that will mostly run on bare ATmega328p at 8 MHz (as described eg. here), but sometimes I need to run the same code on an Arduino.

I need to initialize the DHT library depending on the clock frequency. For simplicity, I'd rather detect the running environment runtime, in the program itself.

So, is there a straightforward way to detect the clock speed (from fuses?) or some other difference between a stock UNO board and a bare ATmega328p using the internal 8 MHz clock?

  • 1
    Is there anything stopping you using the internal clock of the ATmega on an Arduino board? – user588 Aug 27 '14 at 19:06
  • Actually one thing I want this for is to test how the clock frequency (among other things) affects the RF range. – tuomassalo Aug 27 '14 at 19:27
  • 1
    is F_CPU set? im wondering if you can read that – sachleen Aug 27 '14 at 20:54
3

Bare vs Stock is nearly impossible to detect at compile time. Where it depends on what you think you are trying to determine versus detect.

The best I can think of is creating a different Board definition; one for your bare and the other being the stock (i.g. UNO). Where now in IDE 1.5 see github.com/arduino/Arduino/wiki/… the parameter uno.build.board=AVR_UNO, which provides is used to set a compile-time variable ARDUINO_{build.board} to allow use of conditional code between #ifdefs. So you can create bare board definition with e.g. bare.build.board=MY_BARE and then have some #ifdef's for ARDUINO_MY_BARE

Otherwise wrap some #ifdef's around F_CPU as @sechleen points out. This is viable and done often in the core libraries.

1

The easiest way to distinguish your bare AVR from the arduino board at run time is to build the bare AVR board with a distinguishing feature - such as a known voltage on one of the D/A ports - that won't be present on your Arduino.

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.