user943: 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?

user313: 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.

user60: 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.

943: <p>I'm building something that will mostly run on bare ATmega328p at 8 MHz (as described eg. <a href="http://www.gammon.com.au/forum/?id=11637" rel="nofollow"><strong>here</strong></a>), but sometimes I need to run the same code on an Arduino.</p>

<p>I need to <strong><a href="https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino#L21" rel="nofollow">initialize the DHT library</a></strong> depending on the clock frequency. For simplicity, I'd rather detect the running environment runtime, in the program itself.</p>

<p>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?</p>
 313: <p>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.</p>
 60: <p>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.</p>

<p>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 <a href="https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification" rel="nofollow">github.com/arduino/Arduino/wiki/…</a> 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</p>

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