/************************************************************************************/ /* port_d_check */ /*Reads all port D switches and places them in a 16 location circular queue and */ /*logically ANDs all columns. If all 16 samples of the bit is true, a one set */ /*in that bit position in the global variable "debounced_port_d". Also returned */ /* is a "1" if something did change, else a "0" is retuened. Since port D is an */ /*active low asserted port, if a button is pushed and qualifies for the debounce, */ /*it returns a "1" and not a zero. In othe words, it inverts the state of Port D. */ /************************************************************************************/ uint8_t port_d_check() { static uint8_t index = 0; //queue pointer static uint8_t state[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //circular queue for state uint8_t i,new_state; state[index] = (PIND ^ 0xFF); //put inverted PIND inputs into queue ++index; //advance pointer if(index>=16) index=0; //wrap the queue pointer if necessary new_state = 0xFF; //initalize summing variable for(i=0; i<15; i++) //logically AND all column bits of port D new_state = new_state & state[i]; if(new_state == debounced_port_d){return 0;} //everything still the same else{ //something changed debounced_port_d = new_state; //pass back new value through global variable return 1; //note that something happened } } //port_d_check