root / trunk / code / projects / libdragonfly / dio.c @ 378
History | View | Annotate | Download (7.09 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
|
27 |
/**
|
28 |
* @file dio.c
|
29 |
* @brief Digital Input and Output
|
30 |
*
|
31 |
* Implementation of functions for digital input and output.
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
**/
|
35 |
|
36 |
#include <avr/interrupt.h> |
37 |
#include <dio.h> |
38 |
#include <time.h> |
39 |
#include <lights.h> |
40 |
|
41 |
/**
|
42 |
* @defgroup dio Digital Input / Output
|
43 |
* @brief Controls digital input and output
|
44 |
*
|
45 |
* A general note on how port / pin numbers work:<br>
|
46 |
* The portpin is used to select both the bank and which pin is selected.
|
47 |
* 6 bits are used (lower 6, ex: 0b00abcdef).
|
48 |
* The first 3 (abc in this example) are used to select the bank.<br>
|
49 |
* A = 001<br>
|
50 |
* B = 010<br>
|
51 |
* C = 011<br>
|
52 |
* D = 100<br>
|
53 |
* E = 101<br>
|
54 |
* F = 110<br>
|
55 |
* G = 111<br><br>
|
56 |
*
|
57 |
* The bank can be found by doing portpin >> 3. <br>
|
58 |
*
|
59 |
* The next three (def in this example) are used to select the pin number.
|
60 |
* These three bits are just the binary representation of the pin number.<br>
|
61 |
* <br>
|
62 |
* The pin number can be found by doing portpin & 0b111.<br><br>
|
63 |
*
|
64 |
* Include dio.h to access these functions.
|
65 |
**/
|
66 |
|
67 |
/**
|
68 |
* Reads the selected portpin.
|
69 |
*
|
70 |
* @param portpin The portpin to be read. See the general description
|
71 |
* for a description of portpins.
|
72 |
*
|
73 |
* @return 1 or 0, depending on the value of the portpin.
|
74 |
**/
|
75 |
int digital_input(int portpin) { |
76 |
int pin = portpin & 0x7; |
77 |
int pin_val = 0; |
78 |
|
79 |
switch(portpin >> 3) { |
80 |
case _PORT_A:
|
81 |
DDRA &= ~_BV(pin); |
82 |
pin_val = PINA; |
83 |
return (pin_val >> pin) & 1; |
84 |
case _PORT_B:
|
85 |
DDRB &= ~_BV(pin); |
86 |
pin_val = PINB; |
87 |
return (pin_val >> pin) & 1; |
88 |
case _PORT_C:
|
89 |
DDRC &= ~_BV(pin); |
90 |
pin_val = PINC; |
91 |
return (pin_val >> pin) & 1; |
92 |
case _PORT_D:
|
93 |
DDRD &= ~_BV(pin); |
94 |
pin_val = PIND; |
95 |
return (pin_val >> pin) & 1; |
96 |
case _PORT_E:
|
97 |
DDRE &= ~_BV(pin); |
98 |
pin_val = PINE; |
99 |
return (pin_val >> pin) & 1; |
100 |
case _PORT_F:
|
101 |
if(pin>=4) { |
102 |
MCUSR|=1<<7; |
103 |
MCUSR|=1<<7; |
104 |
} |
105 |
DDRF &= ~_BV(pin); |
106 |
pin_val = PINF; |
107 |
return (pin_val >> pin) & 1; |
108 |
case _PORT_G:
|
109 |
DDRG &= ~_BV(pin); |
110 |
pin_val = PING; |
111 |
return (pin_val >> pin) & 1; |
112 |
default: break; |
113 |
} |
114 |
|
115 |
return -1; |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Enables pullup on a pin. If it is an output pin, the pin will output
|
120 |
* 1.
|
121 |
*
|
122 |
* @param portpin the pin to enable pullup on. See the general description
|
123 |
* for a discussion of portpins.
|
124 |
**/
|
125 |
void digital_pull_up(int portpin) { |
126 |
int pins = portpin & 0x07; |
127 |
|
128 |
switch(portpin >> 3) { |
129 |
case _PORT_A:
|
130 |
PORTA |= _BV(pins); |
131 |
break;
|
132 |
case _PORT_B:
|
133 |
PORTB |= _BV(pins); |
134 |
break;
|
135 |
case _PORT_C:
|
136 |
PORTC |= _BV(pins); |
137 |
break;
|
138 |
case _PORT_D:
|
139 |
PORTD |= _BV(pins); |
140 |
break;
|
141 |
case _PORT_E:
|
142 |
PORTE |= _BV(pins); |
143 |
break;
|
144 |
case _PORT_F:
|
145 |
PORTF |= _BV(pins); |
146 |
break;
|
147 |
case _PORT_G:
|
148 |
PORTG |= _BV(pins); |
149 |
break;
|
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Sets portpin to the given value.
|
155 |
*
|
156 |
* @param portpin the portpin to output to. See the general
|
157 |
* description for a discussion of portpins.
|
158 |
*
|
159 |
* @param val the value to set the portpin to. 0 for off,
|
160 |
* nonzero for on.
|
161 |
**/
|
162 |
void digital_output(int portpin, int val) { |
163 |
int pins = portpin & 0x07; |
164 |
|
165 |
/* if you want to set to 0... */
|
166 |
if(val == 0) { |
167 |
switch(portpin >> 3) { |
168 |
case _PORT_A:
|
169 |
DDRA |= _BV(pins); |
170 |
PORTA &= (0XFF - _BV(pins));
|
171 |
break;
|
172 |
case _PORT_B:
|
173 |
DDRB |= _BV(pins); |
174 |
PORTB &= (0XFF - _BV(pins));
|
175 |
break;
|
176 |
case _PORT_C:
|
177 |
DDRC |= _BV(pins); |
178 |
PORTC &= (0XFF - _BV(pins));
|
179 |
break;
|
180 |
case _PORT_D:
|
181 |
DDRD |= _BV(pins); |
182 |
PORTD &= (0XFF - _BV(pins));
|
183 |
break;
|
184 |
case _PORT_E:
|
185 |
DDRE |= _BV(pins); |
186 |
PORTE &= (0XFF - _BV(pins));
|
187 |
break;
|
188 |
case _PORT_F:
|
189 |
DDRF |= _BV(pins); |
190 |
PORTF &= (0XFF - _BV(pins));
|
191 |
break;
|
192 |
case _PORT_G:
|
193 |
DDRG |= _BV(pins); |
194 |
PORTG &= (0XFF - _BV(pins));
|
195 |
break;
|
196 |
} |
197 |
} else { /* ( val == 1) */ |
198 |
switch(portpin >> 3) { |
199 |
case _PORT_A:
|
200 |
DDRA |= _BV(pins); |
201 |
PORTA |= _BV(pins); |
202 |
break;
|
203 |
case _PORT_B:
|
204 |
DDRB |= _BV(pins); |
205 |
PORTB |= _BV(pins); |
206 |
break;
|
207 |
case _PORT_C:
|
208 |
DDRC |= _BV(pins); |
209 |
PORTC |= _BV(pins); |
210 |
break;
|
211 |
case _PORT_D:
|
212 |
DDRD |= _BV(pins); |
213 |
PORTD |= _BV(pins); |
214 |
break;
|
215 |
case _PORT_E:
|
216 |
DDRE |= _BV(pins); |
217 |
PORTE |= _BV(pins); |
218 |
break;
|
219 |
case _PORT_F:
|
220 |
DDRF |= _BV(pins); |
221 |
PORTF |= _BV(pins); |
222 |
break;
|
223 |
case _PORT_G:
|
224 |
DDRG |= _BV(pins); |
225 |
PORTG |= _BV(pins); |
226 |
break;
|
227 |
} |
228 |
} |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Checks if button1 is currently pressed.
|
233 |
*
|
234 |
* @return 1 if button1 is pressed, 0 otherwise
|
235 |
*
|
236 |
* @see button1_wait, button1_click
|
237 |
**/
|
238 |
int button1_read( void ) { |
239 |
int pin_val;
|
240 |
DDRG &= ~_BV(PING0); |
241 |
PORTG|= _BV(PING0); |
242 |
pin_val = PING; |
243 |
return !((pin_val & _BV(PING0)));
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Delays execution until button1 is pressed.
|
248 |
*
|
249 |
* @see button1_read, button1_click
|
250 |
**/
|
251 |
void button1_wait( void ) { |
252 |
while(!button1_read() ) {
|
253 |
delay_ms(15);
|
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
258 |
* If button1 is pressed, waits until it is released before returning.
|
259 |
* Otherwise, the function returns immediately.
|
260 |
*
|
261 |
* @return 1 if button1 has been pressed, 0 otherwise
|
262 |
*
|
263 |
* @see button1_read, button1_wait
|
264 |
**/
|
265 |
int button1_click() {
|
266 |
if(button1_read()) {
|
267 |
while(button1_read());
|
268 |
return 1; |
269 |
} else {
|
270 |
return 0; |
271 |
} |
272 |
} |
273 |
|
274 |
/**
|
275 |
* Checks if button2 is currently pressed.
|
276 |
*
|
277 |
* @return 1 if button2 is pressed, 0 otherwise
|
278 |
*
|
279 |
* @see button2_wait, button2_click
|
280 |
**/
|
281 |
int button2_read( void ) { |
282 |
int pin_val;
|
283 |
DDRG &= ~_BV(PING1); |
284 |
PORTG|= _BV(PING1); |
285 |
pin_val = PING; |
286 |
return !((pin_val & _BV(PING1)));
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* Delays execution until button2 is pressed.
|
291 |
*
|
292 |
* @see button2_read, button2_click
|
293 |
**/
|
294 |
void button2_wait( void ) { |
295 |
while(!button2_read()){
|
296 |
delay_ms(15);
|
297 |
} |
298 |
} |
299 |
|
300 |
/**
|
301 |
* If button2 is pressed, waits until it is released before returning.
|
302 |
* Otherwise, the function returns immediately.
|
303 |
*
|
304 |
* @return 1 if button2 has been pressed, 0 otherwise
|
305 |
*
|
306 |
* @see button2_read, button2_wait
|
307 |
**/
|
308 |
int button2_click() {
|
309 |
if(button2_read()) {
|
310 |
while(button2_read());
|
311 |
return 1; |
312 |
} else {
|
313 |
return 0; |
314 |
} |
315 |
} |
316 |
|
317 |
/** @} **/ //end defgroup |
318 |
|