Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / dio.c @ 437

History | View | Annotate | Download (7.09 KB)

1 241 bcoltin
/**
2
 * Copyright (c) 2007 Colony Project
3 336 bcoltin
 *
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 336 bcoltin
 *
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 336 bcoltin
 *
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 336 bcoltin
 *
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 336 bcoltin
 *
57 7 bcoltin
 * The bank can be found by doing portpin >> 3. <br>
58 336 bcoltin
 *
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 336 bcoltin
 *
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 437 kwoo
int digital_input(int portpin) {
76 7 bcoltin
  int pin = portpin & 0x7;
77
  int pin_val = 0;
78 336 bcoltin
79 437 kwoo
  switch(portpin >> 3) {
80 7 bcoltin
  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 437 kwoo
    if(pin>=4) {
102 7 bcoltin
      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 336 bcoltin
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 336 bcoltin
    break;
135 7 bcoltin
  case _PORT_C:
136
    PORTC |= _BV(pins);
137 336 bcoltin
    break;
138 7 bcoltin
  case _PORT_D:
139
    PORTD |= _BV(pins);
140 336 bcoltin
    break;
141 7 bcoltin
  case _PORT_E:
142
    PORTE |= _BV(pins);
143 336 bcoltin
    break;
144 7 bcoltin
  case _PORT_F:
145
    PORTF |= _BV(pins);
146 336 bcoltin
    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 336 bcoltin
 *
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 336 bcoltin
165 7 bcoltin
  /* if you want to set to 0... */
166 336 bcoltin
  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 336 bcoltin
      break;
176 7 bcoltin
    case _PORT_C:
177
      DDRC |= _BV(pins);
178
      PORTC &= (0XFF - _BV(pins));
179 336 bcoltin
      break;
180 7 bcoltin
    case _PORT_D:
181
      DDRD |= _BV(pins);
182
      PORTD &= (0XFF - _BV(pins));
183 336 bcoltin
      break;
184 7 bcoltin
    case _PORT_E:
185
      DDRE |= _BV(pins);
186
      PORTE &= (0XFF - _BV(pins));
187 336 bcoltin
      break;
188 7 bcoltin
    case _PORT_F:
189
      DDRF |= _BV(pins);
190
      PORTF &= (0XFF - _BV(pins));
191 336 bcoltin
      break;
192 7 bcoltin
    case _PORT_G:
193
      DDRG |= _BV(pins);
194
      PORTG &= (0XFF - _BV(pins));
195
      break;
196
    }
197 437 kwoo
  } 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 336 bcoltin
      break;
207 7 bcoltin
    case _PORT_C:
208
      DDRC |= _BV(pins);
209
      PORTC |= _BV(pins);
210 336 bcoltin
      break;
211 7 bcoltin
    case _PORT_D:
212
      DDRD |= _BV(pins);
213
      PORTD |= _BV(pins);
214 336 bcoltin
      break;
215 7 bcoltin
    case _PORT_E:
216
      DDRE |= _BV(pins);
217
      PORTE |= _BV(pins);
218 336 bcoltin
      break;
219 7 bcoltin
    case _PORT_F:
220
      DDRF |= _BV(pins);
221
      PORTF |= _BV(pins);
222 336 bcoltin
      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 336 bcoltin
 *
234 7 bcoltin
 * @return 1 if button1 is pressed, 0 otherwise
235
 *
236
 * @see button1_wait, button1_click
237
 **/
238 437 kwoo
int button1_read( void ) {
239
  int pin_val;
240
  DDRG &= ~_BV(PING0);
241
  PORTG|= _BV(PING0);
242
  pin_val = PING;
243
  return !((pin_val & _BV(PING0)));
244 7 bcoltin
}
245
246
/**
247
 * Delays execution until button1 is pressed.
248
 *
249
 * @see button1_read, button1_click
250
 **/
251 437 kwoo
void button1_wait( void ) {
252 336 bcoltin
  while(!button1_read() ) {
253 7 bcoltin
    delay_ms(15);
254
  }
255
}
256
257
/**
258
 * If button1 is pressed, waits until it is released before returning.
259
 * Otherwise, the function returns immediately.
260
 *
261
 * @return 1 if button1 has been pressed, 0 otherwise
262
 *
263
 * @see button1_read, button1_wait
264
 **/
265 437 kwoo
int button1_click() {
266
  if(button1_read()) {
267 336 bcoltin
    while(button1_read());
268 7 bcoltin
    return 1;
269 437 kwoo
  } else {
270 7 bcoltin
    return 0;
271
  }
272
}
273
274
/**
275
 * Checks if button2 is currently pressed.
276 336 bcoltin
 *
277 7 bcoltin
 * @return 1 if button2 is pressed, 0 otherwise
278
 *
279
 * @see button2_wait, button2_click
280
 **/
281 437 kwoo
int button2_read( void ) {
282
  int pin_val;
283
  DDRG &= ~_BV(PING1);
284
  PORTG|= _BV(PING1);
285
  pin_val = PING;
286
  return !((pin_val & _BV(PING1)));
287 7 bcoltin
}
288
289
/**
290
 * Delays execution until button2 is pressed.
291
 *
292
 * @see button2_read, button2_click
293
 **/
294 437 kwoo
void button2_wait( void ) {
295 336 bcoltin
  while(!button2_read()){
296 7 bcoltin
    delay_ms(15);
297
  }
298
}
299
300
/**
301
 * If button2 is pressed, waits until it is released before returning.
302
 * Otherwise, the function returns immediately.
303
 *
304
 * @return 1 if button2 has been pressed, 0 otherwise
305
 *
306
 * @see button2_read, button2_wait
307
 **/
308 437 kwoo
int button2_click() {
309
  if(button2_read()) {
310 336 bcoltin
    while(button2_read());
311 7 bcoltin
    return 1;
312 437 kwoo
  } else {
313
    return 0;
314 7 bcoltin
  }
315
}
316
317
/** @} **/ //end defgroup