wiring_digital.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Modified 28 September 2010 by Mark Sproul
  18. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  19. */
  20. #define ARDUINO_MAIN
  21. #include "wiring_private.h"
  22. #include "pins_arduino.h"
  23. void pinMode(uint8_t pin, uint8_t mode)
  24. {
  25. uint8_t bit = digitalPinToBitMask(pin);
  26. uint8_t port = digitalPinToPort(pin);
  27. volatile uint8_t *reg, *out;
  28. if (port == NOT_A_PIN) return;
  29. // JWS: can I let the optimizer do this?
  30. reg = portModeRegister(port);
  31. out = portOutputRegister(port);
  32. if (mode == INPUT) {
  33. uint8_t oldSREG = SREG;
  34. cli();
  35. *reg &= ~bit;
  36. *out &= ~bit;
  37. SREG = oldSREG;
  38. } else if (mode == INPUT_PULLUP) {
  39. uint8_t oldSREG = SREG;
  40. cli();
  41. *reg &= ~bit;
  42. *out |= bit;
  43. SREG = oldSREG;
  44. } else {
  45. uint8_t oldSREG = SREG;
  46. cli();
  47. *reg |= bit;
  48. SREG = oldSREG;
  49. }
  50. }
  51. // Forcing this inline keeps the callers from having to push their own stuff
  52. // on the stack. It is a good performance win and only takes 1 more byte per
  53. // user than calling. (It will take more bytes on the 168.)
  54. //
  55. // But shouldn't this be moved into pinMode? Seems silly to check and do on
  56. // each digitalread or write.
  57. //
  58. // Mark Sproul:
  59. // - Removed inline. Save 170 bytes on atmega1280
  60. // - changed to a switch statment; added 32 bytes but much easier to read and maintain.
  61. // - Added more #ifdefs, now compiles for atmega645
  62. //
  63. //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
  64. //static inline void turnOffPWM(uint8_t timer)
  65. static void turnOffPWM(uint8_t timer)
  66. {
  67. switch (timer)
  68. {
  69. #if defined(TCCR1A) && defined(COM1A1)
  70. case TIMER1A: cbi(TCCR1A, COM1A1); break;
  71. #endif
  72. #if defined(TCCR1A) && defined(COM1B1)
  73. case TIMER1B: cbi(TCCR1A, COM1B1); break;
  74. #endif
  75. #if defined(TCCR2) && defined(COM21)
  76. case TIMER2: cbi(TCCR2, COM21); break;
  77. #endif
  78. #if defined(TCCR0A) && defined(COM0A1)
  79. case TIMER0A: cbi(TCCR0A, COM0A1); break;
  80. #endif
  81. #if defined(TIMER0B) && defined(COM0B1)
  82. case TIMER0B: cbi(TCCR0A, COM0B1); break;
  83. #endif
  84. #if defined(TCCR2A) && defined(COM2A1)
  85. case TIMER2A: cbi(TCCR2A, COM2A1); break;
  86. #endif
  87. #if defined(TCCR2A) && defined(COM2B1)
  88. case TIMER2B: cbi(TCCR2A, COM2B1); break;
  89. #endif
  90. #if defined(TCCR3A) && defined(COM3A1)
  91. case TIMER3A: cbi(TCCR3A, COM3A1); break;
  92. #endif
  93. #if defined(TCCR3A) && defined(COM3B1)
  94. case TIMER3B: cbi(TCCR3A, COM3B1); break;
  95. #endif
  96. #if defined(TCCR3A) && defined(COM3C1)
  97. case TIMER3C: cbi(TCCR3A, COM3C1); break;
  98. #endif
  99. #if defined(TCCR4A) && defined(COM4A1)
  100. case TIMER4A: cbi(TCCR4A, COM4A1); break;
  101. #endif
  102. #if defined(TCCR4A) && defined(COM4B1)
  103. case TIMER4B: cbi(TCCR4A, COM4B1); break;
  104. #endif
  105. #if defined(TCCR4A) && defined(COM4C1)
  106. case TIMER4C: cbi(TCCR4A, COM4C1); break;
  107. #endif
  108. #if defined(TCCR4C) && defined(COM4D1)
  109. case TIMER4D: cbi(TCCR4C, COM4D1); break;
  110. #endif
  111. #if defined(TCCR5A)
  112. case TIMER5A: cbi(TCCR5A, COM5A1); break;
  113. case TIMER5B: cbi(TCCR5A, COM5B1); break;
  114. case TIMER5C: cbi(TCCR5A, COM5C1); break;
  115. #endif
  116. }
  117. }
  118. void digitalWrite(uint8_t pin, uint8_t val)
  119. {
  120. uint8_t timer = digitalPinToTimer(pin);
  121. uint8_t bit = digitalPinToBitMask(pin);
  122. uint8_t port = digitalPinToPort(pin);
  123. volatile uint8_t *out;
  124. if (port == NOT_A_PIN) return;
  125. // If the pin that support PWM output, we need to turn it off
  126. // before doing a digital write.
  127. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  128. out = portOutputRegister(port);
  129. uint8_t oldSREG = SREG;
  130. cli();
  131. if (val == LOW) {
  132. *out &= ~bit;
  133. } else {
  134. *out |= bit;
  135. }
  136. SREG = oldSREG;
  137. }
  138. int digitalRead(uint8_t pin)
  139. {
  140. uint8_t timer = digitalPinToTimer(pin);
  141. uint8_t bit = digitalPinToBitMask(pin);
  142. uint8_t port = digitalPinToPort(pin);
  143. if (port == NOT_A_PIN) return LOW;
  144. // If the pin that support PWM output, we need to turn it off
  145. // before getting a digital reading.
  146. if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  147. if (*portInputRegister(port) & bit) return HIGH;
  148. return LOW;
  149. }