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

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

3.14.5.2 Supported Device Families

AVR® DxAVR® ExATtinyATmega

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

3.14.5.4 How to use the TCB PLIB

Click the links to view the code snippets associated with each example:

3.14.5.5 Specific MISRA C:2012 Deviations

Global TCB0_MAX_COUNT

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

Global TCB0_Read

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

3.14.5.6 Module Documentation

3.14.5.6.1 TCB0

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

Module description

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

Version: TCB0 Driver Version 2.0.0 Package Version 6.0.0
Definitions
Functions

Definition Documentation

TCB0_ClearCaptInterruptFlag

#define TCB0_ClearCaptInterruptFlag TCB0_CaptureStatusClear

Defines the Custom Name for the TCB0_CaptureStatusClear API. The TCB0_ClearCaptInterruptFlag will be deprecated in the future release. Use TCB0_CaptureStatusClear instead.

TCB0_ClearOvfInterruptFlag

#define TCB0_ClearOvfInterruptFlag TCB0_OverflowStatusClear

Defines the Custom Name for the TCB0_OverflowStatusClear API. The TCB0_ClearOvfInterruptFlag will be deprecated in the future release. Use TCB0_OverflowStatusClear instead.

TCB0_CLOCK_FREQ

#define TCB0_CLOCK_FREQ (4,000,000UL)

Defines the TCB0 frequency in hertz.

TCB0_DisableCaptInterrupt

#define TCB0_DisableCaptInterrupt TCB0_CAPTInterruptDisable

Defines the Custom Name for the TCB0_CAPTInterruptDisable API. The TCB0_DisableCaptInterrupt will be deprecated in the future release. Use TCB0_CAPTInterruptDisable instead.

TCB0_DisableOvfInterrupt

#define TCB0_DisableOvfInterrupt TCB0_OVFInterruptDisable

Defines the Custom Name for the TCB0_OVFInterruptDisable API. The TCB0_DisableOvfInterrupt will be deprecated in the future release. Use TCB0_OVFInterruptDisable instead.

TCB0_EnableCaptInterrupt

#define TCB0_EnableCaptInterrupt TCB0_CAPTInterruptEnable

Defines the Custom Name for the TCB0_CAPTInterruptEnable API. The TCB0_EnableCaptInterrupt will be deprecated in the future release. Use TCB0_CAPTInterruptEnable instead.

TCB0_EnableOvfInterrupt

#define TCB0_EnableOvfInterrupt TCB0_OVFInterruptEnable

Defines the Custom Name for the TCB0_OVFInterruptEnable API. The TCB0_EnableOvfInterrupt will be deprecated in the future release. Use TCB0_OVFInterruptEnable instead.

TCB0_IsCaptInterruptFlag

#define TCB0_IsCaptInterruptFlag TCB0_CaptureStatusGet

Defines the Custom Name for the TCB0_CaptureStatusGet API. The TCB0_IsCaptInterruptFlag will be deprecated in the future release. Use TCB0_CaptureStatusGet instead.

TCB0_IsOvfInterruptFlag

#define TCB0_IsOvfInterruptFlag TCB0_OverflowStatusGet

Defines the Custom Name for the TCB0_OverflowStatusGet API. The TCB0_IsOvfInterruptFlag will be deprecated in the future release. Use TCB0_OverflowStatusGet instead.

TCB0_load_counter

#define TCB0_load_counter TCB0_CounterSet

Defines the Custom Name for the TCB0_CounterSet API. The TCB0_load_counter will be deprecated in the future release. Use TCB0_CounterSet instead.

TCB0_load_duty_cycle

#define TCB0_load_duty_cycle TCB0_DutyCycleSet

Defines the Custom Name for the TCB0_DutyCycleSet API. The TCB0_load_duty_cycle will be deprecated in the future release. Use TCB0_DutyCycleSet instead.

TCB0_load_top

#define TCB0_load_top TCB0_PeriodSet

Defines the Custom Name for the TCB0_PeriodSet API. The TCB0_load_top will be deprecated in the future release. Use TCB0_PeriodSet instead.

TCB0_MAX_COUNT

#define TCB0_MAX_COUNT (65535U)

Defines the TCB0 maximum count value.

MISRA C:2012 Deviation Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

TCB0_PWM_Disable

#define TCB0_PWM_Disable TCB0_Stop

Defines the Custom Name for the TCB0_Stop API. The TCB0_PWM_Disable will be deprecated in the future release. Use TCB0_Stop instead.

TCB0_PWM_Enable

#define TCB0_PWM_Enable TCB0_Start

Defines the Custom Name for the TCB0_Start API. The TCB0_PWM_Enable will be deprecated in the future release. Use TCB0_Start instead.

TCB0_pwm_register_t[1/2]

#define TCB0_pwm_register_t uint8_t

Defines a macro to handle deprecated custom data type. This macro will be deprecated in the future release.

TCB0_Read

#define TCB0_Read TCB0_CounterGet

Defines the Custom Name for the TCB0_CounterGet API. The TCB0_Read will be deprecated in the future release. Use TCB0_CounterGet instead.

MISRA C:2012 Deviation Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

TCB0_Write

#define TCB0_Write TCB0_CounterSet

Defines the Custom Name for the TCB0_CounterSet API. The TCB0_Write will be deprecated in the future release. Use TCB0_CounterSet instead.

Function Documentation

TCB0_CAPTInterruptDisable()

void TCB0_CAPTInterruptDisable (void )

Disables the capture interrupt for the TCB0.

Parameters:
None.
Returns:

None.

TCB0_CAPTInterruptEnable()

void TCB0_CAPTInterruptEnable (void )

Enables the capture interrupt for the TCB0.

Parameters:
None.
Returns:

None.

TCB0_CaptureCallbackRegister()

void TCB0_CaptureCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function to be called during a capture event.

Parameters:
void

(* CallbackHandler)(void) - Pointer to the custom callback

Returns:

None.

TCB0_CaptureStatusClear()

void TCB0_CaptureStatusClear (void )

Clears the Capture Interrupt flag.

Parameters:
None.
Returns:

None.

TCB0_CaptureStatusGet()

bool TCB0_CaptureStatusGet (void )

Checks the Capture Interrupt flag.

Parameters:
None.
Returns:

bool

TCB0_CounterGet()

uint16_t TCB0_CounterGet (void )

Reads the counter value from the CNT register.

Precondition:

Initialize TCB0 with TCB0_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the CNT register

TCB0_CounterSet()

void TCB0_CounterSet (uint16_t counterValue)

Writes the counter value to the CNT register.

Precondition:

Initialize TCB0 with TCB0_Initialize() before calling this API.

Parameters:
counterValue

- Counter value to be written to the CNT register

Returns:

None.

TCB0_Deinitialize()

void TCB0_Deinitialize (void )

Deinitializes the TCB0 module.

Parameters:
None.
Returns:

None.

TCB0_DutyCycleSet()

void TCB0_DutyCycleSet (uint8_t duty_value)

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

Parameters:
value

- PWM duty cycle value

Returns:

None.

TCB0_Initialize()

void TCB0_Initialize (void )

Initializes the TCB0 module.

Parameters:
None.
Returns:

None.

TCB0_IsCaptInterruptEnabled()

bool TCB0_IsCaptInterruptEnabled (void )

Checks if the capture interrupt is enabled.

Parameters:
None.
Returns:

None.

TCB0_IsOvfInterruptEnabled()

bool TCB0_IsOvfInterruptEnabled (void )

Checks if the overflow interrupt is enabled.

Parameters:
None.
Returns:

None.

TCB0_MaxCountGet()

uint16_t TCB0_MaxCountGet (void )

Gets the maximum timer count value.

Parameters:
None.
Returns:

Maximum count value

TCB0_OverflowCallbackRegister()

void TCB0_OverflowCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function to be called during an overflow event.

Parameters:
void

(* CallbackHandler)(void) - Pointer to the custom callback

Returns:

None.

TCB0_OverflowStatusClear()

void TCB0_OverflowStatusClear (void )

Clears the Overflow Interrupt flag.

Parameters:
None.
Returns:

None.

TCB0_OverflowStatusGet()

bool TCB0_OverflowStatusGet (void )

Checks the Overflow Interrupt flag.

Parameters:
None.
Returns:

bool

TCB0_OVFInterruptDisable()

void TCB0_OVFInterruptDisable (void )

Disables the overflow interrupt for the TCB0.

Parameters:
None.
Returns:

None.

TCB0_OVFInterruptEnable()

void TCB0_OVFInterruptEnable (void )

Enables the overflow interrupt for the TCB0.

Parameters:
None.
Returns:

None.

TCB0_PeriodGet()

uint16_t TCB0_PeriodGet (void )

Gets the current period value from the CCMP register.

Precondition:

Initialize TCB0 with TCB0_Initialize() before calling this API.

Parameters:
None.
Returns:

Period value from the CCMP register

TCB0_PeriodSet()

void TCB0_PeriodSet (uint8_t periodVal)

Loads the period count value to the CCMP register.

Precondition:

Initialize TCB0 with TCB0_Initialize() before calling this API.

Parameters:
periodVal

- Count value written to the CCMP register

Returns:

None.

TCB0_Start()

void TCB0_Start (void )

Starts the TCB0 counter.

Parameters:
None.
Returns:

None.

TCB0_Stop()

void TCB0_Stop (void )

Stops the TCB0 counter.

Parameters:
None.
Returns:

None.

TCB0_Tasks()

void TCB0_Tasks (void )

Performs the tasks to be executed after the TCB0 overflow event.

Parameters:
None.
Returns:

None.

Performs the tasks to be executed after the TCB0 overflow event.

Parameters:
None.
Returns:

None.

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.

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

3.14.5.7 TCB Examples

This section explains various use case examples of the Timer module within an MCC Melody Project.

3.14.5.7.1 Timer Driver Examples

4 ms/8 ms Timer Period Change During Each Interrupt Event

This use case configures the Timer/Counter Type B (TCB) module to use the TCB0 Peripheral Library (PLIB) to generate an capture interrupt. The period of a timer is changed at run time on each interrupt event.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 4 MHz system clock

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TCB0

    • Custom Name: TCB

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 10 ms

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal High Frequency Oscillator (4MHz)
*     Timer module: Timer PLIB Selector - TCB0, Custom Name - TCB0, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 10 ms
*     Pins module: PC6 - GPIO Output, CustomName - LED 
*/

#define TIMER_TICK_FREQ TCB0_CLOCK_FREQ
#define MS_TO_TICKS(ms) ((ms * TIMER_TICK_FREQ)/(1000UL))
#define LED_4_MS MS_TO_TICKS(4UL)
#define LED_8_MS MS_TO_TICKS(8UL)

static const struct TIMER_INTERFACE *Timer = &TCB;
static void Timer_PeriodChange(void)
{ 
    static volatile bool changePeriod = false;
    LED_Toggle();
    if(true == changePeriod)
    {     
        if(Timer->MaxCountGet() > LED_4_MS)
        {
            Timer->PeriodSet(LED_4_MS);
        } 
        else 
        {
            // Invalid period
        }
        changePeriod = false;
    }
    else
    {
        if(Timer->MaxCountGet() > LED_8_MS)
        {
            Timer->PeriodSet(LED_8_MS);
        } 
        else 
        {
            // Invalid period
        }
        changePeriod = true;
    }
}

int main(void)
{
    SYSTEM_Initialize();
    
    Timer->PeriodSet(LED_4_MS);
    Timer->TimeoutCallbackRegister(Timer_PeriodChange);
    Timer->Start();
    
    while(1)
    {
    }    
}

10 ms Timer Period in Non-Interrupt Mode

This use case configures the TCB module to use TCB PLIB to generate specified timer period in Non-Interrupt mode.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 4 MHz

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TCB0

    • Custom Name: TCB

    • Interrupt Driven: False

    • Requested Period: 10 ms

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal High Frequency Oscillator (4MHz)
*     Timer module: Timer PLIB Selector - TCB0, Timer Enable - False, Interrupt Driven - False, Requested Timer Period - 10 ms
*     Pins module: PC6 - GPIO Output, CustomName - LED 
*/

static void Timer_Callback(void)
{
    LED_Toggle();   
}

int main(void)
{
    SYSTEM_Initialize();

    TCB.TimeoutCallbackRegister(Timer_Callback);
    TCB.Start();
    while(1)
    {
        TCB.Tasks();
    }    
}

40 ms/80 ms Timer Period Change When Switch is Pressed

This use case sets up a project which changes the time-out period between 40 ms and 80 ms when a switch is pressed.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 1 MHz

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

    • Pin Grid View: Select SWITCH pin as input (check the schematic for your board)

    • Pins: Rename Custom Name to "SW"

    • Enable Weak Pullup if needed

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TCB0

    • Custom Name: TCB

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 100 ms

  • Drivers>TCB0:
    • Clock Selection: DIV2

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
#include <util/delay.h>

/*
 * Development Board: AVR128DA48 Curiosity Nano Board
 * MCC Configuration:
 *     Clock Control: Internal High Frequency Oscillator (1 MHz)
 *     Timer module: Timer PLIB Selector - TCB0, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 100 ms
 *     TCB0 module: Clock Selection - DIV2
 *     Pins module: RC6 - GPIO Output, CustomName - LED
 *                  RC7 - GPIO Input, CustomName - SW, Start High - Enable 
 */

#define SW_PRESSED 0U          // Switch - Active Low
#define SW_NOT_PRESSED 1U        
#define DEBONCE_DELAY_MS 20U      // Depends on hardware

#define MS_TO_TICKS(ms) (((TCB0_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_40_MS (MS_TO_TICKS(40UL))           
#define LED_80_MS (MS_TO_TICKS(80UL))

static const struct TIMER_INTERFACE *Timer = &TCB;

bool SW_StateGet(void)
{
    bool status = SW_NOT_PRESSED;
    if(SW_PRESSED == SW_GetValue())
    {
        _delay_ms(DEBONCE_DELAY_MS);
        if(SW_PRESSED == SW_GetValue())
        {
            status = SW_PRESSED;
        }        
    }
    return status;
}

void Timer_FrequencyChange(void)
{
    static volatile bool changePeriod = false;
    Timer->Stop();
    
    uint32_t maxCount = Timer->MaxCountGet();
    uint32_t newPeriod = changePeriod ? LED_40_MS:LED_80_MS;
    
    if(maxCount > newPeriod)
    {      
        Timer->PeriodSet(newPeriod);
    }
    else
    {
        // Invalid period
    }
    changePeriod = !changePeriod;
    Timer->Start(); 
}

void Timer_Callback(void)
{
    LED_Toggle();
}

int main(void)
{
    SYSTEM_Initialize();

    Timer->PeriodSet(LED_40_MS);
    Timer->TimeoutCallbackRegister(Timer_Callback);
    Timer->Start();    
 
    
    while(1)
    {
        if(SW_PRESSED == SW_StateGet())
        {
            Timer_FrequencyChange();
        }
    }    
}

A Simple Task Scheduler That Executes Two Different Tasks at Different Intervals Using Timer Interrupt

This use case sets up a project which executes two different tasks at different intervals using a timer interrupt.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 4 MHz

  • System > Interrupt Manager:
    • Global Interrupt Enable: True

  • System>Pins:
    • Pin Grid View: Select LED1 pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED1"

    • Pin Grid View: Select LED2 pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED2"

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TCB0

    • Custom Name: TCB

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 1 ms

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
 * Development Board: AVR128DA48 Curiosity Nano Board
 * MCC Configuration:
 *     Clock Control: Internal High Frequency Oscillator (4 MHz)
 *     Timer module: Timer PLIB Selector - TCB0, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 1 ms
 *     TCB0 module: Clock Selection - DIV1
 *     Interrupt module:  Global Interrupt Enable - True
 *     Pins module: RC5 - GPIO Output, CustomName - LED1
 *                  RC6 - GPIO Output, CustomName - LED2
 */

static void Task_A_2ms(void)
{
    LED1_SetHigh();
    NOP();
    NOP();
    LED1_SetLow();
    
    // Add your code here. E.g., LED Toggle
}
static void Task_B_5ms(void)
{
    LED2_SetHigh();
    NOP();
    NOP();
    LED2_SetLow();  
    
    // Add your code here. E.g., Read Sensor
}
static void Tasks_Scheduler(void)
{
    static volatile uint8_t oneMs_tickCounter =  0;
    
    oneMs_tickCounter++;
            
    if((oneMs_tickCounter % 2U) == 0U)
    {
        Task_A_2ms();    // Run at 2 ms, 4 ms, 6 ms, 8 ms and 10 ms.
    }
    
    if((oneMs_tickCounter % 5U) == 0U)
    {
        Task_B_5ms();    // Run at 5 ms and 10 ms.
    }
    if(oneMs_tickCounter == 10U)
    {
        oneMs_tickCounter = 0;
    }
}

int main(void)
{
    const struct TIMER_INTERFACE *Timer = &TCB;

    SYSTEM_Initialize();
 
    Timer->TimeoutCallbackRegister(Tasks_Scheduler);
    Timer->Start();
    

    while(1)
    {
    }    
}

3.14.5.7.2 TCB PLIB Examples

4 ms/8 ms Timer Period Change in Non-Interrupt Mode

This use case configures TCB0 PLIB to generate the timer period. The period of a timer is changed at run time each capture event occurred. The callback function is used to change the timer period.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 4 MHz

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

Timer Configuration
  • Drivers>TCB0:
    • Timer Enable: False

    • Timer Mode: INT

    • Requested Timeout: 10 ms

    • Capture/Timeout Interrupt Enable: False

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal High Frequency Oscillator (4 MHz)
*     TCB0 module: Timer Enable - False, Interrupt Driven - True, Timer Mode - INT, Capture/Timeout Interrupt Enable - False 
*     Pins module: PC6 - GPIO Output, CustomName - LED
*/ 

#define  USE_TASKS_API 1

#define MS_TO_TICKS(ms) (((TCB0_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS MS_TO_TICKS(4UL)
#define LED_8_MS MS_TO_TICKS(8UL)

static void Timer_PeriodChange(void)
{ 
    static volatile bool changePeriod = false;
    LED_Toggle();
    if(true == changePeriod)
    {     
        if(TCB0_MaxCountGet() > LED_4_MS)
        {
            TCB0_PeriodSet(LED_4_MS);
        } 
        else 
        {
            // Invalid period
        }
        changePeriod = false;
    }
    else
    {
        if(TCB0_MaxCountGet() > LED_8_MS)
        {
            TCB0_PeriodSet(LED_8_MS);
        } 
        else 
        {
            // Invalid period
        }
        changePeriod = true;
    }
}

int main(void)
{
    SYSTEM_Initialize();
    
    TCB0_PeriodSet(LED_4_MS);
    TCB0_CaptureStatusClear();
    
#if USE_TASKS_API    
    TCB0_CaptureCallbackRegister(Timer_PeriodChange);
#endif
    TCB0_Start();
    
    while(1)
    {
        #if   USE_TASKS_API
            TCB0_Tasks();
        #else
            if(1 == TCB0_CaptureCheck())
            {
                Timer_PeriodChange();
                TCB0_CaptureStatusClear();
            }
        #endif
    }    
}

Toggle the LED after 3s of Pressing the Switch

This use case configures the TCB0 PLIB in Single-Shot mode. Switch is used to trigger an event with a 3s time-out, after which the LED is toggled.

System Configuration
  • System>Clock Control:
    • Clock Selection: Internal 32.768 kHz Oscillator

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

    • Pin Grid View: Select Switch pin as input (Check the schematic for your board)

    • Pins: Rename Custom Name to "SW0"

Timer Configuration
  • Drivers>TCB0:
    • Timer Enable: False

    • Timer Clock Frequency: DIV2

    • Timer Mode: SINGLE

    • Requested Timeout (s): 3s

    • Edge Event: True

    • Event Input Capture Enable: True

    • Capture/Timeout Interrupt Enable: True

Event System Configuration
  • Drivers>EVSYS:
    • Generators: Select SW0 pin (PC7 in this case, channel 2)

    • Channels: SW0 pin channel (channel 2)

    • Users: TCB0CAPT

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal 32.768 kHz Oscillator
*     Interrupt Manager:  Global Interrupt Enable - True
*     TCB0 module: Timer Enable - False, Timer Clock Frequency - DIV2, Timer Mode - SINGLE, Requested Timeout (s) - 3s, Capture/Timeout Interrupt Enable - True, Edge Event - True, Event Input Capture Enable - True, Pin Output Enable - True, Pin Initial State - Low 
*     Pins module:  PC6 - GPIO Output, CustomName - LED, PC7 - GPIO Output, CustomName - SW0
*     Event System: Generators - Select SW0 pin, Channels - SW0 pin channel, Users - TCB0CAPT  
*/

static void Timer_Callback(void){
    LED_Toggle();
}

int main(void)
{
    SYSTEM_Initialize();
    LED_SetHigh();
    TCB0_CaptureCallbackRegister(&Timer_Callback);
    TCB0_Start();
    while(1)
    {
        
    }
}

Timer Wake From Sleep at Every 3s

This use case configures the TCB PLIB to wake the microcontroller from Sleep mode, every three seconds. The wake up event toggles an LED, then puts the microcontroller (MCU) back to Sleep.

System Configuration
  • System>Clock Control:
    • Clock Selection: Internal 32.768 kHz Oscillator

    • Run Standby 32 kHz Oscillator: True

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

  • System>SLPCTRL:
    • Sleep Enable: True

    • Sleep Mode: STDBY

    • Performance Mode: FULL

Timer Configuration
  • Drivers>TCB0:
    • Timer Enable: True

    • Timer Clock Frequency: DIV2

    • Timer Mode: INT

    • Requested Timeout (s): 3s

    • Run Standby: True

    • Capture/Timeout Interrupt Enable: True

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
#include "avr/sleep.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     CLKCTRL module: Internal 32.768 kHz oscillator, Run Standby 32 kHz Oscillator - True
*     TCB0 module: Timer Enable - True, Timer Clock Frequency - DIV2, Timer Mode - INT, Requested Timeout - 3s, Run Standby - True, Capture/Timeout Interrupt Enable - True
*     Pins module: PC6 - GPIO Output, CustomName - LED
*     Interrupt Manager:  Global Interrupt Enable - True
*     SLPCTRL module: Sleep Enable - True, Sleep Mode - STDBY, Performance Mode - FULL
*     
*/
int main(void)
{
    SYSTEM_Initialize(); 

    while(1)
    {
        LED_Toggle();
        sleep_mode();
        
        // Add your code here to execute after exiting Sleep mode 
    }    
}

Generate PWM Signal with 50% Duty Cycle and 1 ms Period

This use case configures the TCB0 PLIB to generate the PWM signal. The period and duty cycle are set in using UI configuration.

System Configuration
  • System>Clock Control:
    • Clock Selection: Internal 32.768 kHz Oscillator

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

  • System>Pins:
    • Pin Grid View: Lock on of the pins from TCB0 WO Pins

Timer Configuration
  • Drivers>TCB0:
    • Timer Enable: False

    • Timer Clock Frequency: DIV1

    • Timer Mode: PWM8

    • Requested Timeout (s): 1 ms

    • Requested Duty Cycle (%): 50

    • Capture/Timeout Interrupt Enable: False

    • Pin Output Enable: True

    • Pin Initial State: Low

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal 32.768 KHz Oscillator
*     TCB0 module: Timer Enable - False, Timer Clock Frequency - DIV1,Timer Mode - PWM8, Requested Timeout (s) - 1 ms, Requested Duty Cycle (%) - 50, Capture/Timeout Interrupt Enable - False, Pin Output Enable - True, Pin Initial State - Low 
*     Pins module:  RF4 - WO
*/

int main(void)
{
    SYSTEM_Initialize();
    TCB0_Start();
}

Count Number of Times a Push Button Is Pressed

This use case configures the TCB0 PLIB in Single-Shot mode. A capture callback is triggered for every rising edge event, which is triggered by an on-board push button. For every press event, the switch press count is displayed though serial terminal. This example is implemented with switch debounce.

System Configuration
  • System>Clock Control:
    • Clock Source: Internal High Frequency Oscillator

    • Oscillator Frequency Selection: 1 MHz system clock

  • System>Interrupt Manager:
    • Global Interrupt Enable: True

Timer Configuration
  • Drivers>TCB0:
    • Timer Enable: False

    • Timer Clock Frequency: DIV2

    • Timer Mode: SINGLE

    • Requested timeout: 15 ms

    • Edge Event: False

    • Event Input Capture Enable: True

    • Noise Cancellation Filter Enable: True

    • Capture/Timeout Interrupt Enable: True

Event System Configuration
  • Drivers>EVSYS:
    • Generators: Select SW0 pin

    • Channels: SW0 pin channel

    • Users: TCB0CAPT

UART Configuration
  • Drivers>UART:
    • UART PLIB Selector: USART1 (check which instance of UART on your board supports serial termial)

    • Requested Baudrate: 9600

    • Redirect Printf to UART: True

  • System>Pins:
    • Pin Grid View: Lock the appropriate UART1 pin pair (RX and TX) to support serial display (refer to the dev board data sheet)

    • Pin Grid View: Select Switch pin as input (Check the schematic for your board)

    • Pins UI : Rename Custom Name to "SW0"

    • Pins UI : SW0 -> Pull-up - True

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
* Development Board: AVR128DA48 Curiosity Nano Board
* MCC Configuration:
*     Clock Control: Internal High Frequency Oscillator (1 MHz)
*     TCB0 module: Timer Enable - False, Timer Clock Frequency - DIV2, Timer Mode - SINGLE, Requested Timeout - 15 ms, Capture/Timeout Interrupt Enable - True, Edge Event - False, Event Input Capture Enable - True, Noise Cancellation Filter Enable - True 
*     Pins module: PC7 - GPIO Input, CustomName - SW0
*     Interrupt Manager:  Global Interrupt Enable - True
*     Event System: Generators - Select SW0 pin, Channels - SW0 pin channel, Users - TCB0CAPT  
*     UART Driver: UART PLIB Selector - USART1, Requested Baudrate - 9600, Redirect Printf to UART - True
*/

#define DEBOUNCE_DELAY_MS 20U  //Change this delay value based on your application requirements
#define COUNT (((DEBOUNCE_DELAY_MS * TCB0_CLOCK_FREQ)/1000U ) - 1U)

static unsigned int SwitchCount = 0;

static void Timer_callback(void){
    SwitchCount++;
    printf("the switch is pressed %d times \n" , SwitchCount);
}

int main(void)
{
    SYSTEM_Initialize();
    TCB0_CaptureCallbackRegister(Timer_callback);
    TCB0_PeriodSet(COUNT) ;
    TCB0_Start();
    while(1){  
    }
}

3.14.5.8 File Documentation

3.14.5.8.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 2.0.0 Package Version 6.0.0

Function Documentation

ISR()

* cppcheck suppress misra c2012* ISR (TCB0_INT_vect )

TCB0_DefaultCaptureCallback()

static void TCB0_DefaultCaptureCallback (void )[static]

TCB0_DefaultOverflowCallback()

static void TCB0_DefaultOverflowCallback (void )[static]

Variable Documentation

TCB0_CAPT_isr_cb

void(* TCB0_CAPT_isr_cb) (void)[static]

TCB0_OVF_isr_cb

void(* TCB0_OVF_isr_cb) (void)[static]

Section: Global Variables Definitions

3.14.5.8.2 source/tcb0.h File Reference

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

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

Functions

Macros

  • #define TCB0_MAX_COUNT (65535U)

    Defines the TCB0 maximum count value.

  • #define TCB0_CLOCK_FREQ (4,000,000UL)

    Defines the TCB0 frequency in hertz.

Detailed Description

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

TCB0 Generated Driver API Header File

TCB0 Generated Driver API Header File

Version: TCB0 Timer Driver Version 2.0.0 Package Version 6.0.0

Function Documentation

TCB0_Tasks()

void TCB0_Tasks (void )

Performs the tasks to be executed on timer events.

Parameters:
None.
Returns:

None.

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.

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

3.14.5.8.3 source/tcb0_deprecated.h File Reference

Macros

3.14.5.8.4 source/tcb0_drv.c File Reference

#include "../tcb0.h"

Functions

Function Documentation

ISR()

ISR (TCB0_INT_vect )

TCB0_DefaultCaptureCallback()

static void TCB0_DefaultCaptureCallback (void )[static]

3.14.5.8.5 source/tcb0_drv.h File Reference

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

Functions

  • void TCB0_Initialize (void)

    Initializes the TCB0 module. This routine must be called before any other TCB0 routines.

  • void TCB0_Deinitialize (void)

    Deinitializes the TCB0 to POR values.

  • void TCB0_Start (void)

    Starts the TCB0.

  • void TCB0_Stop (void)

    Stops the TCB0.

  • void TCB0_CounterSet (uint32_t CounterValue)

    Writes the counter value to the CNT register.

  • uint32_t TCB0_CounterGet (void)

    Reads the counter value from the CNT register.

  • void TCB0_PeriodSet (uint32_t periodCount)

    Loads the 16-bit value to the timerTCB0ReloadVal variable.

  • uint32_t TCB0_PeriodGet (void)

    Returns the TCB0 period count value.

  • uint32_t TCB0_MaxCountGet (void)

    Returns the TCB0 maximum timer count value.

  • void TCB0_CaptureCallbackRegister (void(*CallbackHandler)(void))

    Setter function for the TCB0 overflow callback.

  • void TCB0_Tasks (void)

    Performs the tasks to be executed after the TCB0 overflow event.

Macros

Variable Documentation

Timer0

const struct TIMER_INTERFACE Timer0

Section: Included Files

3.14.5.8.6 source/TCB_example.dox File Reference