Statistics
| Revision:

## root / trunk / code / lib / src / libdragonfly / dio.c @ 7

 1 ```/* ``` ```dio.c ``` ```*/ ``` ```#include ``` ```#include ``` ```#include ``` ```#include ``` ```/** ``` ``` * @defgroup dio Digital Input / Output ``` ``` * @brief Controls digital input and output ``` ``` * ``` ``` * A general note on how port / pin numbers work:
``` ``` * The portpin is used to select both the bank and which pin is selected. ``` ``` * 6 bits are used (lower 6, ex: 0b00abcdef). ``` ``` * The first 3 (abc in this example) are used to select the bank.
``` ``` * A = 001
``` ``` * B = 010
``` ``` * C = 011
``` ``` * D = 100
``` ``` * E = 101
``` ``` * F = 110
``` ``` * G = 111

``` ``` * ``` ``` * The bank can be found by doing portpin >> 3.
``` ``` * ``` ``` * The next three (def in this example) are used to select the pin number. ``` ``` * These three bits are just the binary representation of the pin number.
``` ``` *
``` ``` * The pin number can be found by doing portpin & 0b111.

``` ``` * ``` ``` * Include dio.h to access these functions. ``` ``` **/ ``` ```/** ``` ``` * Reads the selected portpin. ``` ``` * ``` ``` * @param portpin The portpin to be read. See the general description ``` ``` * for a description of portpins. ``` ``` * ``` ``` * @return 1 or 0, depending on the value of the portpin. ``` ``` **/ ``` ```int digital_input(int portpin){ ``` ``` int pin = portpin & 0x7; ``` ``` int pin_val = 0; ``` ``` ``` ``` switch(portpin >> 3){ ``` ``` case _PORT_A: ``` ``` DDRA &= ~_BV(pin); ``` ``` pin_val = PINA; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_B: ``` ``` DDRB &= ~_BV(pin); ``` ``` pin_val = PINB; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_C: ``` ``` DDRC &= ~_BV(pin); ``` ``` pin_val = PINC; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_D: ``` ``` DDRD &= ~_BV(pin); ``` ``` pin_val = PIND; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_E: ``` ``` DDRE &= ~_BV(pin); ``` ``` pin_val = PINE; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_F: ``` ``` if(pin>=4){ ``` ``` MCUSR|=1<<7; ``` ``` MCUSR|=1<<7; ``` ``` } ``` ``` DDRF &= ~_BV(pin); ``` ``` pin_val = PINF; ``` ``` return (pin_val >> pin) & 1; ``` ``` case _PORT_G: ``` ``` DDRG &= ~_BV(pin); ``` ``` pin_val = PING; ``` ``` return (pin_val >> pin) & 1; ``` ``` default: break; ``` ``` } ``` ``` ``` ``` return -1; ``` ```} ``` ```/** ``` ``` * Enables pullup on a pin. If it is an output pin, the pin will output ``` ``` * 1. ``` ``` * ``` ``` * @param portpin the pin to enable pullup on. See the general description ``` ``` * for a discussion of portpins. ``` ``` **/ ``` ```void digital_pull_up(int portpin) { ``` ``` int pins = portpin & 0x07; ``` ``` switch(portpin >> 3) { ``` ``` case _PORT_A: ``` ``` PORTA |= _BV(pins); ``` ``` break; ``` ``` case _PORT_B: ``` ``` PORTB |= _BV(pins); ``` ``` break; ``` ``` case _PORT_C: ``` ``` PORTC |= _BV(pins); ``` ``` break; ``` ``` case _PORT_D: ``` ``` PORTD |= _BV(pins); ``` ``` break; ``` ``` case _PORT_E: ``` ``` PORTE |= _BV(pins); ``` ``` break; ``` ``` case _PORT_F: ``` ``` PORTF |= _BV(pins); ``` ``` break; ``` ``` case _PORT_G: ``` ``` PORTG |= _BV(pins); ``` ``` break; ``` ``` } ``` ```} ``` ```/** ``` ``` * Sets portpin to the given value. ``` ``` * ``` ``` * @param portpin the portpin to output to. See the general ``` ``` * description for a discussion of portpins. ``` ``` * ``` ``` * @param val the value to set the portpin to. 0 for off, ``` ``` * nonzero for on. ``` ``` **/ ``` ```void digital_output(int portpin, int val) { ``` ``` int pins = portpin & 0x07; ``` ``` ``` ``` /* if you want to set to 0... */ ``` ``` if(val == 0) { ``` ``` switch(portpin >> 3) { ``` ``` case _PORT_A: ``` ``` DDRA |= _BV(pins); ``` ``` PORTA &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_B: ``` ``` DDRB |= _BV(pins); ``` ``` PORTB &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_C: ``` ``` DDRC |= _BV(pins); ``` ``` PORTC &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_D: ``` ``` DDRD |= _BV(pins); ``` ``` PORTD &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_E: ``` ``` DDRE |= _BV(pins); ``` ``` PORTE &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_F: ``` ``` DDRF |= _BV(pins); ``` ``` PORTF &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` case _PORT_G: ``` ``` DDRG |= _BV(pins); ``` ``` PORTG &= (0XFF - _BV(pins)); ``` ``` break; ``` ``` } ``` ``` }else { /* ( val == 1) */ ``` ``` switch(portpin >> 3) { ``` ``` case _PORT_A: ``` ``` DDRA |= _BV(pins); ``` ``` PORTA |= _BV(pins); ``` ``` break; ``` ``` case _PORT_B: ``` ``` DDRB |= _BV(pins); ``` ``` PORTB |= _BV(pins); ``` ``` break; ``` ``` case _PORT_C: ``` ``` DDRC |= _BV(pins); ``` ``` PORTC |= _BV(pins); ``` ``` break; ``` ``` case _PORT_D: ``` ``` DDRD |= _BV(pins); ``` ``` PORTD |= _BV(pins); ``` ``` break; ``` ``` case _PORT_E: ``` ``` DDRE |= _BV(pins); ``` ``` PORTE |= _BV(pins); ``` ``` break; ``` ``` case _PORT_F: ``` ``` DDRF |= _BV(pins); ``` ``` PORTF |= _BV(pins); ``` ``` break; ``` ``` case _PORT_G: ``` ``` DDRG |= _BV(pins); ``` ``` PORTG |= _BV(pins); ``` ``` break; ``` ``` } ``` ``` } ``` ```} ``` ```/** ``` ``` * Checks if button1 is currently pressed. ``` ``` * ``` ``` * @return 1 if button1 is pressed, 0 otherwise ``` ``` * ``` ``` * @see button1_wait, button1_click ``` ``` **/ ``` ```int button1_read( void ) ``` ```{ ``` ``` int pin_val; ``` ``` DDRG &= ~_BV(PING0); ``` ``` PORTG|= _BV(PING0); ``` ``` pin_val = PING; ``` ``` return !((pin_val & _BV(PING0))); ``` ``` ``` ```} ``` ```/** ``` ``` * Delays execution until button1 is pressed. ``` ``` * ``` ``` * @see button1_read, button1_click ``` ``` **/ ``` ```void button1_wait( void ) ``` ```{ ``` ``` while(!button1_read() ) { ``` ``` delay_ms(15); ``` ``` } ``` ```} ``` ```/** ``` ``` * If button1 is pressed, waits until it is released before returning. ``` ``` * Otherwise, the function returns immediately. ``` ``` * ``` ``` * @return 1 if button1 has been pressed, 0 otherwise ``` ``` * ``` ``` * @see button1_read, button1_wait ``` ``` **/ ``` ```int button1_click() ``` ```{ ``` ``` if(button1_read()){ ``` ``` while(button1_read()); ``` ``` return 1; ``` ``` }else{ ``` ``` return 0; ``` ``` } ``` ```} ``` ```/** ``` ``` * Checks if button2 is currently pressed. ``` ``` * ``` ``` * @return 1 if button2 is pressed, 0 otherwise ``` ``` * ``` ``` * @see button2_wait, button2_click ``` ``` **/ ``` ```int button2_read( void ) ``` ```{ ``` ``` int pin_val; ``` ``` DDRG &= ~_BV(PING1); ``` ``` PORTG|= _BV(PING1); ``` ``` pin_val = PING; ``` ``` return !((pin_val & _BV(PING1))); ``` ```} ``` ```/** ``` ``` * Delays execution until button2 is pressed. ``` ``` * ``` ``` * @see button2_read, button2_click ``` ``` **/ ``` ```void button2_wait( void ) ``` ```{ ``` ``` while(!button2_read()){ ``` ``` delay_ms(15); ``` ``` } ``` ```} ``` ```/** ``` ``` * If button2 is pressed, waits until it is released before returning. ``` ``` * Otherwise, the function returns immediately. ``` ``` * ``` ``` * @return 1 if button2 has been pressed, 0 otherwise ``` ``` * ``` ``` * @see button2_read, button2_wait ``` ``` **/ ``` ```int button2_click() ``` ```{ ``` ``` if(button2_read()){ ``` ``` while(button2_read()); ``` ``` return 1; ``` ``` }else{ ``` ``` return 0; ``` ``` } ``` ```} ``` ```/** @} **/ //end defgroup ```