2
wiring.c - Partial implementation of the Wiring API for the ATmega8.
3
Part of Arduino - http://www.arduino.cc/
5
Copyright (c) 2005-2006 David A. Mellis
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Lesser General Public
9
License as published by the Free Software Foundation; either
10
version 2.1 of the License, or (at your option) any later version.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General
18
Public License along with this library; if not, write to the
19
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
Boston, MA 02111-1307 USA
25
#include "wiring_private.h"
28
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
29
// the overflow handler is called every 256 ticks.
30
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
32
// the whole number of milliseconds per timer0 overflow
33
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
35
// the fractional number of milliseconds per timer0 overflow. we shift right
36
// by three to fit these numbers into a byte. (for the clock speeds we care
37
// about - 8 and 16 MHz - this doesn't lose precision.)
38
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
39
#define FRACT_MAX (1000 >> 3)
41
volatile unsigned long timer0_overflow_count = 0;
42
volatile unsigned long timer0_millis = 0;
43
static unsigned char timer0_fract = 0;
45
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
48
SIGNAL(TIMER0_OVF_vect)
51
// copy these to local variables so they can be stored in registers
52
// (volatile variables must be read from memory on every access)
53
unsigned long m = timer0_millis;
54
unsigned char f = timer0_fract;
65
timer0_overflow_count++;
68
unsigned long millis(void)
71
uint8_t oldSREG = SREG;
73
// disable interrupts while we read timer0_millis or we might get an
74
// inconsistent value (e.g. in the middle of a write to timer0_millis)
82
unsigned long micros(void) {
84
uint8_t oldSREG = SREG, t;
87
m = timer0_overflow_count;
93
#error TIMER 0 not defined
98
if ((TIFR0 & _BV(TOV0)) && (t < 255))
101
if ((TIFR & _BV(TOV0)) && (t < 255))
107
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
110
void delay(unsigned long ms)
112
uint16_t start = (uint16_t)micros();
115
if (((uint16_t)micros() - start) >= 1000) {
122
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
123
void delayMicroseconds(unsigned int us)
125
// calling avrlib's delay_us() function with low values (e.g. 1 or
126
// 2 microseconds) gives delays longer than desired.
129
#if F_CPU >= 16000000L
130
// for the 16 MHz clock on most Arduino boards
132
// for a one-microsecond delay, simply return. the overhead
133
// of the function call yields a delay of approximately 1 1/8 us.
137
// the following loop takes a quarter of a microsecond (4 cycles)
138
// per iteration, so execute it four times for each microsecond of
142
// account for the time taken in the preceeding commands.
145
// for the 8 MHz internal clock on the ATmega168
147
// for a one- or two-microsecond delay, simply return. the overhead of
148
// the function calls takes more than two microseconds. can't just
149
// subtract two, since us is unsigned; we'd overflow.
155
// the following loop takes half of a microsecond (4 cycles)
156
// per iteration, so execute it twice for each microsecond of
160
// partially compensate for the time taken by the preceeding commands.
161
// we can't subtract any more than this or we'd overflow w/ small delays.
166
__asm__ __volatile__ (
167
"1: sbiw %0,1" "\n\t" // 2 cycles
168
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
172
void init_wiring_c(void)
174
// this needs to be called before setup() or some functions won't
178
// on the ATmega168, timer 0 is also used for fast hardware pwm
179
// (using phase-correct PWM would mean that timer 0 overflowed half as often
180
// resulting in different millis() behavior on the ATmega8 and ATmega168)
181
#if defined(TCCR0A) && defined(WGM01)
186
// set timer 0 prescale factor to 64
187
#if defined(__AVR_ATmega128__)
188
// CPU specific: different values for the ATmega128
190
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
191
// this combination is for the standard atmega8
194
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
195
// this combination is for the standard 168/328/1280/2560
198
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
199
// this combination is for the __AVR_ATmega645__ series
203
#error Timer 0 prescale factor 64 not set correctly
206
// enable timer 0 overflow interrupt
207
#if defined(TIMSK) && defined(TOIE0)
209
#elif defined(TIMSK0) && defined(TOIE0)
212
#error Timer 0 overflow interrupt not set correctly
215
// timers 1 and 2 are used for phase-correct hardware pwm
216
// this is better for motors as it ensures an even waveform
217
// note, however, that fast pwm mode can achieve a frequency of up
218
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
220
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
223
// set timer 1 prescale factor to 64
225
#if F_CPU >= 8000000L
228
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
230
#if F_CPU >= 8000000L
234
// put timer 1 in 8-bit phase correct pwm mode
235
#if defined(TCCR1A) && defined(WGM10)
238
#warning this needs to be finished
241
// set timer 2 prescale factor to 64
242
#if defined(TCCR2) && defined(CS22)
244
#elif defined(TCCR2B) && defined(CS22)
247
#warning Timer 2 not finished (may not be present on this CPU)
250
// configure timer 2 for phase correct pwm (8-bit)
251
#if defined(TCCR2) && defined(WGM20)
253
#elif defined(TCCR2A) && defined(WGM20)
256
#warning Timer 2 not finished (may not be present on this CPU)
259
#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
260
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
262
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
265
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
266
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
268
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
271
#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
272
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
274
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
278
// set a2d prescale factor to 128
279
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
280
// XXX: this will not work properly for other clock speeds, and
281
// this code should use F_CPU to determine the prescale factor.
286
// enable a2d conversions
290
// the bootloader connects pins 0 and 1 to the USART; disconnect them
291
// here so they can be used as normal digital i/o; they will be
292
// reconnected in Serial.begin()
295
#elif defined(UCSR0B)