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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  18. */
  19. #include "wiring_private.h"
  20. #include "pins_arduino.h"
  21. uint8_t analog_reference = DEFAULT;
  22. void analogReference(uint8_t mode)
  23. {
  24. // can't actually set the register here because the default setting
  25. // will connect AVCC and the AREF pin, which would cause a short if
  26. // there's something connected to AREF.
  27. analog_reference = mode;
  28. }
  29. int analogRead(uint8_t pin)
  30. {
  31. uint8_t low, high, ch = analogInPinToBit(pin);
  32. // set the analog reference (high two bits of ADMUX) and select the
  33. // channel (low 4 bits). this also sets ADLAR (left-adjust result)
  34. // to 0 (the default).
  35. // the final AND is to clear the pos/neg reference bits
  36. ADMUX = ((analog_reference << 6) | (pin & 0x0f)) & B11000111;
  37. // without a delay, we seem to read from the wrong channel
  38. //delay(1);
  39. // start the conversion
  40. sbi(ADCSRA, ADSC);
  41. // ADSC is cleared when the conversion finishes
  42. while (bit_is_set(ADCSRA, ADSC));
  43. // we have to read ADCL first; doing so locks both ADCL
  44. // and ADCH until ADCH is read. reading ADCL second would
  45. // cause the results of each conversion to be discarded,
  46. // as ADCL and ADCH would be locked when it completed.
  47. low = ADCL;
  48. high = ADCH;
  49. // combine the two bytes
  50. return (high << 8) | low;
  51. }
  52. // Right now, PWM output only works on the pins with
  53. // hardware support. These are defined in the appropriate
  54. // pins_*.c file. For the rest of the pins, we default
  55. // to digital output.
  56. void analogWrite(uint8_t pin, int val)
  57. {
  58. // We need to make sure the PWM output is enabled for those pins
  59. // that support it, as we turn it off when digitally reading or
  60. // writing with them. Also, make sure the pin is in output mode
  61. // for consistenty with Wiring, which doesn't require a pinMode
  62. // call for the analog output pins.
  63. pinMode(pin, OUTPUT);
  64. if (digitalPinToTimer(pin) == TIMER1A) {
  65. // connect pwm to pin on timer 1, channel A
  66. sbi(TCCR1A, COM1A1);
  67. // set pwm duty
  68. OCR1A = val;
  69. } else if (digitalPinToTimer(pin) == TIMER1B) {
  70. // connect pwm to pin on timer 1, channel B
  71. sbi(TCCR1A, COM1B1);
  72. // set pwm duty
  73. OCR1B = val;
  74. } else if (digitalPinToTimer(pin) == TIMER0A) {
  75. // connect pwm to pin on timer 0, channel A
  76. sbi(TCCR0A, COM0A1);
  77. // set pwm duty
  78. OCR0A = val;
  79. } else if (digitalPinToTimer(pin) == TIMER0B) {
  80. // connect pwm to pin on timer 0, channel B
  81. sbi(TCCR0A, COM0B1);
  82. // set pwm duty
  83. OCR0B = val;
  84. } else if (digitalPinToTimer(pin) == TIMER2A) {
  85. // connect pwm to pin on timer 2, channel A
  86. sbi(TCCR2A, COM2A1);
  87. // set pwm duty
  88. OCR2A = val;
  89. } else if (digitalPinToTimer(pin) == TIMER2B) {
  90. // connect pwm to pin on timer 2, channel B
  91. sbi(TCCR2A, COM2B1);
  92. // set pwm duty
  93. OCR2B = val;
  94. } else if (val < 128)
  95. //fail semi-intelligently
  96. digitalWrite(pin, LOW);
  97. else
  98. digitalWrite(pin, HIGH);
  99. }