root / trunk / code / projects / colonet / testing / robot_routine_reg_test / dio.c @ 13
History | View | Annotate | Download (4.98 KB)
1 | 13 | emarinel | /*
|
---|---|---|---|
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 & 0b111; |
||
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 | /*
|
||
191 | similar to button1_read, but hold program until the button is actually pressed
|
||
192 | */
|
||
193 | void button1_wait( void ) |
||
194 | { |
||
195 | while(!button1_read() ) {
|
||
196 | delay_ms(15);
|
||
197 | } |
||
198 | } |
||
199 | |||
200 | |||
201 | /*
|
||
202 | same as button1_wait
|
||
203 | However, blink the led while waiting
|
||
204 | |||
205 | IMPORTANT: This requires that the LED has been initialized ( init_led )
|
||
206 | */
|
||
207 | void button1_wait_led( void ) |
||
208 | { |
||
209 | int i = 0; |
||
210 | |||
211 | while(!button1_read() ) {
|
||
212 | if( i < 8 ) |
||
213 | led_user(1);
|
||
214 | else {
|
||
215 | led_user(0);
|
||
216 | } |
||
217 | //increment i, but restart when i = 15;
|
||
218 | i = (i+1) & 0xF; |
||
219 | delay_ms(15);
|
||
220 | } |
||
221 | |||
222 | led_user(0);
|
||
223 | } |
||
224 | |||
225 | |||
226 | //////////////////////////////////////
|
||
227 | //////////// button2 //////////////
|
||
228 | //////////////////////////////////////
|
||
229 | //see button1 functions for descriptions
|
||
230 | //same except for which button is used
|
||
231 | int button2_read( void ) |
||
232 | { |
||
233 | return (PIN_BTN >> BTN2) & 1; |
||
234 | } |
||
235 | |||
236 | void button2_wait( void ) |
||
237 | { |
||
238 | while(!button2_read() ) {
|
||
239 | delay_ms(15);
|
||
240 | } |
||
241 | } |
||
242 | |||
243 | |||
244 | void button2_wait_led( void ) |
||
245 | { |
||
246 | int i = 0; |
||
247 | |||
248 | while(!button2_read() ) {
|
||
249 | if( i < 8 ) |
||
250 | led_user(1);
|
||
251 | else {
|
||
252 | led_user(0);
|
||
253 | } |
||
254 | //increment i, but restart when i = 15;
|
||
255 | i = (i+1) & 0xF; |
||
256 | delay_ms(15);
|
||
257 | } |
||
258 | |||
259 | led_user(0);
|
||
260 | } |
||
261 | |||
262 | /*
|
||
263 | // EXTERNAL INTERRUPTS
|
||
264 | /// example code to be used by anyone in need
|
||
265 | /// this example has 2 bump sensors on PE6 and PE7
|
||
266 | |||
267 | // left touch sensor on PE6
|
||
268 | SIGNAL (SIG_INTERRUPT6)
|
||
269 | {
|
||
270 | putcharlcd('6');
|
||
271 | }
|
||
272 | |||
273 | // right touch sensor on PE7
|
||
274 | SIGNAL (SIG_INTERRUPT7)
|
||
275 | {
|
||
276 | putcharlcd('7');
|
||
277 | }
|
||
278 | */ |