My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

wiring_digital.c 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. wiring_digital.c - digital input and output functions
  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. void pinMode(uint8_t pin, uint8_t mode)
  22. {
  23. uint8_t bit = digitalPinToBitMask(pin);
  24. uint8_t port = digitalPinToPort(pin);
  25. volatile uint8_t *reg;
  26. if (port == NOT_A_PIN) return;
  27. // JWS: can I let the optimizer do this?
  28. reg = portModeRegister(port);
  29. if (mode == INPUT) *reg &= ~bit;
  30. else *reg |= bit;
  31. }
  32. // Forcing this inline keeps the callers from having to push their own stuff
  33. // on the stack. It is a good performance win and only takes 1 more byte per
  34. // user than calling. (It will take more bytes on the 168.)
  35. //
  36. // But shouldn't this be moved into pinMode? Seems silly to check and do on
  37. // each digitalread or write.
  38. //
  39. static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
  40. static inline void turnOffPWM(uint8_t timer)
  41. {
  42. if (timer == TIMER0A) cbi(TCCR0A, COM0A1);
  43. if (timer == TIMER0B) cbi(TCCR0A, COM0B1);
  44. if (timer == TIMER1A) cbi(TCCR1A, COM1A1);
  45. if (timer == TIMER1B) cbi(TCCR1A, COM1B1);
  46. if (timer == TIMER2A) cbi(TCCR2A, COM2A1);
  47. if (timer == TIMER2B) cbi(TCCR2A, COM2B1);
  48. }
  49. void digitalWrite(uint8_t pin, uint8_t val)
  50. {
  51. uint8_t timer = digitalPinToTimer(pin);
  52. uint8_t bit = digitalPinToBitMask(pin);
  53. uint8_t port = digitalPinToPort(pin);
  54. volatile uint8_t *out;
  55. if (port == NOT_A_PIN) return;
  56. // If the pin that support PWM output, we need to turn it off
  57. // before doing a digital write.
  58. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  59. out = portOutputRegister(port);
  60. if (val == LOW) *out &= ~bit;
  61. else *out |= bit;
  62. }
  63. int digitalRead(uint8_t pin)
  64. {
  65. uint8_t timer = digitalPinToTimer(pin);
  66. uint8_t bit = digitalPinToBitMask(pin);
  67. uint8_t port = digitalPinToPort(pin);
  68. if (port == NOT_A_PIN) return LOW;
  69. // If the pin that support PWM output, we need to turn it off
  70. // before getting a digital reading.
  71. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  72. if (*portInputRegister(port) & bit) return HIGH;
  73. return LOW;
  74. }