Input debouncing module that uses the task_sched.h task scheduler. Can be configured to notify and/or synchronously invoke tasks when there is a change in the debounced input value of specified I/O pins.
NOTE: To disable debouncing on an I/O port, define the macro TBOUNCER_DISABLE_PORT_{x}, where {x} is “A”, “B”, “C” or “D”. Doing this saves both some clock cycles and about 12 bytes of RAM per disabled port.
CAUTION (ATmega): The ATmega has no port A, so if neither LIBAVR_ATTINY nor LIBAVR_ATMEGA_U is defined, that port is automatically disabled.
CAUTION (ATtiny): The ATtiny has no port C or D, so if LIBAVR_ATTINY is defined, those ports are automatically disabled.
CAUTION (ATmegaU): Port C is very small on the ATmegaU (only two pins), so if LIBAVR_ATMEGA_U is defined, the port called “C” in this module is actually hardware port F, while the port called “A” is hardware port C. (Confusing, I know, but they started it with their weird port layout.)
tbouncer.h | Input debouncing module that uses the task_sched.h task scheduler. |
Configuration Macros | |
Macros | |
TBOUNCER_TICK_COUNT_BITS | The number of bits in the tick count field of an I/O pin’s debouncing state variable. |
TBOUNCER_NEQ_COUNT_BITS | The number of bits in the tick count field of an I/O pin’s debouncing state variable. |
TBOUNCER_TICKS_FOR_UPDATE | The number of debouncer ticks that must pass after a pin value update before the debouncer starts counting consecutive pin input values different from the current debounced value. |
TBOUNCER_NEQ_FOR_UPDATE | The number of consecutive pin input values (as obtained at each debouncer tick) different from the current debounced value that are required to update the debounced value. |
API | |
Macros and Variables | |
TBOUNCER_TICKS_FOR_UPDATE_MAX | Maximum allowed value of TBOUNCER_TICKS_FOR_UPDATE. |
TBOUNCER_NEQ_FOR_UPDATE_MAX | Maximum allowed value of TBOUNCER_NEQ_FOR_UPDATE. |
TBOUNCER_ALL | This value can be given to the pin change notification mask arguments of tbouncer_init to indicate that all the pins of the corresponding I/O port should trigger pin change notifications. |
TBOUNCER_INIT | Wrapper macro for tbouncer_init. |
tbouncer_a_mask | Pin change notification bit mask for I/O port A. |
tbouncer_a | Debounced pin values for I/O port A. |
tbouncer_a_prev | Previous debounced pin values for I/O port A. |
tbouncer_a_diff | Pin change flags for I/O port A. |
TBOUNCER_A_RISING | Rising edge flags for I/O port A. |
TBOUNCER_A_FALLING | Falling edge flags for I/O port A. |
tbouncer_b_mask | Pin change notification bit mask for I/O port B. |
tbouncer_b | Debounced pin values for I/O port B. |
tbouncer_b_prev | Previous debounced pin values for I/O port B. |
tbouncer_b_diff | Pin change flags for I/O port B. |
TBOUNCER_B_RISING | Rising edge flags for I/O port B. |
TBOUNCER_B_FALLING | Falling edge flags for I/O port B. |
tbouncer_c_mask | Pin change notification bit mask for I/O port C. |
tbouncer_c | Debounced pin values for I/O port C. |
tbouncer_c_prev | Previous debounced pin values for I/O port C. |
tbouncer_c_diff | Pin change flags for I/O port C. |
TBOUNCER_C_RISING | Rising edge flags for I/O port C. |
TBOUNCER_C_FALLING | Falling edge flags for I/O port C. |
tbouncer_d_mask | Pin change notification bit mask for I/O port D. |
tbouncer_d | Debounced pin values for I/O port D. |
tbouncer_d_prev | Previous debounced pin values for I/O port D. |
tbouncer_d_diff | Pin change flags for I/O port D. |
TBOUNCER_D_RISING | Rising edge flags for I/O port D. |
TBOUNCER_D_FALLING | Falling edge flags for I/O port D. |
tbouncer_task_cats | Contains a set of task category bit flags. |
tbouncer_invoke_mask | Contains a TCSB bit mask. |
tbouncer_invoke_st | Contains a TCSB bit pattern. |
Functions | |
tbouncer_init | Initializes the debouncer module. |
tbouncer_shutdown | Shuts down the debouncer module. |
Macros | |
TBOUNCER_TICK_COUNT_BITS | The number of bits in the tick count field of an I/O pin’s debouncing state variable. |
TBOUNCER_NEQ_COUNT_BITS | The number of bits in the tick count field of an I/O pin’s debouncing state variable. |
TBOUNCER_TICKS_FOR_UPDATE | The number of debouncer ticks that must pass after a pin value update before the debouncer starts counting consecutive pin input values different from the current debounced value. |
TBOUNCER_NEQ_FOR_UPDATE | The number of consecutive pin input values (as obtained at each debouncer tick) different from the current debounced value that are required to update the debounced value. |
The number of bits in the tick count field of an I/O pin’s debouncing state variable. Configuration macro.
This must be in the range 1-7, and the sum of TBOUNCER_TICK_COUNT_BITS and TBOUNCER_NEQ_COUNT_BITS must be less than or equal to 8.
Default value: 5
The number of bits in the tick count field of an I/O pin’s debouncing state variable. Configuration macro.
This must be in the range 1-7, and the sum of TBOUNCER_NEQ_COUNT_BITS and TBOUNCER_TICK_COUNT_BITS must be less than or equal to 8.
Default value: 3
The number of debouncer ticks that must pass after a pin value update before the debouncer starts counting consecutive pin input values different from the current debounced value. If the debounced value was last updated at tick T, then the debouncer will start counting different input values at tick T + TBOUNCER_TICKS_FOR_UPDATE. Configuration macro.
The value of this macro must be in the range 1-(TBOUNCER_TICKS_FOR_UPDATE_MAX).
Default value: 5
The number of consecutive pin input values (as obtained at each debouncer tick) different from the current debounced value that are required to update the debounced value. If the first different input value was obtained at tick T, then the debouncer will update the debounced pin value at tick T + TBOUNCER_NEQ_FOR_UPDATE. Configuration macro.
The value of this macro must be in the range 1-(TBOUNCER_NEQ_FOR_UPDATE_MAX).
Default value: 2
Macros and Variables | |
TBOUNCER_TICKS_FOR_UPDATE_MAX | Maximum allowed value of TBOUNCER_TICKS_FOR_UPDATE. |
TBOUNCER_NEQ_FOR_UPDATE_MAX | Maximum allowed value of TBOUNCER_NEQ_FOR_UPDATE. |
TBOUNCER_ALL | This value can be given to the pin change notification mask arguments of tbouncer_init to indicate that all the pins of the corresponding I/O port should trigger pin change notifications. |
TBOUNCER_INIT | Wrapper macro for tbouncer_init. |
tbouncer_a_mask | Pin change notification bit mask for I/O port A. |
tbouncer_a | Debounced pin values for I/O port A. |
tbouncer_a_prev | Previous debounced pin values for I/O port A. |
tbouncer_a_diff | Pin change flags for I/O port A. |
TBOUNCER_A_RISING | Rising edge flags for I/O port A. |
TBOUNCER_A_FALLING | Falling edge flags for I/O port A. |
tbouncer_b_mask | Pin change notification bit mask for I/O port B. |
tbouncer_b | Debounced pin values for I/O port B. |
tbouncer_b_prev | Previous debounced pin values for I/O port B. |
tbouncer_b_diff | Pin change flags for I/O port B. |
TBOUNCER_B_RISING | Rising edge flags for I/O port B. |
TBOUNCER_B_FALLING | Falling edge flags for I/O port B. |
tbouncer_c_mask | Pin change notification bit mask for I/O port C. |
tbouncer_c | Debounced pin values for I/O port C. |
tbouncer_c_prev | Previous debounced pin values for I/O port C. |
tbouncer_c_diff | Pin change flags for I/O port C. |
TBOUNCER_C_RISING | Rising edge flags for I/O port C. |
TBOUNCER_C_FALLING | Falling edge flags for I/O port C. |
tbouncer_d_mask | Pin change notification bit mask for I/O port D. |
tbouncer_d | Debounced pin values for I/O port D. |
tbouncer_d_prev | Previous debounced pin values for I/O port D. |
tbouncer_d_diff | Pin change flags for I/O port D. |
TBOUNCER_D_RISING | Rising edge flags for I/O port D. |
TBOUNCER_D_FALLING | Falling edge flags for I/O port D. |
tbouncer_task_cats | Contains a set of task category bit flags. |
tbouncer_invoke_mask | Contains a TCSB bit mask. |
tbouncer_invoke_st | Contains a TCSB bit pattern. |
Functions | |
tbouncer_init | Initializes the debouncer module. |
tbouncer_shutdown | Shuts down the debouncer module. |
Maximum allowed value of TBOUNCER_TICKS_FOR_UPDATE. Constant macro. Equal to 2^TBOUNCER_TICK_COUNT_BITS.
Maximum allowed value of TBOUNCER_NEQ_FOR_UPDATE. Constant macro. Equal to 2^TBOUNCER_NEQ_COUNT_BITS.
This value can be given to the pin change notification mask arguments of tbouncer_init to indicate that all the pins of the corresponding I/O port should trigger pin change notifications. Constant macro.
Wrapper macro for tbouncer_init. Omits the pin change notification bit mask arguments for any ports that are not available on the target device. Function-like macro.
TNC | A TCSB value (see sched_task) containing the task instance and category numbers that should be used by the debouncer task. (The sleep bit is ignored.) |
PD | Scheduler delay of the debouncer task. Determines how often the I/O ports are sampled and the debounced pin values updated. |
[ABCD]M | Device-dependent pin change notification bit mask arguments. See tbouncer_init and the explanation above. |
AC | Target task categories for pin change notifications. Will be used to initialize tbouncer_task_cats. |
IM | Bit mask of pin change notification query. Will be used to initialize tbouncer_invoke_mask. |
IS | Bit pattern of pin change notification query. Will be used to initialize tbouncer_invoke_st. |
extern uint8_t tbouncer_a_diff
Pin change flags for I/O port A. Equal to tbouncer_a XOR tbouncer_a_prev.
Rising edge flags for I/O port A. Expression macro. Evaluates to tbouncer_a AND NOT tbouncer_a_prev.
Falling edge flags for I/O port A. Expression macro. Evaluates to NOT tbouncer_a AND tbouncer_a_prev.
extern uint8_t tbouncer_b_diff
Pin change flags for I/O port B. Equal to tbouncer_b XOR tbouncer_b_prev.
Rising edge flags for I/O port B. Expression macro. Evaluates to tbouncer_b AND NOT tbouncer_b_prev.
Falling edge flags for I/O port B. Expression macro. Evaluates to NOT tbouncer_b AND tbouncer_b_prev.
extern uint8_t tbouncer_c_diff
Pin change flags for I/O port C. Equal to tbouncer_c XOR tbouncer_c_prev.
Rising edge flags for I/O port C. Expression macro. Evaluates to tbouncer_c AND NOT tbouncer_c_prev.
Falling edge flags for I/O port C. Expression macro. Evaluates to NOT tbouncer_c AND tbouncer_c_prev.
extern uint8_t tbouncer_d_diff
Pin change flags for I/O port D. Equal to tbouncer_d XOR tbouncer_d_prev.
Rising edge flags for I/O port D. Expression macro. Evaluates to tbouncer_d AND NOT tbouncer_d_prev.
Falling edge flags for I/O port D. Expression macro. Evaluates to NOT tbouncer_d AND tbouncer_d_prev.
extern sched_catflags tbouncer_task_cats
Contains a set of task category bit flags. Tasks in the indicated categories will be notified by the debouncer task when there is a change in the debounced value of any I/O pin that is selected in one of the pin change notification bit masks (i.e. tbouncer_b_mask, tbouncer_c_mask and tbouncer_d_mask).
CAUTION: By the time tasks notified via this variable are executed, the debounced pin values may have changed again. If it’s important not to miss any pin changes, use the tbouncer_invoke_mask and tbouncer_invoke_st variables to invoke tasks synchronously.
extern uint8_t tbouncer_invoke_mask
Contains a TCSB bit mask. Together with tbouncer_invoke_st, this variable defines a TCSB query. When there is a change in the debounced value of any I/O pin that is selected in one of the pin change notification bit masks (i.e. tbouncer_b_mask, tbouncer_c_mask and tbouncer_d_mask), the debouncer task will use the function sched_invoke_all to invoke all tasks that match this TCSB query.
extern uint8_t tbouncer_invoke_st
Contains a TCSB bit pattern. Refer to the documentation of tbouncer_invoke_mask for an explanation.
void tbouncer_init( uint8_t task_num_cat, sched_time poll_delay, uint8_t a_mask, uint8_t b_mask, uint8_t c_mask, uint8_t d_mask, sched_catflags notify_cats, uint8_t invoke_mask, uint8_t invoke_st )
Initializes the debouncer module. Creates the debouncer task.
task_num_cat | A TCSB value (see sched_task) containing the task instance and category numbers that should be used by the debouncer task. (The sleep bit is ignored.) |
poll_delay | Scheduler delay of the debouncer task. Determines how often the I/O ports are sampled and the debounced pin values updated. |
a_mask | Pin change notification bit mask for I/O port A. Will be used to initialize tbouncer_a_mask. |
b_mask | Pin change notification bit mask for I/O port B. Will be used to initialize tbouncer_b_mask. |
c_mask | Pin change notification bit mask for I/O port C. Will be used to initialize tbouncer_c_mask. |
d_mask | Pin change notification bit mask for I/O port D. Will be used to initialize tbouncer_d_mask. |
notify_cats | Target task categories for pin change notifications. Will be used to initialize tbouncer_task_cats. |
invoke_mask | Bit mask of pin change notification query. Will be used to initialize tbouncer_invoke_mask. |
invoke_st | Bit pattern of pin change notification query. Will be used to initialize tbouncer_invoke_st. |
Initializes the debouncer module.
void tbouncer_init( uint8_t task_num_cat, sched_time poll_delay, uint8_t a_mask, uint8_t b_mask, uint8_t c_mask, uint8_t d_mask, sched_catflags notify_cats, uint8_t invoke_mask, uint8_t invoke_st )
Pin change notification bit mask for I/O port A.
extern uint8_t tbouncer_a_mask
Debounced pin values for I/O port A.
extern uint8_t tbouncer_a
Previous debounced pin values for I/O port A.
extern uint8_t tbouncer_a_prev
Pin change flags for I/O port A.
extern uint8_t tbouncer_a_diff
Pin change notification bit mask for I/O port B.
extern uint8_t tbouncer_b_mask
Debounced pin values for I/O port B.
extern uint8_t tbouncer_b
Previous debounced pin values for I/O port B.
extern uint8_t tbouncer_b_prev
Pin change flags for I/O port B.
extern uint8_t tbouncer_b_diff
Pin change notification bit mask for I/O port C.
extern uint8_t tbouncer_c_mask
Debounced pin values for I/O port C.
extern uint8_t tbouncer_c
Previous debounced pin values for I/O port C.
extern uint8_t tbouncer_c_prev
Pin change flags for I/O port C.
extern uint8_t tbouncer_c_diff
Pin change notification bit mask for I/O port D.
extern uint8_t tbouncer_d_mask
Debounced pin values for I/O port D.
extern uint8_t tbouncer_d
Previous debounced pin values for I/O port D.
extern uint8_t tbouncer_d_prev
Pin change flags for I/O port D.
extern uint8_t tbouncer_d_diff
Contains a set of task category bit flags.
extern sched_catflags tbouncer_task_cats
Contains a TCSB bit mask.
extern uint8_t tbouncer_invoke_mask
Contains a TCSB bit pattern.
extern uint8_t tbouncer_invoke_st
Shuts down the debouncer module.
void tbouncer_shutdown( void )
Represents an instance of a task that can be scheduled for execution.
typedef struct sched_task
Invokes all matching tasks on the task list.
void sched_invoke_all( uint8_t st_mask, uint8_t st_val )