Hi everyone
I just tested the hardware based debounce filter that is provided by the SAM3X8E. I found out it is quite easy to set up and I thought I would share my results with you. It is usually used to debounce mechanical shutters and things like that.
All you need to activate it, are the following 3 lines of code:
// activate input filters
REG_PIOx_IFER = yourPinMask;
// choose debounce filter as input filter type
REG_PIOx_DIFSR = yourPinMask;
// set clock divider for slow clock -> rejects pulses shorter than (DIV+1)*31µs and accepts pulses longer than 2*(DIV+1)*31µs
REG_PIOx_SCDR = DIV;
}
replace "yourPinMask" by the bitmask of your input pin, the "x" in REG_PIOx_... by the corresponding PORT of your pin (A,B,C or D to be looked up in the due pinout diagramm http://arduino.cc/forum/index.php/topic,132130.0.html) and the slow clock divider DIV according to the pulse length you'd like to reject. DIV is a 14bit value, allowing for rejection times between 60µs and 500s.
For people who this code looks cryptic to, here an easy example how to use it:
// input pin to debounce, pin 24 is on PORT A
int pin1 = 24;
// get bitmask for register manipulation
int mask_1 = digitalPinToBitMask(pin1);
void setup() {
pinMode(pin1, INPUT);
// activate input filters for pin 24
REG_PIOA_IFER = mask_1;
// choose debounce filter as input filter for pin 24
REG_PIOA_DIFSR = mask_1;
// set clock divider for slow clock -> rejects pulses shorter than (DIV+1)*31µs and accepts pulses longer than 2*(DIV+1)*31µs
REG_PIOA_SCDR = 10;
}
void loop() {
// do whatever you like to ;)
}
This code sets up a debounce filter for pin 24 (on PORT A) with a clock divider of DIV = 10, which means that pulses shorter than 341µs are rejected and pulses longer than 682µs are accepted (inbetween it might or might not be accepted, more or less randomly).
According to the SAM3X8E datasheet, the debounce filter influences the values read by REG_PIOx_PDSR (or digitalRead()) and the input CHANGE interrupt detection. Nothing else.. all peripheral functions (peripheral A or B), if used, are not affected.
Please mind that the activation of the debounce filter adds latencies to your digital input signals on this pin. The delays are on the order of the acceptance time set by DIV, since this is basically the time the SAM needs to check if your pulse exceeds the acceptance time.
I hope this might help someone someday