root / branches / simulator / projects / libdragonfly / dio.c @ 891
History | View | Annotate | Download (4.77 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 |
int digital_input(int portpin) { |
43 |
int pin = portpin & 0x7; |
44 |
int pin_val = 0; |
45 |
|
46 |
switch(portpin >> 3) { |
47 |
case _PORT_A:
|
48 |
DDRA &= ~_BV(pin); |
49 |
pin_val = PINA; |
50 |
return (pin_val >> pin) & 1; |
51 |
case _PORT_B:
|
52 |
DDRB &= ~_BV(pin); |
53 |
pin_val = PINB; |
54 |
return (pin_val >> pin) & 1; |
55 |
case _PORT_C:
|
56 |
DDRC &= ~_BV(pin); |
57 |
pin_val = PINC; |
58 |
return (pin_val >> pin) & 1; |
59 |
case _PORT_D:
|
60 |
DDRD &= ~_BV(pin); |
61 |
pin_val = PIND; |
62 |
return (pin_val >> pin) & 1; |
63 |
case _PORT_E:
|
64 |
DDRE &= ~_BV(pin); |
65 |
pin_val = PINE; |
66 |
return (pin_val >> pin) & 1; |
67 |
case _PORT_F:
|
68 |
if(pin>=4) { |
69 |
MCUSR|=1<<7; |
70 |
MCUSR|=1<<7; |
71 |
} |
72 |
DDRF &= ~_BV(pin); |
73 |
pin_val = PINF; |
74 |
return (pin_val >> pin) & 1; |
75 |
case _PORT_G:
|
76 |
DDRG &= ~_BV(pin); |
77 |
pin_val = PING; |
78 |
return (pin_val >> pin) & 1; |
79 |
default: break; |
80 |
} |
81 |
|
82 |
return -1; |
83 |
} |
84 |
|
85 |
void digital_pull_up(int portpin) { |
86 |
int pins = portpin & 0x07; |
87 |
|
88 |
switch(portpin >> 3) { |
89 |
case _PORT_A:
|
90 |
PORTA |= _BV(pins); |
91 |
break;
|
92 |
case _PORT_B:
|
93 |
PORTB |= _BV(pins); |
94 |
break;
|
95 |
case _PORT_C:
|
96 |
PORTC |= _BV(pins); |
97 |
break;
|
98 |
case _PORT_D:
|
99 |
PORTD |= _BV(pins); |
100 |
break;
|
101 |
case _PORT_E:
|
102 |
PORTE |= _BV(pins); |
103 |
break;
|
104 |
case _PORT_F:
|
105 |
PORTF |= _BV(pins); |
106 |
break;
|
107 |
case _PORT_G:
|
108 |
PORTG |= _BV(pins); |
109 |
break;
|
110 |
} |
111 |
} |
112 |
|
113 |
void digital_output(int portpin, int val) { |
114 |
int pins = portpin & 0x07; |
115 |
|
116 |
/* if you want to set to 0... */
|
117 |
if(val == 0) { |
118 |
switch(portpin >> 3) { |
119 |
case _PORT_A:
|
120 |
DDRA |= _BV(pins); |
121 |
PORTA &= (0XFF - _BV(pins));
|
122 |
break;
|
123 |
case _PORT_B:
|
124 |
DDRB |= _BV(pins); |
125 |
PORTB &= (0XFF - _BV(pins));
|
126 |
break;
|
127 |
case _PORT_C:
|
128 |
DDRC |= _BV(pins); |
129 |
PORTC &= (0XFF - _BV(pins));
|
130 |
break;
|
131 |
case _PORT_D:
|
132 |
DDRD |= _BV(pins); |
133 |
PORTD &= (0XFF - _BV(pins));
|
134 |
break;
|
135 |
case _PORT_E:
|
136 |
DDRE |= _BV(pins); |
137 |
PORTE &= (0XFF - _BV(pins));
|
138 |
break;
|
139 |
case _PORT_F:
|
140 |
DDRF |= _BV(pins); |
141 |
PORTF &= (0XFF - _BV(pins));
|
142 |
break;
|
143 |
case _PORT_G:
|
144 |
DDRG |= _BV(pins); |
145 |
PORTG &= (0XFF - _BV(pins));
|
146 |
break;
|
147 |
} |
148 |
} else { /* ( val == 1) */ |
149 |
switch(portpin >> 3) { |
150 |
case _PORT_A:
|
151 |
DDRA |= _BV(pins); |
152 |
PORTA |= _BV(pins); |
153 |
break;
|
154 |
case _PORT_B:
|
155 |
DDRB |= _BV(pins); |
156 |
PORTB |= _BV(pins); |
157 |
break;
|
158 |
case _PORT_C:
|
159 |
DDRC |= _BV(pins); |
160 |
PORTC |= _BV(pins); |
161 |
break;
|
162 |
case _PORT_D:
|
163 |
DDRD |= _BV(pins); |
164 |
PORTD |= _BV(pins); |
165 |
break;
|
166 |
case _PORT_E:
|
167 |
DDRE |= _BV(pins); |
168 |
PORTE |= _BV(pins); |
169 |
break;
|
170 |
case _PORT_F:
|
171 |
DDRF |= _BV(pins); |
172 |
PORTF |= _BV(pins); |
173 |
break;
|
174 |
case _PORT_G:
|
175 |
DDRG |= _BV(pins); |
176 |
PORTG |= _BV(pins); |
177 |
break;
|
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
int button1_read( void ) { |
183 |
int pin_val;
|
184 |
DDRG &= ~_BV(PING0); |
185 |
PORTG|= _BV(PING0); |
186 |
pin_val = PING; |
187 |
return !((pin_val & _BV(PING0)));
|
188 |
} |
189 |
|
190 |
void button1_wait( void ) { |
191 |
while(!button1_read() ) {
|
192 |
delay_ms(15);
|
193 |
} |
194 |
} |
195 |
|
196 |
int button1_click() {
|
197 |
if(button1_read()) {
|
198 |
while(button1_read());
|
199 |
return 1; |
200 |
} else {
|
201 |
return 0; |
202 |
} |
203 |
} |
204 |
|
205 |
int button2_read( void ) { |
206 |
int pin_val;
|
207 |
DDRG &= ~_BV(PING1); |
208 |
PORTG|= _BV(PING1); |
209 |
pin_val = PING; |
210 |
return !((pin_val & _BV(PING1)));
|
211 |
} |
212 |
|
213 |
void button2_wait( void ) { |
214 |
while(!button2_read()){
|
215 |
delay_ms(15);
|
216 |
} |
217 |
} |
218 |
|
219 |
int button2_click() {
|
220 |
if(button2_read()) {
|
221 |
while(button2_read());
|
222 |
return 1; |
223 |
} else {
|
224 |
return 0; |
225 |
} |
226 |
} |
227 |
|