wiring.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. wiring.c - Partial implementation of the Wiring API for the ATmega8.
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. $Id: wiring.c 808 2009-12-18 17:44:08Z dmellis $
  18. */
  19. #include "wiring_private.h"
  20. // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
  21. // the overflow handler is called every 256 ticks.
  22. #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
  23. // the whole number of milliseconds per timer0 overflow
  24. #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
  25. // the fractional number of milliseconds per timer0 overflow. we shift right
  26. // by three to fit these numbers into a byte. (for the clock speeds we care
  27. // about - 8 and 16 MHz - this doesn't lose precision.)
  28. #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
  29. #define FRACT_MAX (1000 >> 3)
  30. volatile unsigned long timer0_overflow_count = 0;
  31. volatile unsigned long timer0_millis = 0;
  32. static unsigned char timer0_fract = 0;
  33. SIGNAL(TIMER0_OVF_vect)
  34. {
  35. // copy these to local variables so they can be stored in registers
  36. // (volatile variables must be read from memory on every access)
  37. unsigned long m = timer0_millis;
  38. unsigned char f = timer0_fract;
  39. m += MILLIS_INC;
  40. f += FRACT_INC;
  41. if (f >= FRACT_MAX) {
  42. f -= FRACT_MAX;
  43. m += 1;
  44. }
  45. timer0_fract = f;
  46. timer0_millis = m;
  47. timer0_overflow_count++;
  48. }
  49. unsigned long millis()
  50. {
  51. unsigned long m;
  52. uint8_t oldSREG = SREG;
  53. // disable interrupts while we read timer0_millis or we might get an
  54. // inconsistent value (e.g. in the middle of a write to timer0_millis)
  55. cli();
  56. m = timer0_millis;
  57. SREG = oldSREG;
  58. return m;
  59. }
  60. unsigned long micros() {
  61. unsigned long m;
  62. uint8_t oldSREG = SREG, t;
  63. cli();
  64. m = timer0_overflow_count;
  65. t = TCNT0;
  66. #ifdef TIFR0
  67. if ((TIFR0 & _BV(TOV0)) && (t < 255))
  68. m++;
  69. #else
  70. if ((TIFR & _BV(TOV0)) && (t < 255))
  71. m++;
  72. #endif
  73. SREG = oldSREG;
  74. return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
  75. }
  76. void delay(unsigned long ms)
  77. {
  78. unsigned long start = millis();
  79. while (millis() - start <= ms)
  80. ;
  81. }
  82. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
  83. void delayMicroseconds(unsigned int us)
  84. {
  85. // calling avrlib's delay_us() function with low values (e.g. 1 or
  86. // 2 microseconds) gives delays longer than desired.
  87. //delay_us(us);
  88. #if F_CPU >= 16000000L
  89. // for the 16 MHz clock on most Arduino boards
  90. // for a one-microsecond delay, simply return. the overhead
  91. // of the function call yields a delay of approximately 1 1/8 us.
  92. if (--us == 0)
  93. return;
  94. // the following loop takes a quarter of a microsecond (4 cycles)
  95. // per iteration, so execute it four times for each microsecond of
  96. // delay requested.
  97. us <<= 2;
  98. // account for the time taken in the preceeding commands.
  99. us -= 2;
  100. #else
  101. // for the 8 MHz internal clock on the ATmega168
  102. // for a one- or two-microsecond delay, simply return. the overhead of
  103. // the function calls takes more than two microseconds. can't just
  104. // subtract two, since us is unsigned; we'd overflow.
  105. if (--us == 0)
  106. return;
  107. if (--us == 0)
  108. return;
  109. // the following loop takes half of a microsecond (4 cycles)
  110. // per iteration, so execute it twice for each microsecond of
  111. // delay requested.
  112. us <<= 1;
  113. // partially compensate for the time taken by the preceeding commands.
  114. // we can't subtract any more than this or we'd overflow w/ small delays.
  115. us--;
  116. #endif
  117. // busy wait
  118. __asm__ __volatile__ (
  119. "1: sbiw %0,1" "\n\t" // 2 cycles
  120. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  121. );
  122. }
  123. void init()
  124. {
  125. // this needs to be called before setup() or some functions won't
  126. // work there
  127. sei();
  128. // on the ATmega168, timer 0 is also used for fast hardware pwm
  129. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  130. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  131. #if !defined(__AVR_ATmega8__)
  132. sbi(TCCR0A, WGM01);
  133. sbi(TCCR0A, WGM00);
  134. #endif
  135. // set timer 0 prescale factor to 64
  136. #if defined(__AVR_ATmega8__)
  137. sbi(TCCR0, CS01);
  138. sbi(TCCR0, CS00);
  139. #else
  140. sbi(TCCR0B, CS01);
  141. sbi(TCCR0B, CS00);
  142. #endif
  143. // enable timer 0 overflow interrupt
  144. #if defined(__AVR_ATmega8__)
  145. sbi(TIMSK, TOIE0);
  146. #else
  147. sbi(TIMSK0, TOIE0);
  148. #endif
  149. // timers 1 and 2 are used for phase-correct hardware pwm
  150. // this is better for motors as it ensures an even waveform
  151. // note, however, that fast pwm mode can achieve a frequency of up
  152. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  153. // set timer 1 prescale factor to 64
  154. sbi(TCCR1B, CS11);
  155. sbi(TCCR1B, CS10);
  156. // put timer 1 in 8-bit phase correct pwm mode
  157. sbi(TCCR1A, WGM10);
  158. // set timer 2 prescale factor to 64
  159. #if defined(__AVR_ATmega8__)
  160. sbi(TCCR2, CS22);
  161. #else
  162. sbi(TCCR2B, CS22);
  163. #endif
  164. // configure timer 2 for phase correct pwm (8-bit)
  165. #if defined(__AVR_ATmega8__)
  166. sbi(TCCR2, WGM20);
  167. #else
  168. sbi(TCCR2A, WGM20);
  169. #endif
  170. #if defined(__AVR_ATmega1280__)
  171. // set timer 3, 4, 5 prescale factor to 64
  172. sbi(TCCR3B, CS31); sbi(TCCR3B, CS30);
  173. sbi(TCCR4B, CS41); sbi(TCCR4B, CS40);
  174. sbi(TCCR5B, CS51); sbi(TCCR5B, CS50);
  175. // put timer 3, 4, 5 in 8-bit phase correct pwm mode
  176. sbi(TCCR3A, WGM30);
  177. sbi(TCCR4A, WGM40);
  178. sbi(TCCR5A, WGM50);
  179. #endif
  180. // set a2d prescale factor to 128
  181. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  182. // XXX: this will not work properly for other clock speeds, and
  183. // this code should use F_CPU to determine the prescale factor.
  184. sbi(ADCSRA, ADPS2);
  185. sbi(ADCSRA, ADPS1);
  186. sbi(ADCSRA, ADPS0);
  187. // enable a2d conversions
  188. sbi(ADCSRA, ADEN);
  189. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  190. // here so they can be used as normal digital i/o; they will be
  191. // reconnected in Serial.begin()
  192. #if defined(__AVR_ATmega8__)
  193. UCSRB = 0;
  194. #else
  195. UCSR0B = 0;
  196. #endif
  197. }