2.5.2.3 TCB in Single-Shot Mode

The Single-Shot mode can be used to generate a pulse with a duration defined by the Compare (TCBn.CCMP) register every time a rising or falling edge is observed on a connected event channel.

AVR® Dx - TCB Initialization in Single-Shot Mode

void TCB2_Init(void)
{
	TCB2.CCMP = 0x400;          /* Pulse width:  0x400*/

	TCB2.CTRLB = 0 << TCB_ASYNC_bp         /* Asynchronous Enable: disabled */
	             | 0 << TCB_CCMPEN_bp      /* Pin Output Enable: disabled */
	             | 0 << TCB_CCMPINIT_bp    /* Pin Initial State: disabled */
	             | TCB_CNTMODE_SINGLE_gc;  /* Single Shot*/

	TCB2.EVCTRL = 1 << TCB_CAPTEI_bp      /* Event Input Enable: enabled */
	              | 0 << TCB_EDGE_bp      /* Event Edge: disabled */
	              | 0 << TCB_FILTER_bp;   /* Input Capture Noise Cancellation Filter: disabled */

	TCB2.INTCTRL = 1 << TCB_CAPT_bp       /* Capture or Timeout: enabled */
	               | 0 << TCB_OVF_bp;     /* OverFlow Interrupt: disabled */

	TCB2.CTRLA = TCB_CLKSEL_DIV1_gc       /* CLK_PER */
	             | 1 << TCB_ENABLE_bp     /* Enable: enabled */
	             | 0 << TCB_RUNSTDBY_bp   /* Run Standby: disabled */
	             | 0 << TCB_SYNCUPD_bp    /* Synchronize Update: disabled */
	             | 0 << TCB_CASCADE_bp;   /* Cascade Two Timer/Counters: disabled */

}

For all previous uses cases, the TCB must be used in correlation with the Event System. The following sample of code shows the configuration of the Event System (the pin PA6 is used as an input event for TCB2):

AVR® Dx - Event System Initialization for TCB

int8_t EVENT_SYSTEM_0_init()
{
    EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN6_gc; /* Port A Pin 6 */
    EVSYS.USERTCB2CAPT = EVSYS_USER_CHANNEL0_gc;   /* Connect TCB2 to event channel 0 */

Additionally to the use cases presented, the TCB can be used for periodic interrupts, time-out checks, PWM generation, or as a 32-bit timer in conjunction with another TCB peripheral. Refer to the AVR Dx data sheet for the complete list of operation modes.

Note: For more details and code examples, refer to TB3214 - Getting Started with TCB.