# tc_example1.c File Reference

## Detailed Description

Timer/Counter example 1.

This example will start a timer/counter and generate a PWM on the output.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices with a TC module can be used.
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file tc_example1.c.

`#include <avr32/io.h>`
`#include "compiler.h"`
`#include "board.h"`
`#include "pm_at32ap7000.h"`
`#include "gpio.h"`
`#include "tc.h"`

Go to the source code of this file.

## Defines
TC channel choice
#define | EXAMPLE_TC AVR32_TC0
#define | EXAMPLE_TC_CHANNEL_FUNCTION AVR32_TC0_A0_0_FUNCTION
#define | EXAMPLE_TC_CHANNEL_ID 0
#define | EXAMPLE_TC_CHANNEL_PIN AVR32_TC0_A0_0_PIN
## Functions
int | main (void)
| Main function. Execution starts here.

## Define Documentation

#define EXAMPLE_TC AVR32_TC0

Definition at line 125 of file tc_example1.c.

Referenced by main().

#define EXAMPLE_TC_CHANNEL_FUNCTION AVR32_TC0_A0_0_FUNCTION

Definition at line 128 of file tc_example1.c.

Referenced by main().

#define EXAMPLE_TC_CHANNEL_ID 0

Definition at line 126 of file tc_example1.c.

Referenced by main().

#define EXAMPLE_TC_CHANNEL_PIN AVR32_TC0_A0_0_PIN

Definition at line 127 of file tc_example1.c.

Referenced by main().

## Function Documentation

int main | ( | void |  | ) |

Main function. Execution starts here.

Definition at line 140 of file tc_example1.c.

References EXAMPLE_TC, EXAMPLE_TC_CHANNEL_FUNCTION, EXAMPLE_TC_CHANNEL_ID, EXAMPLE_TC_CHANNEL_PIN, TC_BURST_NOT_GATED, TC_CLOCK_RISING_EDGE, TC_CLOCK_SOURCE_TC3, TC_EVT_EFFECT_NOOP, TC_EVT_EFFECT_TOGGLE, TC_EXT_EVENT_SEL_TIOB_INPUT, tc_init_waveform(), TC_SEL_NO_EDGE, tc_start(), TC_WAVEFORM_SEL_UP_MODE, tc_write_ra(), and tc_write_rc().

    00141 {
    00142   // The timer/counter instance and channel number are used in several functions.
    00143   // It's defined as local variable for ease-of-use causes and readability.
    00144   volatile avr32_tc_t *tc = &EXAMPLE_TC;
    00145
    00146   // Options for waveform genration.
    00147   tc_waveform_opt_t waveform_opt =
    00148   {
    00149     .channel  = EXAMPLE_TC_CHANNEL_ID,        // Channel selection.
    00150
    00151     .bswtrg   = TC_EVT_EFFECT_NOOP,           // Software trigger effect on TIOB.
    00152     .beevt    = TC_EVT_EFFECT_NOOP,           // External event effect on TIOB.
    00153     .bcpc     = TC_EVT_EFFECT_NOOP,           // RC compare effect on TIOB.
    00154     .bcpb     = TC_EVT_EFFECT_NOOP,           // RB compare effect on TIOB.
    00155
    00156     .aswtrg   = TC_EVT_EFFECT_NOOP,           // Software trigger effect on TIOA.
    00157     .aeevt    = TC_EVT_EFFECT_NOOP,           // External event effect on TIOA.
    00158     .acpc     = TC_EVT_EFFECT_TOGGLE,         // RC compare effect on TIOA: toggle.
    00159     .acpa     = TC_EVT_EFFECT_TOGGLE,         // RA compare effect on TIOA: toggle (other possibilities are none, set and clear).
    00160
    00161     .wavsel   = TC_WAVEFORM_SEL_UP_MODE,      // Waveform selection: Up mode without automatic trigger on RC compare.
    00162     .enetrg   = FALSE,                        // External event trigger enable.
    00163     .eevt     = TC_EXT_EVENT_SEL_TIOB_INPUT,  // External event selection.
    00164     .eevtedg  = TC_SEL_NO_EDGE,               // External event edge selection.
    00165     .cpcdis   = FALSE,                        // Counter disable when RC compare.
    00166     .cpcstop  = FALSE,                        // Counter clock stopped with RC compare.
    00167
    00168     .burst    = TC_BURST_NOT_GATED,           // Burst signal selection.
    00169     .clki     = TC_CLOCK_RISING_EDGE,         // Clock inversion.
    00170     .tcclks   = TC_CLOCK_SOURCE_TC3           // Internal source clock 3, connected to fPBB / 8.
    00171   };
    00172
    00173 #if BOARD == STK1000
    00174   // Reset PM. Makes sure we get the expected clocking after a soft reset (e.g.: JTAG reset)
    00175   pm_reset();
    00176
    00177   // Start PLL0 giving 96 MHz clock
    00178   pm_pll_opt_t pll_opt = {
    00179     .pll_id = 0,
    00180     .mul = 24,
    00181     .div = 5,
    00182     .osc_id = 0,
    00183     .count = 16,
    00184     .wait_for_lock = 1,
    00185   };
    00186   pm_start_pll(&pll_opt);
    00187
    00188   // Divide HSB by 2 and PBA by 4 to keep them below maximum ratings
    00189   pm_set_clock_domain_scaler(PM_HSB_DOMAIN, 2);
    00190   pm_set_clock_domain_scaler(PM_PBA_DOMAIN, 4);
    00191
    00192   // Divide 96 MHz by 8 to get 12 MHz PBB clock used for TIMER 0
    00193   pm_set_clock_domain_scaler(PM_PBB_DOMAIN, 8);
    00194
    00195   // Use PLL0 as clock source
    00196   pm_set_mclk_source(PM_PLL0);
    00197 #endif
    00198
    00199   // Assign I/O to timer/counter channel pin & function.
    00200   gpio_enable_module_pin(EXAMPLE_TC_CHANNEL_PIN, EXAMPLE_TC_CHANNEL_FUNCTION);
    00201
    00202   // Initialize the timer/counter.
    00203   tc_init_waveform(tc, &waveform_opt);  // Initialize the timer/counter waveform.
    00204
    00205   // Set the compare triggers.
    00206   tc_write_ra(tc, EXAMPLE_TC_CHANNEL_ID, 0x0600);     // Set RA value.
    00207   tc_write_rc(tc, EXAMPLE_TC_CHANNEL_ID, 0x2000);     // Set RC value.
    00208
    00209   // Start the timer/counter.
    00210   tc_start(tc, EXAMPLE_TC_CHANNEL_ID);
    00211
    00212   while (TRUE);
    00213 }