Startup code for Arduino

From Wikiid
Revision as of 20:34, 14 December 2008 by SteveBaker (Talk | contribs) (New page: ''See also Arduino'' Here's how you programs must initialise themselves for Arduino. sei () ; // Set Enable Interrupts. timer0_overflow_count = 0; // set timer 0 prescale f...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

See also Arduino

Here's how you programs must initialise themselves for Arduino.

 sei () ;   // Set Enable Interrupts.
 timer0_overflow_count = 0;
 // set timer 0 prescale factor to 64
 // enable timer 0 overflow interrupt
 #if defined(__AVR_ATmega168__)
   sbi(TCCR0A, WGM01);
   sbi(TCCR0A, WGM00);
   sbi(TCCR0B, CS01);
   sbi(TCCR0B, CS00);
   sbi(TIMSK0, TOIE0);
 #else
   sbi(TCCR0, CS01);
   sbi(TCCR0, CS00);
   sbi(TIMSK, TOIE0);
 #endif
 // set timer 1 prescale factor to 64
 sbi(TCCR1B, CS11);
 sbi(TCCR1B, CS10);
 // put timer 1 in 8-bit phase correct pwm mode
 sbi(TCCR1A, WGM10);
 // set timer 2 prescale factor to 64
 #if defined(__AVR_ATmega168__)
   sbi(TCCR2B, CS22);
 #else
   sbi(TCCR2, CS22);
 #endif
 // configure timer 2 for phase correct pwm (8-bit)
 #if defined(__AVR_ATmega168__)
   sbi(TCCR2A, WGM20);
 #else
   sbi(TCCR2, WGM20);
 #endif
 // 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);
 // 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(__AVR_ATmega168__)
   UCSR0B = 0;
 #else
   UCSRB = 0;
 #endif