© EnSol AS 2018
EnSol AS

A Simple Timer routine:

Accessing the timer / counters on the AVR-32 chips is a little harder than you might expect, as finding the actual registers in the AVR studio software takes a fair bit of trial and error! The simple routine shown below initialises Timer0 and programs the Compare register of part A of this timer. A simple while loop is included for illustration, which waits for a compare match before exiting the loop. An LED previously set up, (on port PC18) is toggled with each cycle to allow the execution to be verified. In this case the timer is reset by the software with each cycle, rather than using it in continuous wave mode, as it is the ultimate intention to use this timer a simple one-shot time delay. Note that the clock source is selected to be TIMER_CLOCK2 (see page 850) which according to page 869 is PBC clock / 2 and more details of this clock are found in the Power Management section of the data sheet. By default PBC clock runs at the main system clock frequency (see page 63). (See the USART section of this blog on how to change the system clock settings). One thing to note with the timers is that each Timer actually has 3 channels A, B & C although only A & B can access input & output pins.)
//================================================================================== void Initialise_Timer0(void) { // Setup Timer 0 AVR32_TC0.channel[0].CMR.waveform.wave=1; // Set to waveform mode AVR32_TC0.channel[0].CMR.waveform.tcclks=1; // Set to TIMER_CLOCK2 source  (system clock PBC Clock /2) AVR32_TC0.channel[0].ra=2000; // Set counter A0 compare value AVR32_TC0.channel[0].CCR.clken=1; // Enable the clock AVR32_TC0.channel[0].CCR.swtrg=1; // Reset and start clock while(1) { AVR32_TC0.channel[0].CCR.swtrg=1; // Reset and start clock while (!(AVR32_TC0.channel[0].SR.cpas) & 0b1 ) { // Loop until Compare Match occurs ; } AVR32_GPIO.port[2].ovrt=1<<18; // Toggle LED on pin PC18 } } //==================================================================================