My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiring.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 388 2008-03-08 22:05:23Z mellis $
  18. */
  19. #include "wiring_private.h"
  20. volatile unsigned long timer0_millis = 0;
  21. SIGNAL(TIMER0_OVF_vect)
  22. {
  23. // timer 0 prescale factor is 64 and the timer overflows at 256
  24. timer0_millis++;
  25. }
  26. unsigned long millis()
  27. {
  28. unsigned long m;
  29. uint8_t oldSREG = SREG;
  30. // disable interrupts while we read timer0_millis or we might get an
  31. // inconsistent value (e.g. in the middle of the timer0_millis++)
  32. cli();
  33. m = timer0_millis;
  34. SREG = oldSREG;
  35. return m;
  36. }
  37. void delay(unsigned long ms)
  38. {
  39. unsigned long start = millis();
  40. while (millis() - start <= ms)
  41. ;
  42. }
  43. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock.
  44. * Disables interrupts, which will disrupt the millis() function if used
  45. * too frequently. */
  46. void delayMicroseconds(unsigned int us)
  47. {
  48. uint8_t oldSREG;
  49. // calling avrlib's delay_us() function with low values (e.g. 1 or
  50. // 2 microseconds) gives delays longer than desired.
  51. //delay_us(us);
  52. #if F_CPU >= 16000000L
  53. // for the 16 MHz clock on most Arduino boards
  54. // for a one-microsecond delay, simply return. the overhead
  55. // of the function call yields a delay of approximately 1 1/8 us.
  56. if (--us == 0)
  57. return;
  58. // the following loop takes a quarter of a microsecond (4 cycles)
  59. // per iteration, so execute it four times for each microsecond of
  60. // delay requested.
  61. us <<= 2;
  62. // account for the time taken in the preceeding commands.
  63. us -= 2;
  64. #else
  65. // for the 8 MHz internal clock on the ATmega168
  66. // for a one- or two-microsecond delay, simply return. the overhead of
  67. // the function calls takes more than two microseconds. can't just
  68. // subtract two, since us is unsigned; we'd overflow.
  69. if (--us == 0)
  70. return;
  71. if (--us == 0)
  72. return;
  73. // the following loop takes half of a microsecond (4 cycles)
  74. // per iteration, so execute it twice for each microsecond of
  75. // delay requested.
  76. us <<= 1;
  77. // partially compensate for the time taken by the preceeding commands.
  78. // we can't subtract any more than this or we'd overflow w/ small delays.
  79. us--;
  80. #endif
  81. // disable interrupts, otherwise the timer 0 overflow interrupt that
  82. // tracks milliseconds will make us delay longer than we want.
  83. oldSREG = SREG;
  84. cli();
  85. // busy wait
  86. __asm__ __volatile__ (
  87. "1: sbiw %0,1" "\n\t" // 2 cycles
  88. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  89. );
  90. // reenable interrupts.
  91. SREG = oldSREG;
  92. }
  93. void init()
  94. {
  95. // this needs to be called before setup() or some functions won't
  96. // work there
  97. sei();
  98. // on the ATmega168, timer 0 is also used for fast hardware pwm
  99. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  100. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  101. sbi(TCCR0A, WGM01);
  102. sbi(TCCR0A, WGM00);
  103. // set timer 0 prescale factor to 64
  104. sbi(TCCR0B, CS01);
  105. sbi(TCCR0B, CS00);
  106. // enable timer 0 overflow interrupt
  107. sbi(TIMSK0, TOIE0);
  108. // timers 1 and 2 are used for phase-correct hardware pwm
  109. // this is better for motors as it ensures an even waveform
  110. // note, however, that fast pwm mode can achieve a frequency of up
  111. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  112. #if 0
  113. // set timer 1 prescale factor to 64
  114. sbi(TCCR1B, CS11);
  115. sbi(TCCR1B, CS10);
  116. // put timer 1 in 8-bit phase correct pwm mode
  117. sbi(TCCR1A, WGM10);
  118. // set timer 2 prescale factor to 64
  119. sbi(TCCR2B, CS22);
  120. // configure timer 2 for phase correct pwm (8-bit)
  121. sbi(TCCR2A, WGM20);
  122. // set a2d prescale factor to 128
  123. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  124. // XXX: this will not work properly for other clock speeds, and
  125. // this code should use F_CPU to determine the prescale factor.
  126. sbi(ADCSRA, ADPS2);
  127. sbi(ADCSRA, ADPS1);
  128. sbi(ADCSRA, ADPS0);
  129. // enable a2d conversions
  130. sbi(ADCSRA, ADEN);
  131. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  132. // here so they can be used as normal digital i/o; they will be
  133. // reconnected in Serial.begin()
  134. UCSR0B = 0;
  135. #if defined(__AVR_ATmega644P__)
  136. //TODO: test to see if disabling this helps?
  137. //UCSR1B = 0;
  138. #endif
  139. #endif
  140. }