Atmel AT32UC3
Home - Developer - Contact us





Chapter 1: Start

Chapter 2: Clocks

Chapter 3: Port

Chapter 4: Interrupt

Chapter 5: Timer

Chapter 6: Real Time

Chapter 7: Serial Link


Lecture Material


Chapter 5

The way that the LED-action is programmed in Chapter 1 to 4 is for sure not elegant - using a loop for a delay time of the order of 100ms is very unpractical. The AT32UC3A3 series of controllers have advanced controlling possibilities and timers that we will use in this chapter together with interrupt routines to streamline the LED-runner program.


To implement long delays by loops is a waste of computing power and requires, e.g., to switch off any compiler optimization. The AT32UC3A3 series of controllers contains 6 channels of 16-bit Timer/Counters. Each timer/counter has a huge number of options available for the clock selection, mode of operation and I/O-connections. We will deal here with a simple example, an interrupt-driven delay circuit.

First let us select the clock: we want to get an interrupt every 1ms; then we can choose any multiple of it by a counter in the interrupt routine. This means that the 'Waveform Mode' is the appropriate mode: it is cyclic and and we can achieve the switch back to zero after a positive result of a comparison automatically.

The timer/counter can be programmed either via ASF or via predefined definitions and structures in the file tc_223.h. This file can easiest be found in the 'Dependencies' part of the 'Solutions Explorer'. We will now specify the content of these structures for our project. The structure of type avr32_tc_channel_t specifies all the options - here for the 'Waveform mode':

We use the internal clock 'PBA', which we set to 40MHz (see Chapter 2), divided by 2. This is the clock input TIMER_CLOCK2 as defined in the 'Module Configuration' in the datasheet. To get an interrupt every 1ms, the timer needs to perform an additional division by 20,000.


Application_5 keeps the logic for the 'running' LEDs as well as the initialization in the user.c program. Application_5 uses direct register setting for the Timer/Counter just like it does for the GPIO. It is just a bit more code, but keeps the access of the hardware transparent.

The initialization routines are called from init.c, the general board initialization routine. To start the timer, switching on the clock is not suffiecient, one has also to execute a 'software trigger':

Application_5 is setup the same way as the other application programs. If the STK600 oscillator is set to 12MHz, one loop of the LEDs should take exactly 1s in the lowest speed setting.

Application_5a performs the same action as Application_5, but uses the ASF-software. You need to include the 'Timer/Counter' driver using the ASF-wizzard. The driver will be located in the directory 'src/ASF/avr32/drivers/tc'. Apart from using the ASF-software, the function and structure of Application_5a is the same as that of Application_5.