Click here to Skip to main content
15,886,258 members
Articles / Internet of Things / Arduino

Arduino Unleashed

Rate me:
Please Sign up or sign in to vote.
4.90/5 (19 votes)
18 Sep 2010CPOL8 min read 171.2K   678   40  
This article is all about empowering you with more powerful tools that let you unleash your creativity without being limited by the default IDE.
  • arduino.zip
    • Arduino
      • .metadata
        • .lock
        • .log
        • .mylyn
          • contexts
          • repositories.xml.zip
        • .plugins
          • de.innot.avreclipse.core
          • org.eclipse.cdt.core
          • org.eclipse.cdt.make.core
          • org.eclipse.cdt.managedbuilder.core
          • org.eclipse.cdt.ui
          • org.eclipse.core.resources
            • .history
              • 0
                • 90178f64a8c1001f13c7f5de4bcffe65
              • 11
                • 90c8cb7aaec1001f1a968c8925716fac
              • 15
                • e003e79caec1001f1a968c8925716fac
              • 16
                • 309eaa2aaac1001f1a968c8925716fac
                • b00fc1e8a7c1001f13c7f5de4bcffe65
              • 17
                • 303f0b1aaac1001f1a968c8925716fac
              • 19
                • 50db879ba9c1001f13c7f5de4bcffe65
                • b0822d6baac1001f1a968c8925716fac
              • 1a
                • a0d01b72aec1001f1a968c8925716fac
              • 1d
                • 9006869ba9c1001f13c7f5de4bcffe65
              • 2
                • 40805c37b3c1001f1a968c8925716fac
              • 24
                • 908eddbfafc1001f1a968c8925716fac
              • 25
                • 60a2bb77aec1001f1a968c8925716fac
              • 26
                • 709c0a3eadc1001f1a968c8925716fac
              • 29
                • b0563bbbafc1001f1a968c8925716fac
              • 2a
                • 50b68fbfa7c1001f13c7f5de4bcffe65
              • 2f
                • 609fb36eaec1001f1a968c8925716fac
              • 3
                • 00b2433ea6c1001f13c7f5de4bcffe65
              • 30
                • e0862b6baac1001f1a968c8925716fac
              • 31
                • e0f5edd5a9c1001f13c7f5de4bcffe65
              • 32
                • 40a6c07eaac1001f1a968c8925716fac
              • 33
                • 7066e74ab3c1001f1a968c8925716fac
                • 80265848a7c1001f13c7f5de4bcffe65
              • 34
                • 00eb51d6adc1001f1a968c8925716fac
              • 35
                • d05a5b48a7c1001f13c7f5de4bcffe65
                • e046fe19aac1001f1a968c8925716fac
              • 36
                • 20ed7c6ba8c1001f13c7f5de4bcffe65
              • 37
                • f03b866ba8c1001f13c7f5de4bcffe65
              • 39
                • 507dde20a9c1001f13c7f5de4bcffe65
              • 3b
                • a0b1ce97aec1001f1a968c8925716fac
              • 3d
                • 0063ef74aec1001f1a968c8925716fac
              • 3e
                • e0191572aec1001f1a968c8925716fac
              • 42
                • 80903869aac1001f1a968c8925716fac
              • 45
                • 10c140a1aec1001f1a968c8925716fac
              • 47
                • 3073473caec1001f1a968c8925716fac
              • 48
                • c0b85d37b3c1001f1a968c8925716fac
              • 4f
                • 70572f6baac1001f1a968c8925716fac
              • 50
                • 30083988b3c1001f1a968c8925716fac
              • 51
                • d0723269a6c1001f13c7f5de4bcffe65
              • 52
                • 902b1441b0c1001f1a968c8925716fac
              • 54
                • d06a4b3caec1001f1a968c8925716fac
              • 56
                • 002c6d00aac1001f13c7f5de4bcffe65
                • b00a8fbdafc1001f1a968c8925716fac
                • b09bbdc9a7c1001f13c7f5de4bcffe65
              • 57
                • b0f54422aac1001f1a968c8925716fac
              • 58
                • d077b7b2afc1001f1a968c8925716fac
              • 5a
                • d05d819faec1001f1a968c8925716fac
              • 5b
                • b0f4b977aec1001f1a968c8925716fac
              • 5c
                • 007bc27eaac1001f1a968c8925716fac
              • 5d
                • 40e5c897aec1001f1a968c8925716fac
                • 608adfbfafc1001f1a968c8925716fac
                • d013899ba9c1001f13c7f5de4bcffe65
              • 5e
                • c0b1af2db3c1001f1a968c8925716fac
              • 5f
                • 3048ecd5a9c1001f13c7f5de4bcffe65
              • 61
                • 404b5cd6adc1001f1a968c8925716fac
              • 67
                • 20740441b0c1001f1a968c8925716fac
              • 68
                • 20aee081aec1001f1a968c8925716fac
              • 6a
                • f0ee1a37a9c1001f13c7f5de4bcffe65
              • 6b
                • d06be49caec1001f1a968c8925716fac
              • 6c
                • 10595034b0c1001f1a968c8925716fac
              • 7
                • d076c47eaac1001f1a968c8925716fac
              • 70
                • 10503ea1aec1001f1a968c8925716fac
                • 30b3ee79a7c1001f13c7f5de4bcffe65
                • e06d6369a6c1001f13c7f5de4bcffe65
              • 71
                • f06556d4a9c1001f13c7f5de4bcffe65
              • 72
                • 40a2011aaac1001f1a968c8925716fac
              • 74
                • 00145534b0c1001f1a968c8925716fac
                • 70add097aec1001f1a968c8925716fac
              • 77
                • 50a0ae2db3c1001f1a968c8925716fac
                • 70271c37a9c1001f13c7f5de4bcffe65
              • 79
                • 20b07f9faec1001f1a968c8925716fac
              • 7a
                • 400092bfa7c1001f13c7f5de4bcffe65
                • 8051a52db3c1001f1a968c8925716fac
              • 7b
                • 703298bdafc1001f1a968c8925716fac
              • 7d
                • 006f493caec1001f1a968c8925716fac
                • 0087c57aaec1001f1a968c8925716fac
                • f063093eadc1001f1a968c8925716fac
              • 7f
                • d033043eadc1001f1a968c8925716fac
              • 80
                • a038c7c9a7c1001f13c7f5de4bcffe65
                • f03e3e22aac1001f1a968c8925716fac
              • 81
                • 402ade9caec1001f1a968c8925716fac
                • 809b876ba8c1001f13c7f5de4bcffe65
              • 84
                • 30698b4ea8c1001f13c7f5de4bcffe65
              • 85
                • 90b7c839aec1001f1a968c8925716fac
              • 87
                • 2040ed74aec1001f1a968c8925716fac
              • 8a
                • 004b6169a6c1001f13c7f5de4bcffe65
                • 60a34622aac1001f1a968c8925716fac
              • 8c
                • 70f2e59caec1001f1a968c8925716fac
              • 8d
                • c02f1241b0c1001f1a968c8925716fac
              • 8f
                • a05b95bfa7c1001f13c7f5de4bcffe65
                • e082e281aec1001f1a968c8925716fac
              • 93
                • 602ce681aec1001f1a968c8925716fac
              • 98
                • 5096829faec1001f1a968c8925716fac
              • 99
                • 309f4822aac1001f1a968c8925716fac
                • b0b13c88b3c1001f1a968c8925716fac
              • a
                • 50b47d9faec1001f1a968c8925716fac
              • a4
                • f04b5937b3c1001f1a968c8925716fac
              • a7
                • b077a4e3a7c1001f13c7f5de4bcffe65
                • c015c5c9a7c1001f13c7f5de4bcffe65
              • aa
                • 70c9a82aaac1001f1a968c8925716fac
              • ab
                • 40411937a9c1001f13c7f5de4bcffe65
                • 60a56b00aac1001f13c7f5de4bcffe65
              • ad
                • 30ea3d88b3c1001f1a968c8925716fac
                • c0125bd6adc1001f1a968c8925716fac
              • ae
                • 9088be77aec1001f1a968c8925716fac
                • b0205b37b3c1001f1a968c8925716fac
              • af
                • d02d5234b0c1001f1a968c8925716fac
              • b
                • 007b4155aac1001f1a968c8925716fac
              • b0
                • 10ec9ae1a7c1001f13c7f5de4bcffe65
              • b2
                • 00a8ed4ab3c1001f1a968c8925716fac
              • b4
                • 6066b6b2afc1001f1a968c8925716fac
              • b5
                • 00caa2e3a7c1001f13c7f5de4bcffe65
              • b9
                • 70880d57aac1001f1a968c8925716fac
              • bb
                • 00e5fa3dadc1001f1a968c8925716fac
                • 10ec7f9ba9c1001f13c7f5de4bcffe65
              • bc
                • 0062bfe8a7c1001f13c7f5de4bcffe65
                • 2077bd77aec1001f1a968c8925716fac
              • c1
                • b04742a1aec1001f1a968c8925716fac
                • e0480641b0c1001f1a968c8925716fac
              • c2
                • 1052e020a9c1001f13c7f5de4bcffe65
                • d008d497aec1001f1a968c8925716fac
              • c5
                • 10fc93bfa7c1001f13c7f5de4bcffe65
              • c6
                • 207294e3a7c1001f13c7f5de4bcffe65
              • c7
                • 40840f57aac1001f1a968c8925716fac
              • c8
                • 900a53d4a9c1001f13c7f5de4bcffe65
                • c0bc1057aac1001f1a968c8925716fac
              • c9
                • 40a743a1aec1001f1a968c8925716fac
                • d0cce481aec1001f1a968c8925716fac
              • ca
                • 3066bde8a7c1001f13c7f5de4bcffe65
              • cb
                • 908ae120a9c1001f13c7f5de4bcffe65
              • cc
                • c02d0a1aaac1001f1a968c8925716fac
                • f08df0d5a9c1001f13c7f5de4bcffe65
              • d0
                • 2085eb4ab3c1001f1a968c8925716fac
              • d3
                • 000c5248a7c1001f13c7f5de4bcffe65
              • d5
                • a08c0b57aac1001f1a968c8925716fac
              • d6
                • 80e0ee4ab3c1001f1a968c8925716fac
              • d8
                • 20d0e3bfafc1001f1a968c8925716fac
              • d9
                • b0f4a62aaac1001f1a968c8925716fac
                • f06d2b69a6c1001f13c7f5de4bcffe65
              • dc
                • c0aece7aaec1001f1a968c8925716fac
              • e0
                • 80db5334b0c1001f1a968c8925716fac
              • e1
                • 00c31337a9c1001f13c7f5de4bcffe65
              • e3
                • 8081dc20a9c1001f13c7f5de4bcffe65
              • e5
                • 507e1d72aec1001f1a968c8925716fac
              • e6
                • 704b50d4a9c1001f13c7f5de4bcffe65
              • e7
                • 106559d6adc1001f1a968c8925716fac
              • e8
                • c0006f00aac1001f13c7f5de4bcffe65
                • c037f174aec1001f1a968c8925716fac
              • e9
                • a091b4b2afc1001f1a968c8925716fac
              • ea
                • 408e846ba8c1001f13c7f5de4bcffe65
              • eb
                • c08496bdafc1001f1a968c8925716fac
              • ec
                • 702d55d4a9c1001f13c7f5de4bcffe65
              • f1
                • b019ad2db3c1001f1a968c8925716fac
                • f0dc3a88b3c1001f1a968c8925716fac
              • f4
                • 50225a48a7c1001f13c7f5de4bcffe65
              • f7
                • 4076cd7aaec1001f1a968c8925716fac
              • f8
                • b0d6ab2aaac1001f1a968c8925716fac
              • fb
                • 10fda6b2afc1001f1a968c8925716fac
            • .projects
              • Arduino
                • .indexes
                  • 19
                    • history.index
                  • 33
                    • history.index
                  • af
                    • history.index
                  • history.index
                  • properties.index
                • .markers
              • BlinkenLights
                • .indexes
                  • 19
                    • history.index
                  • 33
                    • history.index
                  • af
                    • history.index
                  • history.index
                  • properties.index
            • .root
              • .indexes
                • history.version
                • properties.index
                • properties.version
              • 3.tree
            • .safetable
              • org.eclipse.core.resources
          • org.eclipse.core.runtime
          • org.eclipse.debug.core
          • org.eclipse.debug.ui
          • org.eclipse.epp.usagedata.recording
          • org.eclipse.equinox.p2.ui
          • org.eclipse.ltk.core.refactoring
          • org.eclipse.mylyn.bugzilla.core
          • org.eclipse.mylyn.tasks.ui
          • org.eclipse.team.cvs.core
          • org.eclipse.ui.intro
          • org.eclipse.ui.workbench
        • version.ini
      • Arduino
      • BlinkenLights
/*
  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: wiring.c 970 2010-05-25 20:16:15Z dmellis $
*/

#include "wiring_private.h"

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

// the whole number of milliseconds per timer0 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)

// the fractional number of milliseconds per timer0 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_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)

volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

SIGNAL(TIMER0_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 = timer0_millis;
	unsigned char f = timer0_fract;

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

	timer0_fract = f;
	timer0_millis = m;
	timer0_overflow_count++;
}

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

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

	return m;
}

unsigned long micros() {
	unsigned long m;
	uint8_t oldSREG = SREG, t;
	
	cli();
	m = timer0_overflow_count;
	t = TCNT0;
  
#ifdef TIFR0
	if ((TIFR0 & _BV(TOV0)) && (t < 255))
		m++;
#else
	if ((TIFR & _BV(TOV0)) && (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 0 is also used for fast hardware pwm
	// (using phase-correct PWM would mean that timer 0 overflowed half as often
	// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if !defined(__AVR_ATmega8__)
	sbi(TCCR0A, WGM01);
	sbi(TCCR0A, WGM00);
#endif  
	// set timer 0 prescale factor to 64
#if defined(__AVR_ATmega8__)
	sbi(TCCR0, CS01);
	sbi(TCCR0, CS00);
#else
	sbi(TCCR0B, CS01);
	sbi(TCCR0B, CS00);
#endif
	// enable timer 0 overflow interrupt
#if defined(__AVR_ATmega8__)
	sbi(TIMSK, TOIE0);
#else
	sbi(TIMSK0, TOIE0);
#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

	// 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_ATmega8__)
	sbi(TCCR2, CS22);
#else
	sbi(TCCR2B, CS22);
#endif
	// configure timer 2 for phase correct pwm (8-bit)
#if defined(__AVR_ATmega8__)
	sbi(TCCR2, WGM20);
#else
	sbi(TCCR2A, WGM20);
#endif

#if defined(__AVR_ATmega1280__)
	// set timer 3, 4, 5 prescale factor to 64
	sbi(TCCR3B, CS31);	sbi(TCCR3B, CS30);
	sbi(TCCR4B, CS41);	sbi(TCCR4B, CS40);
	sbi(TCCR5B, CS51);	sbi(TCCR5B, CS50);
	// put timer 3, 4, 5 in 8-bit phase correct pwm mode
	sbi(TCCR3A, WGM30);
	sbi(TCCR4A, WGM40);
	sbi(TCCR5A, WGM50);
#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_ATmega8__)
	UCSRB = 0;
#else
	UCSR0B = 0;
#endif
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NVIDIA India
India India
I am a software engineer by profession and an electronics engineer by education. Used to be (and still am) a hobby programmer, an electronics enthusiast/DIYer etc.

Some areas which interest me are...compiler construction, operating systems, robotics, science in general etc

Comments and Discussions