Revision 276
cleaned up library - dos2unix and delete-trailing-whitespace
dio.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (c) 2007 Colony Project |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Permission is hereby granted, free of charge, to any person |
5 | 5 |
* obtaining a copy of this software and associated documentation |
6 | 6 |
* files (the "Software"), to deal in the Software without |
... | ... | |
9 | 9 |
* copies of the Software, and to permit persons to whom the |
10 | 10 |
* Software is furnished to do so, subject to the following |
11 | 11 |
* conditions: |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* The above copyright notice and this permission notice shall be |
14 | 14 |
* included in all copies or substantial portions of the Software. |
15 |
*
|
|
15 |
* |
|
16 | 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 | 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
... | ... | |
41 | 41 |
/** |
42 | 42 |
* @defgroup dio Digital Input / Output |
43 | 43 |
* @brief Controls digital input and output |
44 |
*
|
|
44 |
* |
|
45 | 45 |
* A general note on how port / pin numbers work:<br> |
46 | 46 |
* The portpin is used to select both the bank and which pin is selected. |
47 | 47 |
* 6 bits are used (lower 6, ex: 0b00abcdef). |
... | ... | |
53 | 53 |
* E = 101<br> |
54 | 54 |
* F = 110<br> |
55 | 55 |
* G = 111<br><br> |
56 |
*
|
|
56 |
* |
|
57 | 57 |
* The bank can be found by doing portpin >> 3. <br> |
58 |
*
|
|
58 |
* |
|
59 | 59 |
* The next three (def in this example) are used to select the pin number. |
60 | 60 |
* These three bits are just the binary representation of the pin number.<br> |
61 | 61 |
* <br> |
... | ... | |
66 | 66 |
|
67 | 67 |
/** |
68 | 68 |
* Reads the selected portpin. |
69 |
*
|
|
69 |
* |
|
70 | 70 |
* @param portpin The portpin to be read. See the general description |
71 | 71 |
* for a description of portpins. |
72 | 72 |
* |
... | ... | |
75 | 75 |
int digital_input(int portpin){ |
76 | 76 |
int pin = portpin & 0x7; |
77 | 77 |
int pin_val = 0; |
78 |
|
|
78 |
|
|
79 | 79 |
switch(portpin >> 3){ |
80 | 80 |
case _PORT_A: |
81 | 81 |
DDRA &= ~_BV(pin); |
... | ... | |
111 | 111 |
return (pin_val >> pin) & 1; |
112 | 112 |
default: break; |
113 | 113 |
} |
114 |
|
|
114 |
|
|
115 | 115 |
return -1; |
116 | 116 |
} |
117 | 117 |
|
... | ... | |
131 | 131 |
break; |
132 | 132 |
case _PORT_B: |
133 | 133 |
PORTB |= _BV(pins); |
134 |
break;
|
|
134 |
break; |
|
135 | 135 |
case _PORT_C: |
136 | 136 |
PORTC |= _BV(pins); |
137 |
break;
|
|
137 |
break; |
|
138 | 138 |
case _PORT_D: |
139 | 139 |
PORTD |= _BV(pins); |
140 |
break;
|
|
140 |
break; |
|
141 | 141 |
case _PORT_E: |
142 | 142 |
PORTE |= _BV(pins); |
143 |
break;
|
|
143 |
break; |
|
144 | 144 |
case _PORT_F: |
145 | 145 |
PORTF |= _BV(pins); |
146 |
break;
|
|
146 |
break; |
|
147 | 147 |
case _PORT_G: |
148 | 148 |
PORTG |= _BV(pins); |
149 | 149 |
break; |
... | ... | |
152 | 152 |
|
153 | 153 |
/** |
154 | 154 |
* Sets portpin to the given value. |
155 |
*
|
|
155 |
* |
|
156 | 156 |
* @param portpin the portpin to output to. See the general |
157 | 157 |
* description for a discussion of portpins. |
158 | 158 |
* |
... | ... | |
161 | 161 |
**/ |
162 | 162 |
void digital_output(int portpin, int val) { |
163 | 163 |
int pins = portpin & 0x07; |
164 |
|
|
164 |
|
|
165 | 165 |
/* if you want to set to 0... */ |
166 | 166 |
if(val == 0) { |
167 | 167 |
switch(portpin >> 3) { |
... | ... | |
172 | 172 |
case _PORT_B: |
173 | 173 |
DDRB |= _BV(pins); |
174 | 174 |
PORTB &= (0XFF - _BV(pins)); |
175 |
break;
|
|
175 |
break; |
|
176 | 176 |
case _PORT_C: |
177 | 177 |
DDRC |= _BV(pins); |
178 | 178 |
PORTC &= (0XFF - _BV(pins)); |
179 |
break;
|
|
179 |
break; |
|
180 | 180 |
case _PORT_D: |
181 | 181 |
DDRD |= _BV(pins); |
182 | 182 |
PORTD &= (0XFF - _BV(pins)); |
183 |
break;
|
|
183 |
break; |
|
184 | 184 |
case _PORT_E: |
185 | 185 |
DDRE |= _BV(pins); |
186 | 186 |
PORTE &= (0XFF - _BV(pins)); |
187 |
break;
|
|
187 |
break; |
|
188 | 188 |
case _PORT_F: |
189 | 189 |
DDRF |= _BV(pins); |
190 | 190 |
PORTF &= (0XFF - _BV(pins)); |
191 |
break;
|
|
191 |
break; |
|
192 | 192 |
case _PORT_G: |
193 | 193 |
DDRG |= _BV(pins); |
194 | 194 |
PORTG &= (0XFF - _BV(pins)); |
195 | 195 |
break; |
196 | 196 |
} |
197 |
}else { /* ( val == 1) */
|
|
197 |
}else { /* ( val == 1) */ |
|
198 | 198 |
switch(portpin >> 3) { |
199 | 199 |
case _PORT_A: |
200 | 200 |
DDRA |= _BV(pins); |
... | ... | |
203 | 203 |
case _PORT_B: |
204 | 204 |
DDRB |= _BV(pins); |
205 | 205 |
PORTB |= _BV(pins); |
206 |
break;
|
|
206 |
break; |
|
207 | 207 |
case _PORT_C: |
208 | 208 |
DDRC |= _BV(pins); |
209 | 209 |
PORTC |= _BV(pins); |
210 |
break;
|
|
210 |
break; |
|
211 | 211 |
case _PORT_D: |
212 | 212 |
DDRD |= _BV(pins); |
213 | 213 |
PORTD |= _BV(pins); |
214 |
break;
|
|
214 |
break; |
|
215 | 215 |
case _PORT_E: |
216 | 216 |
DDRE |= _BV(pins); |
217 | 217 |
PORTE |= _BV(pins); |
218 |
break;
|
|
218 |
break; |
|
219 | 219 |
case _PORT_F: |
220 | 220 |
DDRF |= _BV(pins); |
221 | 221 |
PORTF |= _BV(pins); |
222 |
break;
|
|
222 |
break; |
|
223 | 223 |
case _PORT_G: |
224 | 224 |
DDRG |= _BV(pins); |
225 | 225 |
PORTG |= _BV(pins); |
... | ... | |
230 | 230 |
|
231 | 231 |
/** |
232 | 232 |
* Checks if button1 is currently pressed. |
233 |
*
|
|
233 |
* |
|
234 | 234 |
* @return 1 if button1 is pressed, 0 otherwise |
235 | 235 |
* |
236 | 236 |
* @see button1_wait, button1_click |
... | ... | |
242 | 242 |
PORTG|= _BV(PING0); |
243 | 243 |
pin_val = PING; |
244 | 244 |
return !((pin_val & _BV(PING0))); |
245 |
|
|
245 |
|
|
246 | 246 |
} |
247 | 247 |
|
248 | 248 |
/** |
... | ... | |
277 | 277 |
|
278 | 278 |
/** |
279 | 279 |
* Checks if button2 is currently pressed. |
280 |
*
|
|
280 |
* |
|
281 | 281 |
* @return 1 if button2 is pressed, 0 otherwise |
282 | 282 |
* |
283 | 283 |
* @see button2_wait, button2_click |
Also available in: Unified diff