root / branches / charging_station / code / projects / recharging / charging_station / dio.c @ 85
History | View | Annotate | Download (5.3 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 |
/*
|
43 |
digital_input
|
44 |
|
45 |
reads the value on the selected portpin, returns it as 1 or 0
|
46 |
see general description (above) for definition of portpin
|
47 |
|
48 |
*/
|
49 |
int digital_input(int portpin){ |
50 |
int pin = portpin & 0x7; |
51 |
int pin_val = 0; |
52 |
|
53 |
switch(portpin >> 3){ |
54 |
case _PORT_A:
|
55 |
DDRA &= ~_BV(pin); |
56 |
pin_val = PINA; |
57 |
return (pin_val >> pin) & 1; |
58 |
case _PORT_B:
|
59 |
DDRB &= ~_BV(pin); |
60 |
pin_val = PINB; |
61 |
return (pin_val >> pin) & 1; |
62 |
case _PORT_C:
|
63 |
DDRC &= ~_BV(pin); |
64 |
pin_val = PINC; |
65 |
return (pin_val >> pin) & 1; |
66 |
case _PORT_D:
|
67 |
DDRD &= ~_BV(pin); |
68 |
pin_val = PIND; |
69 |
return (pin_val >> pin) & 1; |
70 |
case _PORT_E:
|
71 |
DDRE &= ~_BV(pin); |
72 |
pin_val = PINE; |
73 |
return (pin_val >> pin) & 1; |
74 |
case _PORT_F:
|
75 |
if(pin>=4){ |
76 |
MCUSR|=1<<7; |
77 |
MCUSR|=1<<7; |
78 |
} |
79 |
DDRF &= ~_BV(pin); |
80 |
pin_val = PINF; |
81 |
return (pin_val >> pin) & 1; |
82 |
case _PORT_G:
|
83 |
DDRG &= ~_BV(pin); |
84 |
pin_val = PING; |
85 |
return (pin_val >> pin) & 1; |
86 |
default: break; |
87 |
} |
88 |
|
89 |
return -1; |
90 |
} |
91 |
|
92 |
/*
|
93 |
digital_output
|
94 |
|
95 |
sets portpin to the value
|
96 |
|
97 |
see general description above for explanation of portpin
|
98 |
|
99 |
val can only be 1 or 0
|
100 |
*/
|
101 |
void digital_output(int portpin, int val) { |
102 |
int pins = portpin & 0x07; |
103 |
|
104 |
/*
|
105 |
if you want to set to 0
|
106 |
*/
|
107 |
if(val == 0) { |
108 |
switch(portpin >> 3) { |
109 |
case _PORT_A:
|
110 |
DDRA |= _BV(pins); |
111 |
PORTA &= (0XFF - _BV(pins));
|
112 |
break;
|
113 |
case _PORT_B:
|
114 |
DDRB |= _BV(pins); |
115 |
PORTB &= (0XFF - _BV(pins));
|
116 |
break;
|
117 |
case _PORT_C:
|
118 |
DDRC |= _BV(pins); |
119 |
PORTC &= (0XFF - _BV(pins));
|
120 |
break;
|
121 |
case _PORT_D:
|
122 |
DDRD |= _BV(pins); |
123 |
PORTD &= (0XFF - _BV(pins));
|
124 |
break;
|
125 |
case _PORT_E:
|
126 |
DDRE |= _BV(pins); |
127 |
PORTE &= (0XFF - _BV(pins));
|
128 |
break;
|
129 |
case _PORT_F:
|
130 |
DDRF |= _BV(pins); |
131 |
PORTF &= (0XFF - _BV(pins));
|
132 |
break;
|
133 |
case _PORT_G:
|
134 |
DDRG |= _BV(pins); |
135 |
PORTG &= (0XFF - _BV(pins));
|
136 |
break;
|
137 |
} |
138 |
} |
139 |
if( val == 1) { |
140 |
switch(portpin >> 3) { |
141 |
case _PORT_A:
|
142 |
DDRA |= _BV(pins); |
143 |
PORTA |= _BV(pins); |
144 |
break;
|
145 |
case _PORT_B:
|
146 |
DDRB |= _BV(pins); |
147 |
PORTB |= _BV(pins); |
148 |
break;
|
149 |
case _PORT_C:
|
150 |
DDRC |= _BV(pins); |
151 |
PORTC |= _BV(pins); |
152 |
break;
|
153 |
case _PORT_D:
|
154 |
DDRD |= _BV(pins); |
155 |
PORTD |= _BV(pins); |
156 |
break;
|
157 |
case _PORT_E:
|
158 |
DDRE |= _BV(pins); |
159 |
PORTE |= _BV(pins); |
160 |
break;
|
161 |
case _PORT_F:
|
162 |
DDRF |= _BV(pins); |
163 |
PORTF |= _BV(pins); |
164 |
break;
|
165 |
case _PORT_G:
|
166 |
DDRG |= _BV(pins); |
167 |
PORTG |= _BV(pins); |
168 |
break;
|
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
//////////////////////////////////////
|
178 |
//////////// button1 //////////////
|
179 |
//////////////////////////////////////
|
180 |
|
181 |
/*
|
182 |
return 1 if button is pressed, 0 otherwise
|
183 |
*/
|
184 |
int button1_read( void ) |
185 |
{ |
186 |
//return (BTN & (_BV(BTN1)) >> BTN1);
|
187 |
return (PIN_BTN >> BTN1) & 1; |
188 |
} |
189 |
|
190 |
//click waits until the button is realesed to return true
|
191 |
//return false immediatley
|
192 |
int button1_click()
|
193 |
{ |
194 |
if(button1_read()){
|
195 |
while(button1_read());
|
196 |
return 1; |
197 |
}else{
|
198 |
return 0; |
199 |
} |
200 |
} |
201 |
|
202 |
/*
|
203 |
similar to button1_read, but hold program until the button is actually pressed
|
204 |
*/
|
205 |
void button1_wait( void ) |
206 |
{ |
207 |
while(!button1_read() ) {
|
208 |
delay_ms(15);
|
209 |
} |
210 |
} |
211 |
|
212 |
|
213 |
/*
|
214 |
same as button1_wait
|
215 |
However, blink the led while waiting
|
216 |
|
217 |
IMPORTANT: This requires that the LED has been initialized ( init_led )
|
218 |
*/
|
219 |
void button1_wait_led( void ) |
220 |
{ |
221 |
int i = 0; |
222 |
|
223 |
while(!button1_read() ) {
|
224 |
if( i < 8 ) |
225 |
led_user(1);
|
226 |
else {
|
227 |
led_user(0);
|
228 |
} |
229 |
//increment i, but restart when i = 15;
|
230 |
i = (i+1) & 0xF; |
231 |
delay_ms(15);
|
232 |
} |
233 |
|
234 |
led_user(0);
|
235 |
} |
236 |
|
237 |
|
238 |
//////////////////////////////////////
|
239 |
//////////// button2 //////////////
|
240 |
//////////////////////////////////////
|
241 |
//see button1 functions for descriptions
|
242 |
//same except for which button is used
|
243 |
int button2_read( void ) |
244 |
{ |
245 |
return (PIN_BTN >> BTN2) & 1; |
246 |
} |
247 |
|
248 |
int button2_click()
|
249 |
{ |
250 |
if(button2_read()){
|
251 |
while(button2_read());
|
252 |
return 1; |
253 |
}else{
|
254 |
return 0; |
255 |
} |
256 |
} |
257 |
|
258 |
void button2_wait( void ) |
259 |
{ |
260 |
while(!button2_read() ) {
|
261 |
delay_ms(15);
|
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
void button2_wait_led( void ) |
267 |
{ |
268 |
int i = 0; |
269 |
|
270 |
while(!button2_read() ) {
|
271 |
if( i < 8 ) |
272 |
led_user(1);
|
273 |
else {
|
274 |
led_user(0);
|
275 |
} |
276 |
//increment i, but restart when i = 15;
|
277 |
i = (i+1) & 0xF; |
278 |
delay_ms(15);
|
279 |
} |
280 |
|
281 |
led_user(0);
|
282 |
} |
283 |
|
284 |
/*
|
285 |
// EXTERNAL INTERRUPTS
|
286 |
/// example code to be used by anyone in need
|
287 |
/// this example has 2 bump sensors on PE6 and PE7
|
288 |
|
289 |
// left touch sensor on PE6
|
290 |
SIGNAL (SIG_INTERRUPT6)
|
291 |
{
|
292 |
putcharlcd('6');
|
293 |
}
|
294 |
|
295 |
// right touch sensor on PE7
|
296 |
SIGNAL (SIG_INTERRUPT7)
|
297 |
{
|
298 |
putcharlcd('7');
|
299 |
}
|
300 |
*/
|