root / trunk / code / lib / src / libdragonfly / dio.c @ 277
History | View | Annotate | Download (7.01 KB)
1 | 241 | bcoltin | /**
|
---|---|---|---|
2 | * Copyright (c) 2007 Colony Project
|
||
3 | 276 | emarinel | *
|
4 | 241 | bcoltin | * 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 | 276 | emarinel | *
|
13 | 241 | bcoltin | * The above copyright notice and this permission notice shall be
|
14 | * included in all copies or substantial portions of the Software.
|
||
15 | 276 | emarinel | *
|
16 | 241 | bcoltin | * 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 | 7 | bcoltin | |
26 | 241 | bcoltin | |
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 | 7 | bcoltin | #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 | 276 | emarinel | *
|
45 | 7 | bcoltin | * 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 | 276 | emarinel | *
|
57 | 7 | bcoltin | * The bank can be found by doing portpin >> 3. <br>
|
58 | 276 | emarinel | *
|
59 | 7 | bcoltin | * 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 | 276 | emarinel | *
|
70 | 7 | bcoltin | * @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 | 276 | emarinel | |
79 | 7 | bcoltin | 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 | 276 | emarinel | |
115 | 7 | bcoltin | 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 | 276 | emarinel | break;
|
135 | 7 | bcoltin | case _PORT_C:
|
136 | PORTC |= _BV(pins); |
||
137 | 276 | emarinel | break;
|
138 | 7 | bcoltin | case _PORT_D:
|
139 | PORTD |= _BV(pins); |
||
140 | 276 | emarinel | break;
|
141 | 7 | bcoltin | case _PORT_E:
|
142 | PORTE |= _BV(pins); |
||
143 | 276 | emarinel | break;
|
144 | 7 | bcoltin | case _PORT_F:
|
145 | PORTF |= _BV(pins); |
||
146 | 276 | emarinel | break;
|
147 | 7 | bcoltin | case _PORT_G:
|
148 | PORTG |= _BV(pins); |
||
149 | break;
|
||
150 | } |
||
151 | } |
||
152 | |||
153 | /**
|
||
154 | * Sets portpin to the given value.
|
||
155 | 276 | emarinel | *
|
156 | 7 | bcoltin | * @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 | 276 | emarinel | |
165 | 7 | bcoltin | /* if you want to set to 0... */
|
166 | 277 | emarinel | if (val == 0) { |
167 | 7 | bcoltin | 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 | 276 | emarinel | break;
|
176 | 7 | bcoltin | case _PORT_C:
|
177 | DDRC |= _BV(pins); |
||
178 | PORTC &= (0XFF - _BV(pins));
|
||
179 | 276 | emarinel | break;
|
180 | 7 | bcoltin | case _PORT_D:
|
181 | DDRD |= _BV(pins); |
||
182 | PORTD &= (0XFF - _BV(pins));
|
||
183 | 276 | emarinel | break;
|
184 | 7 | bcoltin | case _PORT_E:
|
185 | DDRE |= _BV(pins); |
||
186 | PORTE &= (0XFF - _BV(pins));
|
||
187 | 276 | emarinel | break;
|
188 | 7 | bcoltin | case _PORT_F:
|
189 | DDRF |= _BV(pins); |
||
190 | PORTF &= (0XFF - _BV(pins));
|
||
191 | 276 | emarinel | break;
|
192 | 7 | bcoltin | case _PORT_G:
|
193 | DDRG |= _BV(pins); |
||
194 | PORTG &= (0XFF - _BV(pins));
|
||
195 | break;
|
||
196 | } |
||
197 | 277 | emarinel | } else { /* ( val == 1) */ |
198 | 7 | bcoltin | 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 | 276 | emarinel | break;
|
207 | 7 | bcoltin | case _PORT_C:
|
208 | DDRC |= _BV(pins); |
||
209 | PORTC |= _BV(pins); |
||
210 | 276 | emarinel | break;
|
211 | 7 | bcoltin | case _PORT_D:
|
212 | DDRD |= _BV(pins); |
||
213 | PORTD |= _BV(pins); |
||
214 | 276 | emarinel | break;
|
215 | 7 | bcoltin | case _PORT_E:
|
216 | DDRE |= _BV(pins); |
||
217 | PORTE |= _BV(pins); |
||
218 | 276 | emarinel | break;
|
219 | 7 | bcoltin | case _PORT_F:
|
220 | DDRF |= _BV(pins); |
||
221 | PORTF |= _BV(pins); |
||
222 | 276 | emarinel | break;
|
223 | 7 | bcoltin | 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 | 276 | emarinel | *
|
234 | 7 | bcoltin | * @return 1 if button1 is pressed, 0 otherwise
|
235 | *
|
||
236 | * @see button1_wait, button1_click
|
||
237 | **/
|
||
238 | int button1_read( void ) |
||
239 | { |
||
240 | 277 | emarinel | int pin_val;
|
241 | DDRG &= ~_BV(PING0); |
||
242 | PORTG|= _BV(PING0); |
||
243 | pin_val = PING; |
||
244 | return !((pin_val & _BV(PING0)));
|
||
245 | 7 | bcoltin | } |
246 | |||
247 | /**
|
||
248 | * Delays execution until button1 is pressed.
|
||
249 | *
|
||
250 | * @see button1_read, button1_click
|
||
251 | **/
|
||
252 | void button1_wait( void ) |
||
253 | { |
||
254 | 277 | emarinel | while (!button1_read()) {
|
255 | 7 | bcoltin | delay_ms(15);
|
256 | } |
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * If button1 is pressed, waits until it is released before returning.
|
||
261 | * Otherwise, the function returns immediately.
|
||
262 | *
|
||
263 | * @return 1 if button1 has been pressed, 0 otherwise
|
||
264 | *
|
||
265 | * @see button1_read, button1_wait
|
||
266 | **/
|
||
267 | int button1_click()
|
||
268 | { |
||
269 | 277 | emarinel | if (button1_read()) {
|
270 | while (button1_read());
|
||
271 | 7 | bcoltin | return 1; |
272 | 277 | emarinel | } else {
|
273 | 7 | bcoltin | return 0; |
274 | } |
||
275 | } |
||
276 | |||
277 | /**
|
||
278 | * Checks if button2 is currently pressed.
|
||
279 | 276 | emarinel | *
|
280 | 7 | bcoltin | * @return 1 if button2 is pressed, 0 otherwise
|
281 | *
|
||
282 | * @see button2_wait, button2_click
|
||
283 | **/
|
||
284 | int button2_read( void ) |
||
285 | { |
||
286 | 277 | emarinel | int pin_val;
|
287 | DDRG &= ~_BV(PING1); |
||
288 | PORTG|= _BV(PING1); |
||
289 | pin_val = PING; |
||
290 | return !((pin_val & _BV(PING1)));
|
||
291 | 7 | bcoltin | } |
292 | |||
293 | /**
|
||
294 | * Delays execution until button2 is pressed.
|
||
295 | *
|
||
296 | * @see button2_read, button2_click
|
||
297 | **/
|
||
298 | 277 | emarinel | void button2_wait(void) |
299 | 7 | bcoltin | { |
300 | 277 | emarinel | while (!button2_read()) {
|
301 | 7 | bcoltin | delay_ms(15);
|
302 | } |
||
303 | } |
||
304 | |||
305 | /**
|
||
306 | * If button2 is pressed, waits until it is released before returning.
|
||
307 | * Otherwise, the function returns immediately.
|
||
308 | *
|
||
309 | * @return 1 if button2 has been pressed, 0 otherwise
|
||
310 | *
|
||
311 | * @see button2_read, button2_wait
|
||
312 | **/
|
||
313 | int button2_click()
|
||
314 | { |
||
315 | 277 | emarinel | if (button2_read()) {
|
316 | while (button2_read());
|
||
317 | 7 | bcoltin | return 1; |
318 | 277 | emarinel | } else {
|
319 | return 0; |
||
320 | 7 | bcoltin | } |
321 | } |
||
322 | |||
323 | /** @} **/ //end defgroup |