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_analog.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. wiring_analog.c - analog input and output
  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. Modified 28 September 2010 by Mark Sproul
  18. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  19. */
  20. #include "wiring_private.h"
  21. #include "pins_arduino.h"
  22. uint8_t analog_reference = DEFAULT;
  23. void analogReference(uint8_t mode)
  24. {
  25. // can't actually set the register here because the default setting
  26. // will connect AVCC and the AREF pin, which would cause a short if
  27. // there's something connected to AREF.
  28. analog_reference = mode;
  29. }
  30. int analogRead(uint8_t pin)
  31. {
  32. uint8_t low, high;
  33. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  34. if (pin >= 54) pin -= 54; // allow for channel or pin numbers
  35. #else
  36. if (pin >= 14) pin -= 14; // allow for channel or pin numbers
  37. #endif
  38. #if defined(ADCSRB) && defined(MUX5)
  39. // the MUX5 bit of ADCSRB selects whether we're reading from channels
  40. // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
  41. ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
  42. #endif
  43. // set the analog reference (high two bits of ADMUX) and select the
  44. // channel (low 4 bits). this also sets ADLAR (left-adjust result)
  45. // to 0 (the default).
  46. #if defined(ADMUX)
  47. ADMUX = (analog_reference << 6) | (pin & 0x07);
  48. #endif
  49. // without a delay, we seem to read from the wrong channel
  50. //delay(1);
  51. #if defined(ADCSRA) && defined(ADCL)
  52. // start the conversion
  53. sbi(ADCSRA, ADSC);
  54. // ADSC is cleared when the conversion finishes
  55. while (bit_is_set(ADCSRA, ADSC));
  56. // we have to read ADCL first; doing so locks both ADCL
  57. // and ADCH until ADCH is read. reading ADCL second would
  58. // cause the results of each conversion to be discarded,
  59. // as ADCL and ADCH would be locked when it completed.
  60. low = ADCL;
  61. high = ADCH;
  62. #else
  63. // we dont have an ADC, return 0
  64. low = 0;
  65. high = 0;
  66. #endif
  67. // combine the two bytes
  68. return (high << 8) | low;
  69. }
  70. // Right now, PWM output only works on the pins with
  71. // hardware support. These are defined in the appropriate
  72. // pins_*.c file. For the rest of the pins, we default
  73. // to digital output.
  74. void analogWrite(uint8_t pin, int val)
  75. {
  76. // We need to make sure the PWM output is enabled for those pins
  77. // that support it, as we turn it off when digitally reading or
  78. // writing with them. Also, make sure the pin is in output mode
  79. // for consistenty with Wiring, which doesn't require a pinMode
  80. // call for the analog output pins.
  81. pinMode(pin, OUTPUT);
  82. if (val == 0)
  83. {
  84. digitalWrite(pin, LOW);
  85. }
  86. else if (val == 255)
  87. {
  88. digitalWrite(pin, HIGH);
  89. }
  90. else
  91. {
  92. switch(digitalPinToTimer(pin))
  93. {
  94. // XXX fix needed for atmega8
  95. #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
  96. case TIMER0A:
  97. // connect pwm to pin on timer 0
  98. sbi(TCCR0, COM00);
  99. OCR0 = val; // set pwm duty
  100. break;
  101. #endif
  102. #if defined(TCCR0A) && defined(COM0A1)
  103. case TIMER0A:
  104. // connect pwm to pin on timer 0, channel A
  105. sbi(TCCR0A, COM0A1);
  106. OCR0A = val; // set pwm duty
  107. break;
  108. #endif
  109. #if defined(TCCR0A) && defined(COM0B1)
  110. case TIMER0B:
  111. // connect pwm to pin on timer 0, channel B
  112. sbi(TCCR0A, COM0B1);
  113. OCR0B = val; // set pwm duty
  114. break;
  115. #endif
  116. #if defined(TCCR1A) && defined(COM1A1)
  117. case TIMER1A:
  118. // connect pwm to pin on timer 1, channel A
  119. sbi(TCCR1A, COM1A1);
  120. OCR1A = val; // set pwm duty
  121. break;
  122. #endif
  123. #if defined(TCCR1A) && defined(COM1B1)
  124. case TIMER1B:
  125. // connect pwm to pin on timer 1, channel B
  126. sbi(TCCR1A, COM1B1);
  127. OCR1B = val; // set pwm duty
  128. break;
  129. #endif
  130. #if defined(TCCR2) && defined(COM21)
  131. case TIMER2:
  132. // connect pwm to pin on timer 2
  133. sbi(TCCR2, COM21);
  134. OCR2 = val; // set pwm duty
  135. break;
  136. #endif
  137. #if defined(TCCR2A) && defined(COM2A1)
  138. case TIMER2A:
  139. // connect pwm to pin on timer 2, channel A
  140. sbi(TCCR2A, COM2A1);
  141. OCR2A = val; // set pwm duty
  142. break;
  143. #endif
  144. #if defined(TCCR2A) && defined(COM2B1)
  145. case TIMER2B:
  146. // connect pwm to pin on timer 2, channel B
  147. sbi(TCCR2A, COM2B1);
  148. OCR2B = val; // set pwm duty
  149. break;
  150. #endif
  151. #if defined(TCCR3A) && defined(COM3A1)
  152. case TIMER3A:
  153. // connect pwm to pin on timer 3, channel A
  154. sbi(TCCR3A, COM3A1);
  155. OCR3A = val; // set pwm duty
  156. break;
  157. #endif
  158. #if defined(TCCR3A) && defined(COM3B1)
  159. case TIMER3B:
  160. // connect pwm to pin on timer 3, channel B
  161. sbi(TCCR3A, COM3B1);
  162. OCR3B = val; // set pwm duty
  163. break;
  164. #endif
  165. #if defined(TCCR3A) && defined(COM3C1)
  166. case TIMER3C:
  167. // connect pwm to pin on timer 3, channel C
  168. sbi(TCCR3A, COM3C1);
  169. OCR3C = val; // set pwm duty
  170. break;
  171. #endif
  172. #if defined(TCCR4A) && defined(COM4A1)
  173. case TIMER4A:
  174. // connect pwm to pin on timer 4, channel A
  175. sbi(TCCR4A, COM4A1);
  176. OCR4A = val; // set pwm duty
  177. break;
  178. #endif
  179. #if defined(TCCR4A) && defined(COM4B1)
  180. case TIMER4B:
  181. // connect pwm to pin on timer 4, channel B
  182. sbi(TCCR4A, COM4B1);
  183. OCR4B = val; // set pwm duty
  184. break;
  185. #endif
  186. #if defined(TCCR4A) && defined(COM4C1)
  187. case TIMER4C:
  188. // connect pwm to pin on timer 4, channel C
  189. sbi(TCCR4A, COM4C1);
  190. OCR4C = val; // set pwm duty
  191. break;
  192. #endif
  193. #if defined(TCCR5A) && defined(COM5A1)
  194. case TIMER5A:
  195. // connect pwm to pin on timer 5, channel A
  196. sbi(TCCR5A, COM5A1);
  197. OCR5A = val; // set pwm duty
  198. break;
  199. #endif
  200. #if defined(TCCR5A) && defined(COM5B1)
  201. case TIMER5B:
  202. // connect pwm to pin on timer 5, channel B
  203. sbi(TCCR5A, COM5B1);
  204. OCR5B = val; // set pwm duty
  205. break;
  206. #endif
  207. #if defined(TCCR5A) && defined(COM5C1)
  208. case TIMER5C:
  209. // connect pwm to pin on timer 5, channel C
  210. sbi(TCCR5A, COM5C1);
  211. OCR5C = val; // set pwm duty
  212. break;
  213. #endif
  214. case NOT_ON_TIMER:
  215. default:
  216. if (val < 128) {
  217. digitalWrite(pin, LOW);
  218. } else {
  219. digitalWrite(pin, HIGH);
  220. }
  221. }
  222. }
  223. }