Xmega Application Note


tc_extensions_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00050 #include "avr_compiler.h"
00051 #include "hires_driver.h"
00052 #include "awex_driver.h"
00053 
00054 
00055 /* Prototyping of functions. */
00056 void ConfigClockSystem( void );
00057 void ConfigDTI( uint8_t deadTime );
00058 void ConfigFaultProtection( void );
00059 
00060 
00064 #define TIMER_TOP_VALUE   0xFFFF
00065 
00067 #define DEAD_TIME_CYCLES    4
00068 
00069 
00071 uint16_t sineTable[256] = {
00072         32768, 33572, 34376, 35179, 35980, 36779, 37576, 38370,
00073         39161, 39948, 40730, 41508, 42280, 43047, 43807, 44561,
00074         45308, 46047, 46778, 47501, 48215, 48919, 49614, 50299,
00075         50973, 51636, 52288, 52928, 53556, 54171, 54774, 55363,
00076         55938, 56500, 57047, 57580, 58098, 58601, 59088, 59559,
00077         60014, 60452, 60874, 61279, 61667, 62037, 62390, 62725,
00078         63042, 63340, 63621, 63882, 64125, 64349, 64554, 64740,
00079         64906, 65054, 65181, 65290, 65378, 65447, 65497, 65526,
00080         65530, 65526, 65497, 65447, 65378, 65290, 65181, 65054,
00081         64906, 64740, 64554, 64349, 64125, 63882, 63621, 63340,
00082         63042, 62725, 62390, 62037, 61667, 61279, 60874, 60452,
00083         60014, 59559, 59088, 58601, 58098, 57580, 57047, 56500,
00084         55938, 55363, 54774, 54171, 53556, 52928, 52288, 51636,
00085         50973, 50299, 49614, 48919, 48215, 47501, 46778, 46047,
00086         45308, 44561, 43807, 43047, 42280, 41508, 40730, 39948,
00087         39161, 38370, 37576, 36779, 35980, 35179, 34376, 33572,
00088         32768, 31964, 31160, 30357, 29556, 28757, 27960, 27166,
00089         26375, 25588, 24806, 24028, 23256, 22489, 21729, 20975,
00090         20228, 19489, 18758, 18035, 17321, 16617, 15922, 15237,
00091         14563, 13900, 13248, 12608, 11980, 11365, 10762, 10173,
00092          9598,  9036,  8489,  7956,  7438,  6935,  6448,  5977,
00093          5522,  5084,  4662,  4257,  3869,  3499,  3146,  2811,
00094          2494,  2196,  1915,  1654,  1411,  1187,   982,   796,
00095           630,   482,   355,   246,   158,    89,    39,    10,
00096             6,    10,    39,    89,   158,   246,   355,   482,
00097           630,   796,   982,  1187,  1411,  1654,  1915,  2196,
00098          2494,  2811,  3146,  3499,  3869,  4257,  4662,  5084,
00099          5522,  5977,  6448,  6935,  7438,  7956,  8489,  9036,
00100          9598, 10173, 10762, 11365, 11980, 12608, 13248, 13900,
00101         14563, 15237, 15922, 16617, 17321, 18035, 18758, 19489,
00102         20228, 20975, 21729, 22489, 23256, 24028, 24806, 25588,
00103         26375, 27166, 27960, 28757, 29556, 30357, 31160, 31964
00104 };
00105 
00106 
00135 int main( void )
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 }
00167 
00168 
00176 void ConfigClockSystem( void )
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 }
00194 
00195 
00200 void ConfigDTI( uint8_t deadTime )
00201 {
00202         /* Configure dead time insertion. */
00203         AWEX_EnableDeadTimeInsertion( &AWEXC, AWEX_DTICCAEN_bm );
00204         AWEX_SetOutputOverrideValue( AWEXC, 0x03 );
00205         AWEX_SetDeadTimesSymmetricalUnbuffered( AWEXC, deadTime );
00206 }
00207 
00211 void ConfigFaultProtection( void )
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 }
00223 
00224 
00232 ISR(TCC0_OVF_vect)
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 }
00243 
00244 
@DOC_TITLE@
Generated on Wed Apr 23 08:14:07 2008 for AVR1306 Using the Xmega Timer/Counter Extentions by doxygen 1.5.5