Difference between revisions of "Startup code for Arduino"

From Wikiid
Jump to: navigation, search
 
Line 2: Line 2:
  
 
   sei () ;  // Set Enable Interrupts.
 
   sei () ;  // Set Enable Interrupts.
 
 
   timer0_overflow_count = 0;
 
   timer0_overflow_count = 0;
 
 
   // set timer 0 prescale factor to 64
 
   // set timer 0 prescale factor to 64
 
   // enable timer 0 overflow interrupt
 
   // enable timer 0 overflow interrupt
 
 
   #if defined(__AVR_ATmega168__)
 
   #if defined(__AVR_ATmega168__)
 
     sbi(TCCR0A, WGM01);
 
     sbi(TCCR0A, WGM01);
Line 19: Line 16:
 
     sbi(TIMSK, TOIE0);
 
     sbi(TIMSK, TOIE0);
 
   #endif
 
   #endif
 
 
   // set timer 1 prescale factor to 64
 
   // set timer 1 prescale factor to 64
 
   sbi(TCCR1B, CS11);
 
   sbi(TCCR1B, CS11);
 
   sbi(TCCR1B, CS10);
 
   sbi(TCCR1B, CS10);
 
 
   // put timer 1 in 8-bit phase correct pwm mode
 
   // put timer 1 in 8-bit phase correct pwm mode
 
   sbi(TCCR1A, WGM10);
 
   sbi(TCCR1A, WGM10);
 
 
   // set timer 2 prescale factor to 64
 
   // set timer 2 prescale factor to 64
 
   #if defined(__AVR_ATmega168__)
 
   #if defined(__AVR_ATmega168__)
Line 33: Line 27:
 
     sbi(TCCR2, CS22);
 
     sbi(TCCR2, CS22);
 
   #endif
 
   #endif
 
 
   // configure timer 2 for phase correct pwm (8-bit)
 
   // configure timer 2 for phase correct pwm (8-bit)
 
   #if defined(__AVR_ATmega168__)
 
   #if defined(__AVR_ATmega168__)
Line 40: Line 33:
 
     sbi(TCCR2, WGM20);
 
     sbi(TCCR2, WGM20);
 
   #endif
 
   #endif
 
 
   // set a2d prescale factor to 128
 
   // set a2d prescale factor to 128
 
   // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
 
   // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
Line 48: Line 40:
 
   sbi(ADCSRA, ADPS1);
 
   sbi(ADCSRA, ADPS1);
 
   sbi(ADCSRA, ADPS0);
 
   sbi(ADCSRA, ADPS0);
 
 
   // enable a2d conversions
 
   // enable a2d conversions
 
   sbi(ADCSRA, ADEN);
 
   sbi(ADCSRA, ADEN);
 
 
   // the bootloader connects pins 0 and 1 to the USART; disconnect them
 
   // 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
 
   // here so they can be used as normal digital i/o; they will be

Latest revision as of 15:16, 1 January 2009

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