//*************************************** //*************************************** // // AVRLIB-DEMO // For avrlib and avrmini development board. // // // File: buttons_sensors.c // Author: Michael Gurevich // Date: Nov. 3, 2002 #include #include #include #include "global.h" #include "midi.h" #include "uart.h" #include "timer.h" #include "a2d.h" #define DEBOUNCE_THRESHOLD 10 //returns 1 if button is down, 0 otherwise u08 checkButton(u08 button) { //array of debounce counters static u08 buttonUpCounter[9]; static u08 buttonDownCounter[9]; //button status doesn't change until ..Counter == DEBOUNCE_THRESHOLD static u08 buttonDown[9]; //button 8 is on a different port, so check it separately if (button == 8) { if (! bit_is_set(PIND,0)) { //button is pressed //check if DEBOUNCE_THRESHOLD has been reached if (buttonDown[button]==0){ if (buttonDownCounter[button]++ == DEBOUNCE_THRESHOLD){ buttonDown[button] = 1; buttonUpCounter[button] = 0; timer0ClearOverflowCount(); } } } else { if (buttonDown[button]==1){ if (buttonUpCounter[button]++ == DEBOUNCE_THRESHOLD){ buttonDown[button] = 0; buttonDownCounter[button] = 0; } } } } else { if (! bit_is_set(PINC,button)) { //button is pressed //check if DEBOUNCE_THRESHOLD has been reached if (buttonDown[button]==0){ if (buttonDownCounter[button]++ == DEBOUNCE_THRESHOLD){ buttonDown[button] = 1; buttonUpCounter[button] = 0; timer0ClearOverflowCount(); } } } else { if (buttonDown[button]==1){ if (buttonUpCounter[button]++ == DEBOUNCE_THRESHOLD){ buttonDown[button] = 0; buttonDownCounter[button] = 0; } } } } return buttonDown[button]; } int main(void) { u08 i; u08 buttonstate[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; u08 temp; s16 a2dvalue[4]; // these are for filtering s16 a2d_filt[4]; s16 a2d_last[4] = {0, 0, 0, 0}; uartInit(); midiInit(); timerInit(); a2dInit(); a2dSetReference(0x01); //set a2d pins to inputs outb(DDRA, 0x00); // set button pins as inputs outb(DDRC, 0x00); // loop forever while(1) { for(i=0;i<9;i++){ temp = checkButton(i); if (temp != buttonstate[i]) { buttonstate[i] = !buttonstate[i]; midiNoteOnOut(i, buttonstate[i], 1); } if (i<4){ //filter the a2d values a2dvalue[i] = a2dConvert10bit(i) - 512; a2d_filt[i] = (31*a2d_last[i] + a2dvalue[i])/32; a2d_last[i] = a2d_filt[i]; a2d_filt[i]+=512; a2dvalue[i]+=512; //send raw a2d midiControlChangeOut((a2dvalue[i]>>7),(a2dvalue[i] & 0x7F),i); // send filtered a2d // midiControlChangeOut((a2d_filt[i]>>7),(a2d_filt[i] & 0x7F),i+4); } } timerPause(2); } return 0; }