Startup code for Arduino

From Wikiid
Jump to: navigation, search

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


Wikiid Pages relating to Arduino (edit)
Arduino
Command line Arduino
Startup code for Arduino
Low level functions for Arduino
Putting data in flash on the Arduino
External resources for Arduino
Board schematics for Arduino
Misc notes: Circuit notes, Music notes, Stepper motors