tbouncer.h

Input debouncing module that uses the task_sched.h task scheduler.

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.)

Summary
tbouncer.hInput debouncing module that uses the task_sched.h task scheduler.
Configuration Macros
Macros
TBOUNCER_TICK_COUNT_BITSThe number of bits in the tick count field of an I/O pin’s debouncing state variable.
TBOUNCER_NEQ_COUNT_BITSThe number of bits in the tick count field of an I/O pin’s debouncing state variable.
TBOUNCER_TICKS_FOR_UPDATEThe 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_UPDATEThe 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_MAXMaximum allowed value of TBOUNCER_TICKS_FOR_UPDATE.
TBOUNCER_NEQ_FOR_UPDATE_MAXMaximum allowed value of TBOUNCER_NEQ_FOR_UPDATE.
TBOUNCER_ALLThis 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_INITWrapper macro for tbouncer_init.
tbouncer_a_maskPin change notification bit mask for I/O port A.
tbouncer_aDebounced pin values for I/O port A.
tbouncer_a_prevPrevious debounced pin values for I/O port A.
tbouncer_a_diffPin change flags for I/O port A.
TBOUNCER_A_RISINGRising edge flags for I/O port A.
TBOUNCER_A_FALLINGFalling edge flags for I/O port A.
tbouncer_b_maskPin change notification bit mask for I/O port B.
tbouncer_bDebounced pin values for I/O port B.
tbouncer_b_prevPrevious debounced pin values for I/O port B.
tbouncer_b_diffPin change flags for I/O port B.
TBOUNCER_B_RISINGRising edge flags for I/O port B.
TBOUNCER_B_FALLINGFalling edge flags for I/O port B.
tbouncer_c_maskPin change notification bit mask for I/O port C.
tbouncer_cDebounced pin values for I/O port C.
tbouncer_c_prevPrevious debounced pin values for I/O port C.
tbouncer_c_diffPin change flags for I/O port C.
TBOUNCER_C_RISINGRising edge flags for I/O port C.
TBOUNCER_C_FALLINGFalling edge flags for I/O port C.
tbouncer_d_maskPin change notification bit mask for I/O port D.
tbouncer_dDebounced pin values for I/O port D.
tbouncer_d_prevPrevious debounced pin values for I/O port D.
tbouncer_d_diffPin change flags for I/O port D.
TBOUNCER_D_RISINGRising edge flags for I/O port D.
TBOUNCER_D_FALLINGFalling edge flags for I/O port D.
tbouncer_task_catsContains a set of task category bit flags.
tbouncer_invoke_maskContains a TCSB bit mask.
tbouncer_invoke_stContains a TCSB bit pattern.
Functions
tbouncer_initInitializes the debouncer module.
tbouncer_shutdownShuts down the debouncer module.

Configuration Macros

Summary
Macros
TBOUNCER_TICK_COUNT_BITSThe number of bits in the tick count field of an I/O pin’s debouncing state variable.
TBOUNCER_NEQ_COUNT_BITSThe number of bits in the tick count field of an I/O pin’s debouncing state variable.
TBOUNCER_TICKS_FOR_UPDATEThe 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_UPDATEThe 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.

Macros

TBOUNCER_TICK_COUNT_BITS

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

TBOUNCER_NEQ_COUNT_BITS

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

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.  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

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.  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

API

Summary
Macros and Variables
TBOUNCER_TICKS_FOR_UPDATE_MAXMaximum allowed value of TBOUNCER_TICKS_FOR_UPDATE.
TBOUNCER_NEQ_FOR_UPDATE_MAXMaximum allowed value of TBOUNCER_NEQ_FOR_UPDATE.
TBOUNCER_ALLThis 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_INITWrapper macro for tbouncer_init.
tbouncer_a_maskPin change notification bit mask for I/O port A.
tbouncer_aDebounced pin values for I/O port A.
tbouncer_a_prevPrevious debounced pin values for I/O port A.
tbouncer_a_diffPin change flags for I/O port A.
TBOUNCER_A_RISINGRising edge flags for I/O port A.
TBOUNCER_A_FALLINGFalling edge flags for I/O port A.
tbouncer_b_maskPin change notification bit mask for I/O port B.
tbouncer_bDebounced pin values for I/O port B.
tbouncer_b_prevPrevious debounced pin values for I/O port B.
tbouncer_b_diffPin change flags for I/O port B.
TBOUNCER_B_RISINGRising edge flags for I/O port B.
TBOUNCER_B_FALLINGFalling edge flags for I/O port B.
tbouncer_c_maskPin change notification bit mask for I/O port C.
tbouncer_cDebounced pin values for I/O port C.
tbouncer_c_prevPrevious debounced pin values for I/O port C.
tbouncer_c_diffPin change flags for I/O port C.
TBOUNCER_C_RISINGRising edge flags for I/O port C.
TBOUNCER_C_FALLINGFalling edge flags for I/O port C.
tbouncer_d_maskPin change notification bit mask for I/O port D.
tbouncer_dDebounced pin values for I/O port D.
tbouncer_d_prevPrevious debounced pin values for I/O port D.
tbouncer_d_diffPin change flags for I/O port D.
TBOUNCER_D_RISINGRising edge flags for I/O port D.
TBOUNCER_D_FALLINGFalling edge flags for I/O port D.
tbouncer_task_catsContains a set of task category bit flags.
tbouncer_invoke_maskContains a TCSB bit mask.
tbouncer_invoke_stContains a TCSB bit pattern.
Functions
tbouncer_initInitializes the debouncer module.
tbouncer_shutdownShuts down the debouncer module.

Macros and Variables

TBOUNCER_TICKS_FOR_UPDATE_MAX

Maximum allowed value of TBOUNCER_TICKS_FOR_UPDATE.  Constant macro.  Equal to 2^TBOUNCER_TICK_COUNT_BITS.

TBOUNCER_NEQ_FOR_UPDATE_MAX

Maximum allowed value of TBOUNCER_NEQ_FOR_UPDATE.  Constant macro.  Equal to 2^TBOUNCER_NEQ_COUNT_BITS.

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.  Constant macro.

TBOUNCER_INIT

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.

  • If LIBAVR_ATTINY is defined, ports A and B are available.
  • If LIBAVR_ATMEGA_U is defined, ports A, B, C and D are available.  Note, however, that port “C” is actually port F, while port “A” is port C.
  • Otherwise, ports B, C and D are available.

Parameters

TNCA 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.)
PDScheduler delay of the debouncer task.  Determines how often the I/O ports are sampled and the debounced pin values updated.
[ABCD]MDevice-dependent pin change notification bit mask arguments.  See tbouncer_init and the explanation above.
ACTarget task categories for pin change notifications.  Will be used to initialize tbouncer_task_cats.
IMBit mask of pin change notification query.  Will be used to initialize tbouncer_invoke_mask.
ISBit pattern of pin change notification query.  Will be used to initialize tbouncer_invoke_st.

tbouncer_a_mask

extern uint8_t tbouncer_a_mask

Pin change notification bit mask for I/O port A.  Notifications will be sent for a given pin only if the corresponding bit in this mask is set to one (1).

tbouncer_a

extern uint8_t tbouncer_a

Debounced pin values for I/O port A.

tbouncer_a_prev

extern uint8_t tbouncer_a_prev

Previous debounced pin values for I/O port A.

tbouncer_a_diff

extern uint8_t tbouncer_a_diff

Pin change flags for I/O port A.  Equal to tbouncer_a XOR tbouncer_a_prev.

TBOUNCER_A_RISING

Rising edge flags for I/O port A.  Expression macro.  Evaluates to tbouncer_a AND NOT tbouncer_a_prev.

TBOUNCER_A_FALLING

Falling edge flags for I/O port A.  Expression macro.  Evaluates to NOT tbouncer_a AND tbouncer_a_prev.

tbouncer_b_mask

extern uint8_t tbouncer_b_mask

Pin change notification bit mask for I/O port B.  Notifications will be sent for a given pin only if the corresponding bit in this mask is set to one (1).

tbouncer_b

extern uint8_t tbouncer_b

Debounced pin values for I/O port B.

tbouncer_b_prev

extern uint8_t tbouncer_b_prev

Previous debounced pin values for I/O port B.

tbouncer_b_diff

extern uint8_t tbouncer_b_diff

Pin change flags for I/O port B.  Equal to tbouncer_b XOR tbouncer_b_prev.

TBOUNCER_B_RISING

Rising edge flags for I/O port B.  Expression macro.  Evaluates to tbouncer_b AND NOT tbouncer_b_prev.

TBOUNCER_B_FALLING

Falling edge flags for I/O port B.  Expression macro.  Evaluates to NOT tbouncer_b AND tbouncer_b_prev.

tbouncer_c_mask

extern uint8_t tbouncer_c_mask

Pin change notification bit mask for I/O port C.  Notifications will be sent for a given pin only if the corresponding bit in this mask is set to one (1).

tbouncer_c

extern uint8_t tbouncer_c

Debounced pin values for I/O port C.

tbouncer_c_prev

extern uint8_t tbouncer_c_prev

Previous debounced pin values for I/O port C.

tbouncer_c_diff

extern uint8_t tbouncer_c_diff

Pin change flags for I/O port C.  Equal to tbouncer_c XOR tbouncer_c_prev.

TBOUNCER_C_RISING

Rising edge flags for I/O port C.  Expression macro.  Evaluates to tbouncer_c AND NOT tbouncer_c_prev.

TBOUNCER_C_FALLING

Falling edge flags for I/O port C.  Expression macro.  Evaluates to NOT tbouncer_c AND tbouncer_c_prev.

tbouncer_d_mask

extern uint8_t tbouncer_d_mask

Pin change notification bit mask for I/O port D.  Notifications will be sent for a given pin only if the corresponding bit in this mask is set to one (1).

tbouncer_d

extern uint8_t tbouncer_d

Debounced pin values for I/O port D.

tbouncer_d_prev

extern uint8_t tbouncer_d_prev

Previous debounced pin values for I/O port D.

tbouncer_d_diff

extern uint8_t tbouncer_d_diff

Pin change flags for I/O port D.  Equal to tbouncer_d XOR tbouncer_d_prev.

TBOUNCER_D_RISING

Rising edge flags for I/O port D.  Expression macro.  Evaluates to tbouncer_d AND NOT tbouncer_d_prev.

TBOUNCER_D_FALLING

Falling edge flags for I/O port D.  Expression macro.  Evaluates to NOT tbouncer_d AND tbouncer_d_prev.

tbouncer_task_cats

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.

tbouncer_invoke_mask

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.

tbouncer_invoke_st

extern uint8_t tbouncer_invoke_st

Contains a TCSB bit pattern.  Refer to the documentation of tbouncer_invoke_mask for an explanation.

Functions

tbouncer_init

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.

Parameters

task_num_catA 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_delayScheduler delay of the debouncer task.  Determines how often the I/O ports are sampled and the debounced pin values updated.
a_maskPin change notification bit mask for I/O port A.  Will be used to initialize tbouncer_a_mask.
b_maskPin change notification bit mask for I/O port B.  Will be used to initialize tbouncer_b_mask.
c_maskPin change notification bit mask for I/O port C.  Will be used to initialize tbouncer_c_mask.
d_maskPin change notification bit mask for I/O port D.  Will be used to initialize tbouncer_d_mask.
notify_catsTarget task categories for pin change notifications.  Will be used to initialize tbouncer_task_cats.
invoke_maskBit mask of pin change notification query.  Will be used to initialize tbouncer_invoke_mask.
invoke_stBit pattern of pin change notification query.  Will be used to initialize tbouncer_invoke_st.

tbouncer_shutdown

void tbouncer_shutdown(void)

Shuts down the debouncer module.  Stops the debouncer task.

A task scheduler for AVR microcontrollers.
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.
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.
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.
extern uint8_t tbouncer_a_mask
Pin change notification bit mask for I/O port A.
extern uint8_t tbouncer_a
Debounced pin values for I/O port A.
extern uint8_t tbouncer_a_prev
Previous debounced pin values for I/O port A.
extern uint8_t tbouncer_a_diff
Pin change flags for I/O port A.
extern uint8_t tbouncer_b_mask
Pin change notification bit mask for I/O port B.
extern uint8_t tbouncer_b
Debounced pin values for I/O port B.
extern uint8_t tbouncer_b_prev
Previous debounced pin values for I/O port B.
extern uint8_t tbouncer_b_diff
Pin change flags for I/O port B.
extern uint8_t tbouncer_c_mask
Pin change notification bit mask for I/O port C.
extern uint8_t tbouncer_c
Debounced pin values for I/O port C.
extern uint8_t tbouncer_c_prev
Previous debounced pin values for I/O port C.
extern uint8_t tbouncer_c_diff
Pin change flags for I/O port C.
extern uint8_t tbouncer_d_mask
Pin change notification bit mask for I/O port D.
extern uint8_t tbouncer_d
Debounced pin values for I/O port D.
extern uint8_t tbouncer_d_prev
Previous debounced pin values for I/O port D.
extern uint8_t tbouncer_d_diff
Pin change flags for I/O port D.
extern sched_catflags tbouncer_task_cats
Contains a set of task category bit flags.
extern uint8_t tbouncer_invoke_mask
Contains a TCSB bit mask.
extern uint8_t tbouncer_invoke_st
Contains a TCSB bit pattern.
void tbouncer_shutdown(void)
Shuts down the debouncer module.
The number of bits in the tick count field of an I/O pin’s debouncing state variable.
The number of bits in the tick count field of an I/O pin’s debouncing state variable.
typedef struct sched_task
Represents an instance of a task that can be scheduled for execution.
void sched_invoke_all(uint8_t st_mask,
uint8_t st_val)
Invokes all matching tasks on the task list.
Close