• Main Page
  • Related Pages
  • Files
  • File List
  • Globals

button.c

Go to the documentation of this file.
00001 
00012 #define _BUTTON_C_ 1
00013 
00014 #include <avr/io.h>
00015 #include <avr/interrupt.h>
00016 #include <util/delay.h>
00017 #include "button.h"
00018 #include "uart.h"
00019 
00020 
00021 void button_init(void) {
00022         
00023         //Set Button-Pins as Output
00024         DDRC |= (1<<PC4) | (1<<PC5);
00025         DDRD |= (1<<PD5) | (1<< PD6) | (1<<PD7);
00026         DDRB |= (1<<PB0);
00027         
00028         //Set Button-Pins High to charge debounce capacitors
00029         PORTC |= (1<<PC4) | (1<<PC5);
00030         PORTD |= (1<<PD5) | (1<< PD6) | (1<<PD7);
00031         PORTB |= (1<<PB0);
00032         _delay_ms(1);
00033         
00034         //Set Button-Pins as Input
00035         //as PORTxn is still High, the Pull-Up Resistors are enabled
00036         DDRC &= ~((1<<PC4) | (1<<PC5));
00037         DDRD &= ~((1<<PD5) | (1<< PD6) | (1<<PD7));
00038         DDRB &= ~((1<<PB0));
00039         
00040 }
00041 
00042 uint8_t button_read(void) {
00043         uint8_t tmp;
00044         
00045         tmp=0;
00046         tmp |= ( PINC & (1<<PC5) ) >> 5;
00047         tmp |= ( PINC & (1<<PC4) ) >> 3;
00048         tmp |= ( PINB & (1<<PB0) ) << 2;
00049         tmp |= ( PIND & (1<<PD7) ) >> 4; 
00050         tmp |= ( PIND & (1<<PD6) ) >> 2;
00051         tmp |= ( PIND & (1<<PD5) );
00052         
00053         
00054         return ~tmp;
00055         
00056 }
00057 
00058 void button_process(void) {
00059         static uint8_t read=0;
00060         uint8_t old;
00061         uint8_t mask;
00062         uint8_t diff;
00063         uint8_t sample0;
00064         static uint8_t sample1=0;
00065         uint8_t tmp;
00066         uint8_t i;
00067         
00068         
00069         //simple debouncing:
00070         //PIN must be same level vor 2 samples.
00071         //read contains current (debounced) state.
00072         //old contains previous state for edge detection
00073         sample0 = sample1;
00074         sample1 = button_read();
00075         mask = (sample0 ^ sample1);
00076         old = read;
00077         read = ( (old & mask) | (sample1 & (~mask)) ); 
00078         
00079         
00080         diff = (old ^ read);
00081         
00082         //rising edge
00083         tmp = (read & diff);
00084         for(i=0; i<6; i++) {
00085                 if(tmp & (1<<i)) {
00086                         uart_sendbyte('a'+i);
00087                 }
00088         }
00089         
00090         //falling edge
00091         tmp = ( (~read) & diff);
00092         for(i=0; i<6; i++) {
00093                 if(tmp & (1<<i)) {
00094                         uart_sendbyte('A'+i);
00095                 }
00096         }
00097         
00098 }

Generated on Thu Feb 23 2012 13:31:37 for UartDisplay by  doxygen 1.7.2