Xmega Application Note | |||||
This file contains an example application that demonstrates the driver for the Timer/Counter extension modules.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file tc_extensions_example.c.
#include "avr_compiler.h"
#include "hires_driver.h"
#include "awex_driver.h"
Go to the source code of this file.
Defines | |
#define | DEAD_TIME_CYCLES 4 |
#define | TIMER_TOP_VALUE 0xFFFF |
Functions | |
void | ConfigClockSystem (void) |
This function enables the internal 32MHz oscillator and the prescalers needed by the HiRes extension. | |
void | ConfigDTI (uint8_t deadTime) |
This function configures the Dead-time insertion extension. | |
void | ConfigFaultProtection (void) |
This function configures fault protection, using the falling edge of PD0 as fault input through event channel 0. | |
ISR (TCC0_OVF_vect) | |
Timer/Counter interrupt service routine. | |
int | main (void) |
Variables | |
uint16_t | sineTable [256] |
#define DEAD_TIME_CYCLES 4 |
Dead time length, given in main system clock cycles.
Definition at line 67 of file tc_extensions_example.c.
Referenced by main().
#define TIMER_TOP_VALUE 0xFFFF |
Top value for the Timer/Counter. Determines frequency and resolution of sine wave output.
Definition at line 64 of file tc_extensions_example.c.
Referenced by main().
void ConfigClockSystem | ( | void | ) |
This function enables the internal 32MHz oscillator and the prescalers needed by the HiRes extension.
Definition at line 176 of file tc_extensions_example.c.
Referenced by main().
00177 { 00178 /* Start internal 32MHz RC oscillator. */ 00179 OSC.CTRL = OSC_RC32MEN_bm; 00180 00181 do { 00182 /* Wait while oscillator stabilizes. */ 00183 } while ( ( OSC.STATUS & OSC_RC32MRDY_bm ) == 0 ); 00184 00185 /* Enable prescaler B and C. */ 00186 CCP = CCP_IOREG_gc; 00187 CLK.PSCTRL = CLK_PSBCDIV_2_2_gc; 00188 00189 /* Select 32 MHz as master clock. */ 00190 CCP = CCP_IOREG_gc; 00191 CLK.CTRL = CLK_SCLKSEL_RC32M_gc; 00192 00193 }
void ConfigDTI | ( | uint8_t | deadTime | ) |
This function configures the Dead-time insertion extension.
deadTime | The dead time to insert between the pulses. |
Definition at line 200 of file tc_extensions_example.c.
References AWEX_EnableDeadTimeInsertion(), AWEX_SetDeadTimesSymmetricalUnbuffered, and AWEX_SetOutputOverrideValue.
Referenced by main().
00201 { 00202 /* Configure dead time insertion. */ 00203 AWEX_EnableDeadTimeInsertion( &AWEXC, AWEX_DTICCAEN_bm ); 00204 AWEX_SetOutputOverrideValue( AWEXC, 0x03 ); 00205 AWEX_SetDeadTimesSymmetricalUnbuffered( AWEXC, deadTime ); 00206 }
void ConfigFaultProtection | ( | void | ) |
This function configures fault protection, using the falling edge of PD0 as fault input through event channel 0.
Definition at line 211 of file tc_extensions_example.c.
References AWEX_ConfigureFaultDetection().
Referenced by main().
00212 { 00213 /* Configure PD0 as input, trigger on falling edge. */ 00214 PORTD.DIRCLR = 0x01; 00215 PORTD.PIN0CTRL = PORT_ISC_FALLING_gc; 00216 00217 /* Select PD0 as input for event channel 0 multiplexer. */ 00218 EVSYS.CH0MUX = EVSYS_CHMUX_PORTD_PIN0_gc; 00219 00220 /* Enable fault detection for AWEX C, using event channel 0. */ 00221 AWEX_ConfigureFaultDetection( &AWEXC, AWEX_FDACT_CLEARDIR_gc, EVSYS_CHMUX0_bm ); 00222 }
ISR | ( | TCC0_OVF_vect | ) |
Timer/Counter interrupt service routine.
This interrupt service routine is responsible for updating the Timer/Counter compare value once every PWM cycle to produce a sine wave. The sine values are scaled to the current resolution of the Timer/Counter. A sync-signal is generated on PC2 once every sine wave period.
Definition at line 232 of file tc_extensions_example.c.
References sineTable.
00233 { 00234 static uint8_t index = 0; 00235 00236 /* Write the next ouput compare A value. */ 00237 TCC0.CCABUF = sineTable[index]; 00238 00239 /* Increment table index. */ 00240 index++; 00241 00242 }
int main | ( | void | ) |
/brief Example using the Timer/Counter extension modules.
The example code is configured by commenting/uncommenting the following functions in main:
Timer/Counter C0 is set up for PWM genereation in dual slope mode. The compare value is updated once every PWM cycle to produce a sine wave output. The sine wave compare values are updated in the Timer/Counter C0 overflow interrupt service routine.
If ConfigDTI() is uncommented, the hardware dead-time insertion is enabled, with a dead-time of "DEAD_TIME_CYCLES" main system clock cycles.
If HIRES_Enable() is uncommented, the High-Resolution extension is enabled, increasing the resolution of the output by a factor of 4. The frequency of the output sine wave is also increased by a factor of 4.
If ConfigFaultProtection() is uncommented, the fault protection in the AWeX is enabled. The fault protection is triggered by a falling edge on PD0. In the case of a fault input, the fault protection module will disable output on the pins currently used by the AWeX module. In this case, the direction of the port pins are set to input. The fault protection works only for outputs that are overridden by the AWeX module. If CongigDTI() is commented, the fault protection will have no effect.
Definition at line 135 of file tc_extensions_example.c.
References ConfigClockSystem(), ConfigDTI(), ConfigFaultProtection(), DEAD_TIME_CYCLES, HIRES_Enable(), and TIMER_TOP_VALUE.
00136 { 00137 00138 ConfigClockSystem(); 00139 00140 /* Comment out the following line to disable dead-time insertion. */ 00141 ConfigDTI( DEAD_TIME_CYCLES ); 00142 00143 /* Comment out the following line to disable the HiRes. */ 00144 HIRES_Enable( &HIRESC, HIRES_HREN_TC0_gc ); 00145 00146 /* Comment out the following line to disable fault protection. */ 00147 ConfigFaultProtection(); 00148 00149 /* Enable output on PORTC. */ 00150 PORTC.DIR = 0xFF; 00151 00152 /* Configure timer. */ 00153 TCC0.PER = TIMER_TOP_VALUE; 00154 TCC0.CTRLB = TC0_CCAEN_bm | TC_WGMODE_DS_T_gc; 00155 TCC0.INTCTRLA = TC_OVFINTLVL_LO_gc; 00156 TCC0.CTRLA = TC_CLKSEL_DIV1_gc; 00157 00158 /* Enable low level interrupts. */ 00159 PMIC.CTRL = PMIC_LOLVLEN_bm; 00160 sei( ); 00161 00162 do { 00163 /* Wait while interrupt executes. */ 00164 nop(); 00165 } while (1); 00166 }
uint16_t sineTable[256] |
16-bit sine table with 256 elements.
Definition at line 71 of file tc_extensions_example.c.
Referenced by ISR().
Generated on Wed Apr 23 08:14:08 2008 for AVR1306 Using the Xmega Timer/Counter Extentions by ![]() |