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"
4.14.5.4 How to Use the TCB PLIB Driver
For use cases see: 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.
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
-
void TCBx_PWM_Enable (void)
Enables the PWM mode for the TCB.
-
void TCBx_PWM_Disable (void)
Disables the PWM mode for the TCB.
-
void TCBx_load_counter (TCBx_pwm_register_t counter_value)
Loads the counter value for the 8-bit PWM.
-
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.
-
void TCBx_load_duty_cycle (TCBx_pwm_register_t duty_value)
Loads the duty cycle value for the 8-bit PWM.
-
void TCBx_CaptureCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at capture event.
-
void TCBx_OverflowCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at overflow event.
-
void TCBx_Initialize (void)
Initializes the TCB module.
-
void TCBx_Start (void)
Starts the TCB counter.
-
void TCBx_Stop (void)
Stops the TCB counter.
-
void TCBx_EnableCaptInterrupt (void)
Enables the capture interrupt for the TCB.
-
void TCBx_DisableCaptInterrupt (void)
Disables the capture interrupt for the TCB.
-
void TCBx_EnableOvfInterrupt (void)
Enables the overflow interrupt for the TCB.
-
void TCBx_DisableOvfInterrupt (void)
Disables the overflow interrupt for the TCB.
-
uint16_t TCBx_Read (void)
Reads the 16-bit timer value of the TCB.
-
void TCBx_Write (uint16_t timerVal)
Writes the 16-bit timer value to the TCB.
-
void TCBx_ClearCaptInterruptFlag (void)
Clears the Capture Interrupt flag after the Capture Interrupt flag is set.
-
bool TCBx_IsCaptInterruptEnabled (void)
Checks if the capture interrupt is enabled.
-
void TCBx_ClearOvfInterruptFlag (void)
Clears the Overflow Interrupt flag after the Overflow Interrupt flag is set.
-
bool TCBx_IsOvfInterruptEnabled (void)
Checks if the overflow interrupt is enabled.
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.
|
None. |
TCBx_ClearCaptInterruptFlag()
void TCBx_ClearCaptInterruptFlag (void )[inline]
Clears the Capture Interrupt flag after the Capture Interrupt flag is set.
|
None. |
TCBx_ClearOvfInterruptFlag()
void TCBx_ClearOvfInterruptFlag (void )[inline]
Clears the Overflow Interrupt flag after the Overflow Interrupt flag is set.
|
None. |
TCBx_DisableCaptInterrupt()
void TCBx_DisableCaptInterrupt (void )
Disables the capture interrupt for the TCB.
|
None. |
TCBx_DisableOvfInterrupt()
void TCBx_DisableOvfInterrupt (void )
Disables the overflow interrupt for the TCB.
|
None. |
TCBx_EnableCaptInterrupt()
void TCBx_EnableCaptInterrupt (void )
Enables the capture interrupt for the TCB.
|
None. |
TCBx_EnableOvfInterrupt()
void TCBx_EnableOvfInterrupt (void )
Enables the overflow interrupt for the TCB.
|
None. |
TCBx_Initialize()
void TCBx_Initialize (void )
Initializes the TCB module.
|
None. |
TCBx_IsCaptInterruptEnabled()
bool TCBx_IsCaptInterruptEnabled (void )[inline]
Checks if the capture interrupt is enabled.
|
None. |
TCBx_IsOvfInterruptEnabled()
bool TCBx_IsOvfInterruptEnabled (void )[inline]
Checks if the overflow interrupt is enabled.
|
None. |
TCBx_load_counter()
void TCBx_load_counter (TCBx_pwm_register_t counter_value)
Loads the counter value for the 8-bit PWM.
|
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.
|
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.
|
None. |
TCBx_OverflowCallbackRegister()
void TCBx_OverflowCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at overflow event.
|
None. |
TCBx_PWM_Disable()
void TCBx_PWM_Disable (void )
Disables the PWM mode for the TCB.
|
None. |
TCBx_PWM_Enable()
void TCBx_PWM_Enable (void )
Enables the PWM mode for the TCB.
|
None. |
TCBx_Read()
uint16_t TCBx_Read (void )
Reads the 16-bit timer value of the TCB.
|
uint16_t |
TCBx_Start()
void TCBx_Start (void )
Starts the TCB counter.
|
None. |
TCBx_Stop()
void TCBx_Stop (void )
Stops the TCB counter.
|
None. |
TCBx_Write()
void TCBx_Write (uint16_t timerVal)
Writes the 16-bit timer value to the TCB.
|
None. |
4.14.5.5.2 TIMER INTERFACE
This file contains API prototypes and other data types for the Timer modules.
4.14.5.6 Class Documentation
4.14.5.6.1 TMR_INTERFACE Struct Reference
#include <timer_interface.h>
Public Attributes
-
void(* Initialize )(void)
-
void(* Start )(void)
-
void(* Stop )(void)
-
void(* PeriodCountSet )(size_t count)
-
void(* TimeoutCallbackRegister )(void(*CallbackHandler)(void))
-
void(* Tasks )(void)
Member Data Documentation
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
-
void TCBx_OverflowCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at overflow event.
-
void TCBx_CaptureCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at capture event.
-
ISR (TCB0_INT_vect)
-
void TCBx_Initialize (void)
Initializes the TCB module.
-
void TCBx_Start (void)
Starts the TCB counter.
-
void TCBx_Stop (void)
Stops the TCB counter.
-
void TCBx_Write (uint16_t timerVal)
Writes the 16-bit timer value to the TCB.
-
uint16_t TCBx_Read (void)
Reads the 16-bit timer value of the TCB.
-
void TCBx_EnableCaptInterrupt (void)
Enables the capture interrupt for the TCB.
-
void TCBx_EnableOvfInterrupt (void)
Enables the overflow interrupt for the TCB.
-
void TCBx_DisableCaptInterrupt (void)
Disables the capture interrupt for the TCB.
-
void TCBx_DisableOvfInterrupt (void)
Disables the overflow interrupt for the TCB.
-
void TCBx_ClearCaptInterruptFlag (void)
Clears the Capture Interrupt flag after the Capture Interrupt flag is set.
-
void TCBx_ClearOvfInterruptFlag (void)
Clears the Overflow Interrupt flag after the Overflow Interrupt flag is set.
-
bool TCBx_IsCaptInterruptEnabled (void)
Checks if the capture interrupt is enabled.
-
bool TCBx_IsOvfInterruptEnabled (void)
Checks if the overflow interrupt is enabled.
-
void TCBx_PWM_Enable (void)
Enables the PWM mode for the Timer/Counter B peripheral.
-
void TCBx_PWM_Disable (void)
Disables the PWM mode for the Timer/Counter B peripheral.
-
void TCBx_load_counter (TCBx_pwm_register_t counter_value)
Loads the counter value for the 8-bit PWM.
-
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.
-
void TCBx_load_duty_cycle (TCBx_pwm_register_t duty_value)
Loads the duty cycle value for the 8-bit PWM.
Variables
-
const struct TMR_INTERFACETCBx_Interface
-
void(* TCBx_OVF_isr_cb )(void) = NULL
-
void(* TCBx_CAPT_isr_cb )(void) = NULL
Detailed Description
This file contains the API implementation for the TCB0 module driver.
TCB0 Generated Driver File
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.
Variable Documentation
TCBx_CAPT_isr_cb
void(* TCBx_CAPT_isr_cb) (void) = NULL
TCBx_Interface
const struct TMR_INTERFACE TCBx_Interface
Initial value:
= { .Initialize = TCBx_Initialize, .Start = TCBx_Start, .Stop = TCBx_Stop, .PeriodCountSet = TCBx_Write, .TimeoutCallbackRegister = TCBx_OverflowCallbackRegister, .Tasks = NULL }
TCBx_OVF_isr_cb
void(* TCBx_OVF_isr_cb) (void) = NULL
4.14.5.7.2 source/tcb0.h File Reference
#include <stdint.h> #include "../system/utils/compiler.h" #include "./timer_interface.h"
Functions
-
void TCBx_PWM_Enable (void)
Enables the PWM mode for the Timer/Counter B peripheral.
-
void TCBx_PWM_Disable (void)
Disables the PWM mode for the Timer/Counter B peripheral.
-
void TCBx_load_counter (TCBx_pwm_register_t counter_value)
Loads the counter value for the 8-bit PWM.
-
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.
-
void TCBx_load_duty_cycle (TCBx_pwm_register_t duty_value)
Loads the duty cycle value for the 8-bit PWM.
-
void TCBx_CaptureCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at capture event.
-
void TCBx_OverflowCallbackRegister (TCBx_cb_t cb)
Registers a callback function to be called at overflow event.
-
void TCBx_Initialize (void)
Initializes the TCB module.
-
void TCBx_Start (void)
Starts the TCB counter.
-
void TCBx_Stop (void)
Stops the TCB counter.
-
void TCBx_EnableCaptInterrupt (void)
Enables the capture interrupt for the TCB.
-
void TCBx_DisableCaptInterrupt (void)
Disables the capture interrupt for the TCB.
-
void TCBx_EnableOvfInterrupt (void)
Enables the overflow interrupt for the TCB.
-
void TCBx_DisableOvfInterrupt (void)
Disables the overflow interrupt for the TCB.
-
uint16_t TCBx_Read (void)
Reads the 16-bit timer value of the TCB.
-
void TCBx_Write (uint16_t timerVal)
Writes the 16-bit timer value to the TCB.
-
void TCBx_ClearCaptInterruptFlag (void)
Clears the Capture Interrupt flag after the Capture Interrupt flag is set.
-
bool TCBx_IsCaptInterruptEnabled (void)
Checks if the capture interrupt is enabled.
-
void TCBx_ClearOvfInterruptFlag (void)
Clears the Overflow Interrupt flag after the Overflow Interrupt flag is set.
-
bool TCBx_IsOvfInterruptEnabled (void)
Checks if the overflow interrupt is enabled.
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.
Variables
-
const struct TMR_INTERFACETCBx_Interface
Detailed Description
TCB0 Generated Driver API Header File
Variable Documentation
TCBx_Interface
const struct TMR_INTERFACE TCBx_Interface
4.14.5.7.3 source/timer_interface.h File Reference
#include <stddef.h>
Data structures
-
struct TMR_INTERFACE
Detailed Description
Timer Interface Generated Driver API Header File