root / trunk / code / projects / colonet / utilities / manual_control / manualControlRobot / dio.c @ 13
History | View | Annotate | Download (5.57 KB)
1 |
/*
|
---|---|
2 |
|
3 |
dio.c
|
4 |
Controls digital input and output
|
5 |
|
6 |
A general note on how this code works:
|
7 |
portpin is used to select both the bank and which pin is selected
|
8 |
6 bits are used (lower 6, ex: 0b00abcdef)
|
9 |
the first 3 (abc in this example) are used to select the bank
|
10 |
A = 001
|
11 |
B = 010
|
12 |
C = 011
|
13 |
D = 100
|
14 |
E = 101
|
15 |
F = 110
|
16 |
G = 111
|
17 |
|
18 |
the bank can be found by doing portpin >> 3
|
19 |
|
20 |
the next 3 (def in this example) are used to select the pin number
|
21 |
the 3 bits are just the binary representation of the pin number
|
22 |
0 = 000
|
23 |
1 = 001
|
24 |
2 = 010
|
25 |
3 = 011
|
26 |
4 = 100
|
27 |
5 = 101
|
28 |
6 = 110
|
29 |
7 = 111
|
30 |
|
31 |
the pin number can be found by doing portping & 0b111
|
32 |
|
33 |
|
34 |
*/
|
35 |
|
36 |
#include <avr/interrupt.h> |
37 |
|
38 |
#include <dio.h> |
39 |
#include <time.h> |
40 |
#include <lights.h> |
41 |
/*
|
42 |
digital_input
|
43 |
|
44 |
reads the value on the selected portpin, returns it as 1 or 0
|
45 |
see general description (above) for definition of portpin
|
46 |
|
47 |
*/
|
48 |
int digital_input(int portpin){ |
49 |
int pin = portpin & 0x7; |
50 |
int pin_val = 0; |
51 |
|
52 |
switch(portpin >> 3){ |
53 |
case _PORT_A:
|
54 |
DDRA &= ~_BV(pin); |
55 |
pin_val = PINA; |
56 |
return (pin_val >> pin) & 1; |
57 |
case _PORT_B:
|
58 |
DDRB &= ~_BV(pin); |
59 |
pin_val = PINB; |
60 |
return (pin_val >> pin) & 1; |
61 |
case _PORT_C:
|
62 |
DDRC &= ~_BV(pin); |
63 |
pin_val = PINC; |
64 |
return (pin_val >> pin) & 1; |
65 |
case _PORT_D:
|
66 |
DDRD &= ~_BV(pin); |
67 |
pin_val = PIND; |
68 |
return (pin_val >> pin) & 1; |
69 |
case _PORT_E:
|
70 |
DDRE &= ~_BV(pin); |
71 |
pin_val = PINE; |
72 |
return (pin_val >> pin) & 1; |
73 |
case _PORT_F:
|
74 |
if(pin>=4){ |
75 |
MCUSR|=1<<7; |
76 |
MCUSR|=1<<7; |
77 |
} |
78 |
DDRF &= ~_BV(pin); |
79 |
pin_val = PINF; |
80 |
return (pin_val >> pin) & 1; |
81 |
case _PORT_G:
|
82 |
DDRG &= ~_BV(pin); |
83 |
pin_val = PING; |
84 |
return (pin_val >> pin) & 1; |
85 |
default: break; |
86 |
} |
87 |
|
88 |
return -1; |
89 |
} |
90 |
|
91 |
/*
|
92 |
digital_pull_up
|
93 |
Enables pullup on a pin. if pin is output, it will make it output 1.
|
94 |
|
95 |
*/
|
96 |
void digital_pull_up(int portpin) { |
97 |
int pins = portpin & 0x07; |
98 |
|
99 |
|
100 |
switch(portpin >> 3) { |
101 |
case _PORT_A:
|
102 |
PORTA |= _BV(pins); |
103 |
break;
|
104 |
case _PORT_B:
|
105 |
PORTB |= _BV(pins); |
106 |
break;
|
107 |
case _PORT_C:
|
108 |
PORTC |= _BV(pins); |
109 |
break;
|
110 |
case _PORT_D:
|
111 |
PORTD |= _BV(pins); |
112 |
break;
|
113 |
case _PORT_E:
|
114 |
PORTE |= _BV(pins); |
115 |
break;
|
116 |
case _PORT_F:
|
117 |
PORTF |= _BV(pins); |
118 |
break;
|
119 |
case _PORT_G:
|
120 |
PORTG |= _BV(pins); |
121 |
break;
|
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
/*
|
127 |
digital_output
|
128 |
|
129 |
sets portpin to the value
|
130 |
|
131 |
see general description above for explanation of portpin
|
132 |
|
133 |
val can only be 0 for off, nonzero for on
|
134 |
*/
|
135 |
void digital_output(int portpin, int val) { |
136 |
int pins = portpin & 0x07; |
137 |
|
138 |
/*
|
139 |
if you want to set to 0
|
140 |
*/
|
141 |
if(val == 0) { |
142 |
switch(portpin >> 3) { |
143 |
case _PORT_A:
|
144 |
DDRA |= _BV(pins); |
145 |
PORTA &= (0XFF - _BV(pins));
|
146 |
break;
|
147 |
case _PORT_B:
|
148 |
DDRB |= _BV(pins); |
149 |
PORTB &= (0XFF - _BV(pins));
|
150 |
break;
|
151 |
case _PORT_C:
|
152 |
DDRC |= _BV(pins); |
153 |
PORTC &= (0XFF - _BV(pins));
|
154 |
break;
|
155 |
case _PORT_D:
|
156 |
DDRD |= _BV(pins); |
157 |
PORTD &= (0XFF - _BV(pins));
|
158 |
break;
|
159 |
case _PORT_E:
|
160 |
DDRE |= _BV(pins); |
161 |
PORTE &= (0XFF - _BV(pins));
|
162 |
break;
|
163 |
case _PORT_F:
|
164 |
DDRF |= _BV(pins); |
165 |
PORTF &= (0XFF - _BV(pins));
|
166 |
break;
|
167 |
case _PORT_G:
|
168 |
DDRG |= _BV(pins); |
169 |
PORTG &= (0XFF - _BV(pins));
|
170 |
break;
|
171 |
} |
172 |
} |
173 |
else { /* ( val == 1) */ |
174 |
switch(portpin >> 3) { |
175 |
case _PORT_A:
|
176 |
DDRA |= _BV(pins); |
177 |
PORTA |= _BV(pins); |
178 |
break;
|
179 |
case _PORT_B:
|
180 |
DDRB |= _BV(pins); |
181 |
PORTB |= _BV(pins); |
182 |
break;
|
183 |
case _PORT_C:
|
184 |
DDRC |= _BV(pins); |
185 |
PORTC |= _BV(pins); |
186 |
break;
|
187 |
case _PORT_D:
|
188 |
DDRD |= _BV(pins); |
189 |
PORTD |= _BV(pins); |
190 |
break;
|
191 |
case _PORT_E:
|
192 |
DDRE |= _BV(pins); |
193 |
PORTE |= _BV(pins); |
194 |
break;
|
195 |
case _PORT_F:
|
196 |
DDRF |= _BV(pins); |
197 |
PORTF |= _BV(pins); |
198 |
break;
|
199 |
case _PORT_G:
|
200 |
DDRG |= _BV(pins); |
201 |
PORTG |= _BV(pins); |
202 |
break;
|
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
//////////////////////////////////////
|
212 |
//////////// button1 //////////////
|
213 |
//////////////////////////////////////
|
214 |
|
215 |
/*
|
216 |
return 1 if button is pressed, 0 otherwise
|
217 |
*/
|
218 |
int button1_read( void ) |
219 |
{ |
220 |
//return (BTN & (_BV(BTN1)) >> BTN1);
|
221 |
return (PIN_BTN >> BTN1) & 1; |
222 |
} |
223 |
|
224 |
/*
|
225 |
similar to button1_read, but hold program until the button is actually pressed
|
226 |
*/
|
227 |
void button1_wait( void ) |
228 |
{ |
229 |
while(!button1_read() ) {
|
230 |
delay_ms(15);
|
231 |
} |
232 |
} |
233 |
|
234 |
|
235 |
/*
|
236 |
same as button1_wait
|
237 |
However, blink the led while waiting
|
238 |
|
239 |
IMPORTANT: This requires that the LED has been initialized ( init_led )
|
240 |
*/
|
241 |
void button1_wait_led( void ) |
242 |
{ |
243 |
int i = 0; |
244 |
|
245 |
while(!button1_read() ) {
|
246 |
if( i < 8 ) |
247 |
led_user(1);
|
248 |
else {
|
249 |
led_user(0);
|
250 |
} |
251 |
//increment i, but restart when i = 15;
|
252 |
i = (i+1) & 0xF; |
253 |
delay_ms(15);
|
254 |
} |
255 |
|
256 |
led_user(0);
|
257 |
} |
258 |
|
259 |
|
260 |
//////////////////////////////////////
|
261 |
//////////// button2 //////////////
|
262 |
//////////////////////////////////////
|
263 |
//see button1 functions for descriptions
|
264 |
//same except for which button is used
|
265 |
int button2_read( void ) |
266 |
{ |
267 |
return (PIN_BTN >> BTN2) & 1; |
268 |
} |
269 |
|
270 |
void button2_wait( void ) |
271 |
{ |
272 |
while(!button2_read() ) {
|
273 |
delay_ms(15);
|
274 |
} |
275 |
} |
276 |
|
277 |
|
278 |
void button2_wait_led( void ) |
279 |
{ |
280 |
int i = 0; |
281 |
|
282 |
while(!button2_read() ) {
|
283 |
if( i < 8 ) |
284 |
led_user(1);
|
285 |
else {
|
286 |
led_user(0);
|
287 |
} |
288 |
//increment i, but restart when i = 15;
|
289 |
i = (i+1) & 0xF; |
290 |
delay_ms(15);
|
291 |
} |
292 |
|
293 |
led_user(0);
|
294 |
} |
295 |
|
296 |
/*
|
297 |
// EXTERNAL INTERRUPTS
|
298 |
/// example code to be used by anyone in need
|
299 |
/// this example has 2 bump sensors on PE6 and PE7
|
300 |
|
301 |
// left touch sensor on PE6
|
302 |
SIGNAL (SIG_INTERRUPT6)
|
303 |
{
|
304 |
putcharlcd('6');
|
305 |
}
|
306 |
|
307 |
// right touch sensor on PE7
|
308 |
SIGNAL (SIG_INTERRUPT7)
|
309 |
{
|
310 |
putcharlcd('7');
|
311 |
}
|
312 |
*/
|