Timer0 - millis/micros pause during ISR

Does Timer0 pause (not counting micros/millis anymore) during an ISR defined on other timers?

Thanks!

All ISRs stop interrupts. Timer0 keeps going but the interrupt it generates is not serviced (until you leave the ISR).

So if I call micros outside the ISR it should still increment, even if the arduino had sit in the ISR for let's say 20 micros?

even if the arduino had sit in the ISR for let's say 20 micros?

Why is the Arduino stuck in an ISR this long?

The ISR would only be "c=1" unlocking a function in the loop that measures RPM and so on. I do not know how much that would take, or if it gives any performance boost instead of just polling digitalRead in the loop.

slayer1991:
So if I call micros outside the ISR it should still increment, even if the arduino had sit in the ISR for let's say 20 micros?

Er, it (the timer itself) would increment for sure. I wouldn't be reading micros personally because if it overflowed it might appear to go down. In other words, each time the timer overflows an interrupt adds 1 to an overflow counter. And then micros takes that (multiplied by a suitable figure), and adds in the current timer amount. So if it happened to overflow the current counter would be lower, but you wouldn't know about the overflow yet.

(edit) see clarification below

ISRs should be fast, but staying in one for 20 uS isn't too bad. After all it takes about 3.5 uS to enter and 3.5 uS to leave it. Remember though, while you are in your ISR other interrupts aren't serviced (eg. serial input).

This is what I am talking about:

const int timeCount = 400;
unsigned long times [timeCount];
int which = 0;

void myISR ()
  {
  if (which < timeCount)
    times [which++] = micros ();  
  }  // end of myISR
  
void setup ()
 {
 Serial.begin (115200);
 attachInterrupt (0, myISR, FALLING);
 }  // end of setup

void loop ()
  {
  if (which < timeCount)
    return;
    
  unsigned long total = 0;
  int counter = 0;
  
  detachInterrupt (0);
  Serial.println ("Analyzing ...");

  for (int i = 0; i < timeCount - 1; i++)
    {
    if (times [i] >= times [i + 1])
       {
       Serial.print ("Item ");
       Serial.print (i);
       Serial.print (" this: ");
       Serial.print (times [i]);
       Serial.print (" next: ");
       Serial.print (times [i + 1]);
       Serial.print (" difference: ");
       Serial.print (times [i] - times [i + 1]);
       Serial.println ();
       }  // end of if
     else
       {
       counter++;
       total += times [i + 1] - times [i];
       }
      
    }  // end of for
   
  Serial.print ("Average = ");
  Serial.println (total / counter);
  Serial.println ("Done.");
  Serial.println ("-----------");
  for (int i = 0; i < timeCount; i++)
    {
    Serial.print (i);
    Serial.print (" : ");
    Serial.println (times [i]);
    }
  Serial.println ("-----------");
  
  while (true) {}
  } // end of loop

Output:

Analyzing ...
Item 173 this: 2032 next: 1020 difference: 1012
Item 264 this: 2040 next: 1024 difference: 1016
Item 354 this: 2032 next: 1020 difference: 1012
Average = 11
Done.
-----------
0 : 100
1 : 112
2 : 124
3 : 136
4 : 144
5 : 156
6 : 168
7 : 180
8 : 188
9 : 200
10 : 212
...
169 : 1988
170 : 2000
171 : 2008
172 : 2020
173 : 2032
174 : 1020   <------- here
175 : 1032
176 : 1044
177 : 1052
178 : 1064
...
263 : 2028
264 : 2040
265 : 1024  <------- here
266 : 1036
...
350 : 1988
351 : 2000
352 : 2008
353 : 2020
354 : 2032  
355 : 1020  <------- here
356 : 1032
357 : 1040
358 : 1052
359 : 1064
360 : 1076
...
397 : 1496
398 : 1504
399 : 1516
-----------

The point here is that the ISR was firing (hopefully at a rate of about 130 kHz). At the nominated spots the time returned by micros() went down, not up.

Thanks for the explanation! Because of this, I think I will use digitalReadFast in a loop and miss the beginning of the spark rather than have micros() loose an overflow.

Hi,
Personally I think you were on the right track at the beginning, using interrupts. How about instead of using millis() and micros() just use a hardware timer directly ?

To do this you would need to -

  1. Set the timer pre scaler to give a relevant scale for your application
  2. In the existing ISR that senses the spark, you would take the timer value, store it in a shared variable, then set the timer back to zero in order to capture the duration of the next pulse.
  3. If you have chosen the scale in 1) correctly then you should never get a timer overflow, this is useful because if you write an ISR to catch a timer overflow and you ever get one, you know something is wrong somewhere, maybe your sensor, maybe your assumptions, but its still really useful information to have and then be able to investigate.

Just and idea,

EDIT: I know that timer1 input capture will do most of the work for you, I haven't used it yet, but if you are willing to do the work of figuring out how to set up the timer1 registers it is pretty much exactly what you want.

Duane B

rcarduino.blogspot.com

If you are trying to time events, I've done some posts and examples about that here:

If I take into account the actual frequencies I want to measure:

  1. Engine 26-77 Hz triggered by analog voltage>value
  2. 2xWheel speed max 28 Hz digital low trigger
  3. Fuel 86-260 Hz digital high trigger at 50% duty

the only one that may be too fast to catch in a loop polling cycle is the last one. But I then want to save the data to an SD card using SdFat and BufferedWriter, a value for each every 30 ms (30Hz aprox) allowing me to buffer 2KB of data and to display it on a LCD through serial every second (it takes max 6 ms to transmit data at 115200 bps).

The interrupts I'd declare might interfere with the latter 2 so I would have to disable them when I get to that part. Would it actually worth it going through the trouble for sensors 2 and 3?

[quote author=Nick Gammon link=topic=98283.msg737220#msg737220 date=1332643941]
I wouldn't be reading micros personally because if it overflowed it might appear to go down.[/quote]
The micros() function will account for the above (based on the timer overflow flag) and so it's a non-issue.

It’s safe to read micros() from within an ISR, but delaying is not as other interrupts will not run. We could re-enable global interrupts from within the ISR, but this can lead to reentrancy and stack overflow issues.

For asynchronous serial communication at 115000 baud, a new character must be fetched every 86 us or there about to avoid data loss. High speed SPI and TWI communication may suffer from the same or induce latency for the same reason.

Timer resolution is 4 microseconds on a 16 MHz Arduino (one timer tick every 4 us) and we have 1024 us between overflows.

Hi,
While your frequencies are low, the problem with poling is that you are only able to look at one sensor at a time, so you only have a complete picture about 10 times a second at top speed or much less at start up - i.e less than once a second !

If you want to use interrupts, but think you only have two available, read this -

I have tested this approach at 700hz and do not see why it could not be used for higher frequencies.

Cut a long story short, the effort to build a test rig is minimal, the effort to code an interrupt based approach is also minimal, so try it and see, nothing ventured, nothing gained.

Finally, what is your application ? I have a very similar requirement and solution for my RC Cars, its reading three 50hz RC Channels and four wheel speed sensors at up to a top speed of 68hz per wheel.

Duane B.

rcarduino.blogspot.com

BenF:

[quote author=Nick Gammon link=topic=98283.msg737220#msg737220 date=1332643941]
I wouldn't be reading micros personally because if it overflowed it might appear to go down.

The micros() function will account for the above (based on the timer overflow flag) and so it's a non-issue.
[/quote]

You are quite right. I've struck out my comments in the post above. However my test proved (what you went on to say) that if you disable interrupts too long the timer permanently goes down because you miss the overflow. In other words, if you leave interrupts disabled for 1024 uS, then you have the issue because one overflow is entirely missed.

I'm wondering wether these modifications in wiring.c are enough to change millis/micros/delay from timer 0 to timer 2 because I want to use the first example here Gammon Forum : Electronics : Microprocessors : Timers and counters to count 2 frequencies on timers 0 and 5 (2 input sensors).

/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  $Id$
*/

#include "wiring_private.h"

// the prescaler is set so that timer2 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER2_OVERFLOW (clockCyclesToMicroseconds(64 * 256))

// the whole number of milliseconds per timer2 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER2_OVERFLOW / 1000)

// the fractional number of milliseconds per timer2 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER2_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)

volatile unsigned long timer2_overflow_count = 0;
volatile unsigned long timer2_millis = 0;
static unsigned char timer2_fract = 0;

SIGNAL(TIMER2_OVF_vect)
{
	// copy these to local variables so they can be stored in registers
	// (volatile variables must be read from memory on every access)
	unsigned long m = timer2_millis;
	unsigned char f = timer2_fract;

	m += MILLIS_INC;
	f += FRACT_INC;
	if (f >= FRACT_MAX) {
		f -= FRACT_MAX;
		m += 1;
	}

	timer2_fract = f;
	timer2_millis = m;
	timer2_overflow_count++;
}

unsigned long millis()
{
	unsigned long m;
	uint8_t oldSREG = SREG;

	// disable interrupts while we read timer2_millis or we might get an
	// inconsistent value (e.g. in the middle of a write to timer2_millis)
	cli();
	m = timer2_millis;
	SREG = oldSREG;

	return m;
}

unsigned long micros() {
	unsigned long m;
	uint8_t oldSREG = SREG, t;
	
	cli();
	m = timer2_overflow_count;
#if defined(TCNT2)
	t = TCNT2;
#elif defined(TCNT2L)
	t = TCNT2L;
#else
	#error TIMER 2 not defined
#endif

  
#ifdef TIFR2
	if ((TIFR2 & _BV(TOV2)) && (t < 255))
		m++;
#else
	if ((TIFR & _BV(TOV2)) && (t < 255))
		m++;
#endif

	SREG = oldSREG;
	
	return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}

void delay(unsigned long ms)
{
	uint16_t start = (uint16_t)micros();

	while (ms > 0) {
		if (((uint16_t)micros() - start) >= 1000) {
			ms--;
			start += 1000;
		}
	}
}

/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
	// calling avrlib's delay_us() function with low values (e.g. 1 or
	// 2 microseconds) gives delays longer than desired.
	//delay_us(us);

#if F_CPU >= 16000000L
	// for the 16 MHz clock on most Arduino boards

	// for a one-microsecond delay, simply return.  the overhead
	// of the function call yields a delay of approximately 1 1/8 us.
	if (--us == 0)
		return;

	// the following loop takes a quarter of a microsecond (4 cycles)
	// per iteration, so execute it four times for each microsecond of
	// delay requested.
	us <<= 2;

	// account for the time taken in the preceeding commands.
	us -= 2;
#else
	// for the 8 MHz internal clock on the ATmega168

	// for a one- or two-microsecond delay, simply return.  the overhead of
	// the function calls takes more than two microseconds.  can't just
	// subtract two, since us is unsigned; we'd overflow.
	if (--us == 0)
		return;
	if (--us == 0)
		return;

	// the following loop takes half of a microsecond (4 cycles)
	// per iteration, so execute it twice for each microsecond of
	// delay requested.
	us <<= 1;
    
	// partially compensate for the time taken by the preceeding commands.
	// we can't subtract any more than this or we'd overflow w/ small delays.
	us--;
#endif

	// busy wait
	__asm__ __volatile__ (
		"1: sbiw %0,1" "\n\t" // 2 cycles
		"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
	);
}

void init()
{
	// this needs to be called before setup() or some functions won't
	// work there
	sei();
	
	// on the ATmega168, timer 2 is also used for fast hardware pwm
	// (using phase-correct PWM would mean that timer 2 overflowed half as often
	// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR0A) && defined(WGM01)
	sbi(TCCR0A, WGM01);
	sbi(TCCR0A, WGM00);
#endif  

	// set timer 0 prescale factor to 64
#if defined(__AVR_ATmega128__)
	// CPU specific: different values for the ATmega128
	sbi(TCCR0, CS02);
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
	// this combination is for the standard atmega8
	sbi(TCCR0, CS01);
	sbi(TCCR0, CS00);
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
	// this combination is for the standard 168/328/1280/2560
	sbi(TCCR0B, CS01);
	sbi(TCCR0B, CS00);
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
	// this combination is for the __AVR_ATmega645__ series
	sbi(TCCR0A, CS01);
	sbi(TCCR0A, CS00);
#else
	#error Timer 0 prescale factor 64 not set correctly
#endif

	// enable timer 0 overflow interrupt
#if defined(TIMSK) && defined(TOIE0)
	sbi(TIMSK, TOIE0);
#elif defined(TIMSK0) && defined(TOIE0)
	sbi(TIMSK0, TOIE0);
#else
	#error	Timer 0 overflow interrupt not set correctly
#endif

	// timers 1 and 2 are used for phase-correct hardware pwm
	// this is better for motors as it ensures an even waveform
	// note, however, that fast pwm mode can achieve a frequency of up
	// 8 MHz (with a 16 MHz clock) at 50% duty cycle

	TCCR1B = 0;

	// set timer 1 prescale factor to 64
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
	sbi(TCCR1B, CS11);
	sbi(TCCR1B, CS10);
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
	sbi(TCCR1, CS11);
	sbi(TCCR1, CS10);
#endif
	// put timer 1 in 8-bit phase correct pwm mode
#if defined(TCCR1A) && defined(WGM10)
	sbi(TCCR1A, WGM10);
#elif defined(TCCR1)
	#warning this needs to be finished
#endif

	// set timer 2 prescale factor to 64
#if defined(TCCR2) && defined(CS22)
	sbi(TCCR2, CS22);
#elif defined(TCCR2B) && defined(CS22)
	sbi(TCCR2B, CS22);
#else
	#warning Timer 2 not finished (may not be present on this CPU)
#endif

	// configure timer 2 for phase correct pwm (8-bit)
#if defined(TCCR2) && defined(WGM20)
	sbi(TCCR2, WGM20);
#elif defined(TCCR2A) && defined(WGM20)
	sbi(TCCR2A, WGM20);
#else
	#warning Timer 2 not finished (may not be present on this CPU)
#endif

#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
	sbi(TCCR3B, CS31);		// set timer 3 prescale factor to 64
	sbi(TCCR3B, CS30);
	sbi(TCCR3A, WGM30);		// put timer 3 in 8-bit phase correct pwm mode
#endif
	
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
	sbi(TCCR4B, CS41);		// set timer 4 prescale factor to 64
	sbi(TCCR4B, CS40);
	sbi(TCCR4A, WGM40);		// put timer 4 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
	sbi(TCCR5B, CS51);		// set timer 5 prescale factor to 64
	sbi(TCCR5B, CS50);
	sbi(TCCR5A, WGM50);		// put timer 5 in 8-bit phase correct pwm mode
#endif

#if defined(ADCSRA)
	// set a2d prescale factor to 128
	// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
	// XXX: this will not work properly for other clock speeds, and
	// this code should use F_CPU to determine the prescale factor.
	sbi(ADCSRA, ADPS2);
	sbi(ADCSRA, ADPS1);
	sbi(ADCSRA, ADPS0);

	// enable a2d conversions
	sbi(ADCSRA, ADEN);
#endif

	// the bootloader connects pins 0 and 1 to the USART; disconnect them
	// here so they can be used as normal digital i/o; they will be
	// reconnected in Serial.begin()
#if defined(UCSRB)
	UCSRB = 0;
#elif defined(UCSR0B)
	UCSR0B = 0;
#endif
}

What's the point in changing millis() to timer 2? You've just swapped one with another.

I've got only T0 and T5 routed to pins on a MEGA. I want them to count falling edges independently, so i need my timing functions moved to the other 8bit timer.

LE: pff... The functions are still using timer 0. I tried your frequency example to see wether timer0 registers interfere with the posted version of wiring, and they did.. I think I'm better off reinventing millis/micros myself on another timer, if I want to tamper with timer 0 registers..
Actually, your first example on the website gets stucked after one loop when it reaches delay with my new wiring and timer 2 replaced with 0 on your sketch

I can't see where you initialize Timer 2 (for the timing functions) in init().

Where should I initialize it? Timer0 was done automatically, wasn''t it? In what file does it start? Here's the complete wiring

/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  $Id$
*/

#include "wiring_private.h"

// the prescaler is set so that timer2 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER2_OVERFLOW (clockCyclesToMicroseconds(64 * 256))

// the whole number of milliseconds per timer2 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER2_OVERFLOW / 1000)

// the fractional number of milliseconds per timer2 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER2_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)

volatile unsigned long timer2_overflow_count = 0;
volatile unsigned long timer2_millis = 0;
static unsigned char timer2_fract = 0;

SIGNAL(TIMER2_OVF_vect)
{
	// copy these to local variables so they can be stored in registers
	// (volatile variables must be read from memory on every access)
	unsigned long m = timer2_millis;
	unsigned char f = timer2_fract;

	m += MILLIS_INC;
	f += FRACT_INC;
	if (f >= FRACT_MAX) {
		f -= FRACT_MAX;
		m += 1;
	}

	timer2_fract = f;
	timer2_millis = m;
	timer2_overflow_count++;
}

unsigned long millis()
{
	unsigned long m;
	uint8_t oldSREG = SREG;

	// disable interrupts while we read timer2_millis or we might get an
	// inconsistent value (e.g. in the middle of a write to timer2_millis)
	cli();
	m = timer2_millis;
	SREG = oldSREG;

	return m;
}

unsigned long micros() {
	unsigned long m;
	uint8_t oldSREG = SREG, t;
	
	cli();
	m = timer2_overflow_count;
#if defined(TCNT2)
	t = TCNT2;
#elif defined(TCNT2L)
	t = TCNT2L;
#else
	#error TIMER 2 not defined
#endif

  
#ifdef TIFR2
	if ((TIFR2 & _BV(TOV2)) && (t < 255))
		m++;
#else
	if ((TIFR & _BV(TOV2)) && (t < 255))
		m++;
#endif

	SREG = oldSREG;
	
	return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}

void delay(unsigned long ms)
{
	uint16_t start = (uint16_t)micros();

	while (ms > 0) {
		if (((uint16_t)micros() - start) >= 1000) {
			ms--;
			start += 1000;
		}
	}
}

/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
	// calling avrlib's delay_us() function with low values (e.g. 1 or
	// 2 microseconds) gives delays longer than desired.
	//delay_us(us);

#if F_CPU >= 16000000L
	// for the 16 MHz clock on most Arduino boards

	// for a one-microsecond delay, simply return.  the overhead
	// of the function call yields a delay of approximately 1 1/8 us.
	if (--us == 0)
		return;

	// the following loop takes a quarter of a microsecond (4 cycles)
	// per iteration, so execute it four times for each microsecond of
	// delay requested.
	us <<= 2;

	// account for the time taken in the preceeding commands.
	us -= 2;
#else
	// for the 8 MHz internal clock on the ATmega168

	// for a one- or two-microsecond delay, simply return.  the overhead of
	// the function calls takes more than two microseconds.  can't just
	// subtract two, since us is unsigned; we'd overflow.
	if (--us == 0)
		return;
	if (--us == 0)
		return;

	// the following loop takes half of a microsecond (4 cycles)
	// per iteration, so execute it twice for each microsecond of
	// delay requested.
	us <<= 1;
    
	// partially compensate for the time taken by the preceeding commands.
	// we can't subtract any more than this or we'd overflow w/ small delays.
	us--;
#endif

	// busy wait
	__asm__ __volatile__ (
		"1: sbiw %0,1" "\n\t" // 2 cycles
		"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
	);
}

void init()
{
	// this needs to be called before setup() or some functions won't
	// work there
	sei();
	
	// on the ATmega168, timer 2 is also used for fast hardware pwm
	// (using phase-correct PWM would mean that timer 2 overflowed half as often
	// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR2A) && defined(WGM21)
	sbi(TCCR2A, WGM21);
	sbi(TCCR2A, WGM20);
#endif  

	// set timer 2 prescale factor to 64
#if defined(__AVR_ATmega128__)
	// CPU specific: different values for the ATmega128
	sbi(TCCR2, CS22);
#elif defined(TCCR2) && defined(CS21) && defined(CS20)
	// this combination is for the standard atmega8
	sbi(TCCR2, CS21);
	sbi(TCCR2, CS20);
#elif defined(TCCR2B) && defined(CS21) && defined(CS20)
	// this combination is for the standard 168/328/1282/2562
	sbi(TCCR2B, CS21);
	sbi(TCCR2B, CS20);
#elif defined(TCCR2A) && defined(CS21) && defined(CS20)
	// this combination is for the __AVR_ATmega645__ series
	sbi(TCCR2A, CS21);
	sbi(TCCR2A, CS20);
#else
	#error Timer 2 prescale factor 64 not set correctly
#endif

	// enable timer 2 overflow interrupt
#if defined(TIMSK) && defined(TOIE2)
	sbi(TIMSK, TOIE2);
#elif defined(TIMSK2) && defined(TOIE2)
	sbi(TIMSK2, TOIE2);
#else
	#error	Timer 2 overflow interrupt not set correctly
#endif

	// timers 1 and 2 are used for phase-correct hardware pwm
	// this is better for motors as it ensures an even waveform
	// note, however, that fast pwm mode can achieve a frequency of up
	// 8 MHz (with a 16 MHz clock) at 52% duty cycle

	TCCR1B = 0;

	// set timer 1 prescale factor to 64
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
	sbi(TCCR1B, CS11);
	sbi(TCCR1B, CS10);
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
	sbi(TCCR1, CS11);
	sbi(TCCR1, CS10);
#endif
	// put timer 1 in 8-bit phase correct pwm mode
#if defined(TCCR1A) && defined(WGM10)
	sbi(TCCR1A, WGM10);
#elif defined(TCCR1)
	#warning this needs to be finished
#endif

	// set timer 2 prescale factor to 64
#if defined(TCCR2) && defined(CS22)
	sbi(TCCR2, CS22);
#elif defined(TCCR2B) && defined(CS22)
	sbi(TCCR2B, CS22);
#else
	#warning Timer 2 not finished (may not be present on this CPU)
#endif

	// configure timer 2 for phase correct pwm (8-bit)
#if defined(TCCR2) && defined(WGM20)
	sbi(TCCR2, WGM20);
#elif defined(TCCR2A) && defined(WGM20)
	sbi(TCCR2A, WGM20);
#else
	#warning Timer 2 not finished (may not be present on this CPU)
#endif

#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
	sbi(TCCR3B, CS31);		// set timer 3 prescale factor to 64
	sbi(TCCR3B, CS30);
	sbi(TCCR3A, WGM30);		// put timer 3 in 8-bit phase correct pwm mode
#endif
	
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
	sbi(TCCR4B, CS41);		// set timer 4 prescale factor to 64
	sbi(TCCR4B, CS40);
	sbi(TCCR4A, WGM40);		// put timer 4 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
	sbi(TCCR5B, CS51);		// set timer 5 prescale factor to 64
	sbi(TCCR5B, CS50);
	sbi(TCCR5A, WGM50);		// put timer 5 in 8-bit phase correct pwm mode
#endif

#if defined(ADCSRA)
	// set a2d prescale factor to 128
	// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
	// XXX: this will not work properly for other clock speeds, and
	// this code should use F_CPU to determine the prescale factor.
	sbi(ADCSRA, ADPS2);
	sbi(ADCSRA, ADPS1);
	sbi(ADCSRA, ADPS0);

	// enable a2d conversions
	sbi(ADCSRA, ADEN);
#endif

	// the bootloader connects pins 0 and 1 to the USART; disconnect them
	// here so they can be used as normal digital i/o; they will be
	// reconnected in Serial.begin()
#if defined(UCSRB)
	UCSRB = 0;
#elif defined(UCSR0B)
	UCSR0B = 0;
#endif
}

Moderator edit: quote tags converted to code tags (Nick Gammon)