4.14.5 16-bit Timer/Counter B with PWM

Up to five 16-bit Timer/Counters type B (TCB) with input capture for capture and signal measurements

4.14.5.1 Introduction

The Timer/Counter type B (TCB) is a flexible 16-bit timer that can perform complex actions as well as the basic functions of a simple timer. These capabilities include frequency and waveform generation, input capture on event, and time and frequency measurement of digital signals. The TCB consists of a base counter and control logic that can be set in one of eight different modes, each providing a unique functionality. The base counter is clocked by the peripheral clock with optional prescaling.

4.14.5.2 Supported Device Families

AVR® Dx AVR® Ex ATtiny ATmega

4.14.5.3 Required Header Files

#include "mcc_generated_files/timer/tcb[X].h"
Note: Replace [X] with the selected instance number of the TCB module.

4.14.5.4 How to Use the TCB PLIB Driver

For use cases see: 4.14.4.4 How to Use the TCA PLIB Driver

Since the TCA, TCB and TCD share the same timer interface, running the TCA use cases on the TCB or TCD is relatively simple. Specifically, look for the TMR_INTERFACE definition in tcbX.c or tcdX.c, then set the timer pointer to use the correct timer interface, i.e. *Timer = &TCBX_Interface, where X is the timer instance.

/* Create a pointer of the type TMR_INTERFACE and assign it to the address of the TCA0_Interface TMR_INTERFACE struct.
This enables us to get access to the portable API interface, ensuring that it's easy to change the peripheral instance the timer runs on. */
const struct TMR_INTERFACE *Timer = &TCA0_Interface;

Similarly, for more specific code, such as the line below, replace these with the equivalent code for the selected timer.

TCA0.SINGLE.CNT = 0x0;  //Clear the Counter register

4.14.5.5 Module Documentation

4.14.5.5.1 TCB

This file contains the API prototypes and custom data types for the TCB driver.

Module description

This file contains the API prototypes and custom data types for the TCB driver.

Version: TCB0 Driver Version 1.1.2
Typedefs
  • typedef uint8_t TCBx_pwm_register_t

    Data type for the result of the 8-bit Pulse-Width Modulation (PWM) register.

  • typedef void(* TCBx_cb_t) (void)

    Function pointer to callback function called by the TCB. The default value is set to NULL, which means that no callback function will be used.

Functions

Typedef Documentation

TCBx_cb_t

void * TCBx_cb_t

Function pointer to callback function called by the TCB. The default value is set to NULL, which means that no callback function will be used.

TCBx_pwm_register_t

uint8_t TCBx_pwm_register_t

Data type for the result of the 8-bit Pulse-Width Modulation (PWM) register.

Function Documentation

TCBx_CaptureCallbackRegister()

void TCBx_CaptureCallbackRegister (TCBx_cb_t cb)

Registers a callback function to be called at capture event.

Parameters:
TCBx_cb_t

cb - Callback function for capture event.

Returns:

None.

TCBx_ClearCaptInterruptFlag()

void TCBx_ClearCaptInterruptFlag (void )[inline]

Clears the Capture Interrupt flag after the Capture Interrupt flag is set.

Parameters:
None.
Returns:

None.

TCBx_ClearOvfInterruptFlag()

void TCBx_ClearOvfInterruptFlag (void )[inline]

Clears the Overflow Interrupt flag after the Overflow Interrupt flag is set.

Parameters:
None.
Returns:

None.

TCBx_DisableCaptInterrupt()

void TCBx_DisableCaptInterrupt (void )

Disables the capture interrupt for the TCB.

Parameters:
None.
Returns:

None.

TCBx_DisableOvfInterrupt()

void TCBx_DisableOvfInterrupt (void )

Disables the overflow interrupt for the TCB.

Parameters:
None.
Returns:

None.

TCBx_EnableCaptInterrupt()

void TCBx_EnableCaptInterrupt (void )

Enables the capture interrupt for the TCB.

Parameters:
None.
Returns:

None.

TCBx_EnableOvfInterrupt()

void TCBx_EnableOvfInterrupt (void )

Enables the overflow interrupt for the TCB.

Parameters:
None.
Returns:

None.

TCBx_Initialize()

void TCBx_Initialize (void )

Initializes the TCB module.

Parameters:
None.
Returns:

None.

TCBx_IsCaptInterruptEnabled()

bool TCBx_IsCaptInterruptEnabled (void )[inline]

Checks if the capture interrupt is enabled.

Parameters:
None.
Returns:

None.

TCBx_IsOvfInterruptEnabled()

bool TCBx_IsOvfInterruptEnabled (void )[inline]

Checks if the overflow interrupt is enabled.

Parameters:
None.
Returns:

None.

TCBx_load_counter()

void TCBx_load_counter (TCBx_pwm_register_t counter_value)

Loads the counter value for the 8-bit PWM.

Parameters:
TCBx_pwm_register_t

counter_value - Counter value for PWM.

Returns:

None.

TCBx_load_duty_cycle()

void TCBx_load_duty_cycle (TCBx_pwm_register_t duty_value)

Loads the duty cycle value for the 8-bit PWM.

Parameters:
TCBx_pwm_register_t

duty_value - PWM duty cycle value.

Returns:

None.

TCBx_load_top()

void TCBx_load_top (TCBx_pwm_register_t top_value)

Loads the top value for the PWM. The counter reaches top value when it becomes equal to the highest value in the count sequence.

Parameters:
TCBx_pwm_register_t

top_value - Top value for PWM.

Returns:

None.

TCBx_OverflowCallbackRegister()

void TCBx_OverflowCallbackRegister (TCBx_cb_t cb)

Registers a callback function to be called at overflow event.

Parameters:
TCBx_cb_t

cb - Callback function for overflow event.

Returns:

None.

TCBx_PWM_Disable()

void TCBx_PWM_Disable (void )

Disables the PWM mode for the TCB.

Parameters:
None.
Returns:

None.

TCBx_PWM_Enable()

void TCBx_PWM_Enable (void )

Enables the PWM mode for the TCB.

Parameters:
None.
Returns:

None.

TCBx_Read()

uint16_t TCBx_Read (void )

Reads the 16-bit timer value of the TCB.

Parameters:
None.
Returns:

uint16_t

TCBx_Start()

void TCBx_Start (void )

Starts the TCB counter.

Parameters:
None.
Returns:

None.

TCBx_Stop()

void TCBx_Stop (void )

Stops the TCB counter.

Parameters:
None.
Returns:

None.

TCBx_Write()

void TCBx_Write (uint16_t timerVal)

Writes the 16-bit timer value to the TCB.

Parameters:
uint16_t

timerVal - 16-bit timer value to write for TCB interface.

Returns:

None.

4.14.5.5.2 TIMER INTERFACE

This file contains API prototypes and other data types for the Timer modules.

Version: Timer Interface Version 1.0.0

4.14.5.6 Class Documentation

4.14.5.6.1 TMR_INTERFACE Struct Reference

#include <timer_interface.h>

Member Data Documentation

The documentation for this struct was generated from the following file:

source/

timer_interface.h

Initialize

void(* TMR_INTERFACE::Initialize) (void)

PeriodCountSet

void(* TMR_INTERFACE::PeriodCountSet) (size_t count)

Start

void(* TMR_INTERFACE::Start) (void)

Stop

void(* TMR_INTERFACE::Stop) (void)

Tasks

void(* TMR_INTERFACE::Tasks) (void)

TimeoutCallbackRegister

void(* TMR_INTERFACE::TimeoutCallbackRegister) (void(*CallbackHandler)(void))

4.14.5.7 File Documentation

4.14.5.7.1 source/tcb0.c File Reference

This file contains the API implementation for the TCB0 module driver.

#include "../tcb0.h"

Functions

Detailed Description

This file contains the API implementation for the TCB0 module driver.

TCB0 Generated Driver File

Version: TCB0 Driver Version 1.1.2

Function Documentation

ISR()

ISR (TCB0_INT_vect )

The interrupt flag is cleared by writing 1 to it, or when the Capture register is read in Capture mode.

The Overflow Interrupt flag is cleared by writing 1 to it.

4.14.5.7.2 source/tcb0.h File Reference

#include <stdint.h>
#include "../system/utils/compiler.h"
#include "./timer_interface.h"

Functions

Typedefs

  • typedef uint8_t TCBx_pwm_register_t

    Data type for the result of the 8-bit Pulse-Width Modulation (PWM) register.

  • typedef void(* TCBx_cb_t) (void)

    Function pointer to callback function called by the TCB. The default value is set to NULL, which means that no callback function will be used.

Detailed Description

TCB0 Generated Driver API Header File

Variable Documentation

4.14.5.7.3 source/timer_interface.h File Reference

#include <stddef.h>

Detailed Description

Timer Interface Generated Driver API Header File