|
1 /* |
|
2 wiring_digital.c - digital input and output functions |
|
3 Part of Arduino - http://www.arduino.cc/ |
|
4 |
|
5 Copyright (c) 2005-2006 David A. Mellis |
|
6 |
|
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. |
|
11 |
|
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. |
|
16 |
|
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 |
|
21 |
|
22 Modified 28 September 2010 by Mark Sproul |
|
23 |
|
24 $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ |
|
25 */ |
|
26 |
|
27 #define ARDUINO_MAIN |
|
28 #include "wiring_private.h" |
|
29 #include "pins_arduino.h" |
|
30 |
|
31 void pinMode(uint8_t pin, uint8_t mode) |
|
32 { |
|
33 uint8_t bit = digitalPinToBitMask(pin); |
|
34 uint8_t port = digitalPinToPort(pin); |
|
35 volatile uint8_t *reg, *out; |
|
36 |
|
37 if (port == NOT_A_PIN) return; |
|
38 |
|
39 // JWS: can I let the optimizer do this? |
|
40 reg = portModeRegister(port); |
|
41 out = portOutputRegister(port); |
|
42 |
|
43 if (mode == INPUT) { |
|
44 uint8_t oldSREG = SREG; |
|
45 cli(); |
|
46 *reg &= ~bit; |
|
47 *out &= ~bit; |
|
48 SREG = oldSREG; |
|
49 } else if (mode == INPUT_PULLUP) { |
|
50 uint8_t oldSREG = SREG; |
|
51 cli(); |
|
52 *reg &= ~bit; |
|
53 *out |= bit; |
|
54 SREG = oldSREG; |
|
55 } else { |
|
56 uint8_t oldSREG = SREG; |
|
57 cli(); |
|
58 *reg |= bit; |
|
59 SREG = oldSREG; |
|
60 } |
|
61 } |
|
62 |
|
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.) |
|
66 // |
|
67 // But shouldn't this be moved into pinMode? Seems silly to check and do on |
|
68 // each digitalread or write. |
|
69 // |
|
70 // Mark Sproul: |
|
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 |
|
74 // |
|
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) |
|
78 { |
|
79 switch (timer) |
|
80 { |
|
81 #if defined(TCCR1A) && defined(COM1A1) |
|
82 case TIMER1A: cbi(TCCR1A, COM1A1); break; |
|
83 #endif |
|
84 #if defined(TCCR1A) && defined(COM1B1) |
|
85 case TIMER1B: cbi(TCCR1A, COM1B1); break; |
|
86 #endif |
|
87 |
|
88 #if defined(TCCR2) && defined(COM21) |
|
89 case TIMER2: cbi(TCCR2, COM21); break; |
|
90 #endif |
|
91 |
|
92 #if defined(TCCR0A) && defined(COM0A1) |
|
93 case TIMER0A: cbi(TCCR0A, COM0A1); break; |
|
94 #endif |
|
95 |
|
96 #if defined(TIMER0B) && defined(COM0B1) |
|
97 case TIMER0B: cbi(TCCR0A, COM0B1); break; |
|
98 #endif |
|
99 #if defined(TCCR2A) && defined(COM2A1) |
|
100 case TIMER2A: cbi(TCCR2A, COM2A1); break; |
|
101 #endif |
|
102 #if defined(TCCR2A) && defined(COM2B1) |
|
103 case TIMER2B: cbi(TCCR2A, COM2B1); break; |
|
104 #endif |
|
105 |
|
106 #if defined(TCCR3A) && defined(COM3A1) |
|
107 case TIMER3A: cbi(TCCR3A, COM3A1); break; |
|
108 #endif |
|
109 #if defined(TCCR3A) && defined(COM3B1) |
|
110 case TIMER3B: cbi(TCCR3A, COM3B1); break; |
|
111 #endif |
|
112 #if defined(TCCR3A) && defined(COM3C1) |
|
113 case TIMER3C: cbi(TCCR3A, COM3C1); break; |
|
114 #endif |
|
115 |
|
116 #if defined(TCCR4A) && defined(COM4A1) |
|
117 case TIMER4A: cbi(TCCR4A, COM4A1); break; |
|
118 #endif |
|
119 #if defined(TCCR4A) && defined(COM4B1) |
|
120 case TIMER4B: cbi(TCCR4A, COM4B1); break; |
|
121 #endif |
|
122 #if defined(TCCR4A) && defined(COM4C1) |
|
123 case TIMER4C: cbi(TCCR4A, COM4C1); break; |
|
124 #endif |
|
125 #if defined(TCCR4C) && defined(COM4D1) |
|
126 case TIMER4D: cbi(TCCR4C, COM4D1); break; |
|
127 #endif |
|
128 |
|
129 #if defined(TCCR5A) |
|
130 case TIMER5A: cbi(TCCR5A, COM5A1); break; |
|
131 case TIMER5B: cbi(TCCR5A, COM5B1); break; |
|
132 case TIMER5C: cbi(TCCR5A, COM5C1); break; |
|
133 #endif |
|
134 } |
|
135 } |
|
136 |
|
137 void digitalWrite(uint8_t pin, uint8_t val) |
|
138 { |
|
139 uint8_t timer = digitalPinToTimer(pin); |
|
140 uint8_t bit = digitalPinToBitMask(pin); |
|
141 uint8_t port = digitalPinToPort(pin); |
|
142 volatile uint8_t *out; |
|
143 |
|
144 if (port == NOT_A_PIN) return; |
|
145 |
|
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); |
|
149 |
|
150 out = portOutputRegister(port); |
|
151 |
|
152 uint8_t oldSREG = SREG; |
|
153 cli(); |
|
154 |
|
155 if (val == LOW) { |
|
156 *out &= ~bit; |
|
157 } else { |
|
158 *out |= bit; |
|
159 } |
|
160 |
|
161 SREG = oldSREG; |
|
162 } |
|
163 |
|
164 int digitalRead(uint8_t pin) |
|
165 { |
|
166 uint8_t timer = digitalPinToTimer(pin); |
|
167 uint8_t bit = digitalPinToBitMask(pin); |
|
168 uint8_t port = digitalPinToPort(pin); |
|
169 |
|
170 if (port == NOT_A_PIN) return LOW; |
|
171 |
|
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); |
|
175 |
|
176 if (*portInputRegister(port) & bit) return HIGH; |
|
177 return LOW; |
|
178 } |