wiring_analog.c 7.2 KB

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