]>
git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring_digital.c
2 wiring_digital.c - digital input and output functions
3 Part of Arduino - http://www.arduino.cc/
5 Copyright (c) 2005-2006 David A. Mellis
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General
18 Public License along with this library; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 Boston, MA 02111-1307 USA
22 Modified 28 September 2010 by Mark Sproul
24 $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
28 #include "wiring_private.h"
29 #include "pins_arduino.h"
31 void pinMode(uint8_t pin
, uint8_t mode
)
33 uint8_t bit
= digitalPinToBitMask(pin
);
34 uint8_t port
= digitalPinToPort(pin
);
35 volatile uint8_t *reg
, *out
;
37 if (port
== NOT_A_PIN
) return;
39 // JWS: can I let the optimizer do this?
40 reg
= portModeRegister(port
);
41 out
= portOutputRegister(port
);
44 uint8_t oldSREG
= SREG
;
49 } else if (mode
== INPUT_PULLUP
) {
50 uint8_t oldSREG
= SREG
;
56 uint8_t oldSREG
= SREG
;
63 // Forcing this inline keeps the callers from having to push their own stuff
64 // on the stack. It is a good performance win and only takes 1 more byte per
65 // user than calling. (It will take more bytes on the 168.)
67 // But shouldn't this be moved into pinMode? Seems silly to check and do on
68 // each digitalread or write.
71 // - Removed inline. Save 170 bytes on atmega1280
72 // - changed to a switch statment; added 32 bytes but much easier to read and maintain.
73 // - Added more #ifdefs, now compiles for atmega645
75 //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
76 //static inline void turnOffPWM(uint8_t timer)
77 static void turnOffPWM(uint8_t timer
)
81 #if defined(TCCR1A) && defined(COM1A1)
82 case TIMER1A
: cbi(TCCR1A
, COM1A1
); break;
84 #if defined(TCCR1A) && defined(COM1B1)
85 case TIMER1B
: cbi(TCCR1A
, COM1B1
); break;
88 #if defined(TCCR2) && defined(COM21)
89 case TIMER2
: cbi(TCCR2
, COM21
); break;
92 #if defined(TCCR0A) && defined(COM0A1)
93 case TIMER0A
: cbi(TCCR0A
, COM0A1
); break;
96 #if defined(TIMER0B) && defined(COM0B1)
97 case TIMER0B
: cbi(TCCR0A
, COM0B1
); break;
99 #if defined(TCCR2A) && defined(COM2A1)
100 case TIMER2A
: cbi(TCCR2A
, COM2A1
); break;
102 #if defined(TCCR2A) && defined(COM2B1)
103 case TIMER2B
: cbi(TCCR2A
, COM2B1
); break;
106 #if defined(TCCR3A) && defined(COM3A1)
107 case TIMER3A
: cbi(TCCR3A
, COM3A1
); break;
109 #if defined(TCCR3A) && defined(COM3B1)
110 case TIMER3B
: cbi(TCCR3A
, COM3B1
); break;
112 #if defined(TCCR3A) && defined(COM3C1)
113 case TIMER3C
: cbi(TCCR3A
, COM3C1
); break;
116 #if defined(TCCR4A) && defined(COM4A1)
117 case TIMER4A
: cbi(TCCR4A
, COM4A1
); break;
119 #if defined(TCCR4A) && defined(COM4B1)
120 case TIMER4B
: cbi(TCCR4A
, COM4B1
); break;
122 #if defined(TCCR4A) && defined(COM4C1)
123 case TIMER4C
: cbi(TCCR4A
, COM4C1
); break;
125 #if defined(TCCR4C) && defined(COM4D1)
126 case TIMER4D
: cbi(TCCR4C
, COM4D1
); break;
130 case TIMER5A
: cbi(TCCR5A
, COM5A1
); break;
131 case TIMER5B
: cbi(TCCR5A
, COM5B1
); break;
132 case TIMER5C
: cbi(TCCR5A
, COM5C1
); break;
137 void digitalWrite(uint8_t pin
, uint8_t val
)
139 uint8_t timer
= digitalPinToTimer(pin
);
140 uint8_t bit
= digitalPinToBitMask(pin
);
141 uint8_t port
= digitalPinToPort(pin
);
142 volatile uint8_t *out
;
144 if (port
== NOT_A_PIN
) return;
146 // If the pin that support PWM output, we need to turn it off
147 // before doing a digital write.
148 if (timer
!= NOT_ON_TIMER
) turnOffPWM(timer
);
150 out
= portOutputRegister(port
);
152 uint8_t oldSREG
= SREG
;
164 int digitalRead(uint8_t pin
)
166 uint8_t timer
= digitalPinToTimer(pin
);
167 uint8_t bit
= digitalPinToBitMask(pin
);
168 uint8_t port
= digitalPinToPort(pin
);
170 if (port
== NOT_A_PIN
) return LOW
;
172 // If the pin that support PWM output, we need to turn it off
173 // before getting a digital reading.
174 if (timer
!= NOT_ON_TIMER
) turnOffPWM(timer
);
176 if (*portInputRegister(port
) & bit
) return HIGH
;