i have atxmega256A3u with external 16Mhz crystal, i set the system clock to use that oscillator source as
uint8_t n,s;
OSC.XOSCCTRL = OSC_FRQRANGE_12TO16_gc /* 12 - 16 MHz */
| 0 << OSC_X32KLPM_bp /* 32.768 kHz XTAL OSC Low-power Mode: disabled */
| 0 << OSC_XOSCPWR_bp /* Crystal Oscillator Drive: disabled */
| OSC_XOSCSEL_XTAL_16KCLK_gc; /* 0.4-16 MHz XTAL - 16K CLK */
OSC.CTRL|=OSC_XOSCEN_bm;
n=(CLK.PSCTRL & (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm))) |
CLK_PSADIV_1_gc | CLK_PSBCDIV_1_1_gc;
CCP=CCP_IOREG_gc;
CLK.PSCTRL=n;
// Wait for the Oscillators to be stable
while (!(OSC.STATUS & OSC_XOSCRDY_bm));
n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_XOSC_gc;
CCP=CCP_IOREG_gc;
CLK.CTRL=n;
OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_RC32MEN_bm | OSC_RC32KEN_bm | OSC_PLLEN_bm);
PORTCFG.CLKEVOUT=(PORTCFG.CLKEVOUT & (~PORTCFG_CLKOUT_gm)) | PORTCFG_CLKOUT_OFF_gc;
SREG=s;
and i am trying to generate timer0 interrupt every 500ms as
uint8_t s;
s = SREG;
cli();
Timer_0_Disable(&TCC0);
//TCC0.CTRLA=(TCC0.CTRLA & (~TC0_CLKSEL_gm)) | TC_CLKSEL_DIV1_gc;
TCC0.CTRLA=(TCC0.CTRLA & (~TC0_CLKSEL_gm)) | TC_CLKSEL_DIV256_gc;
//TCC0.CTRLA = TC_CLKSEL_DIV1_gc;
TCC0.CTRLB=(0<<TC0_CCDEN_bp) | (0<<TC0_CCCEN_bp) | (0<<TC0_CCBEN_bp) | (0<<TC0_CCAEN_bp) | TC_WGMODE_NORMAL_gc;
TCF0.CTRLD=TC_EVACT_OFF_gc | TC_EVSEL_OFF_gc;
TCC0.CTRLE=TC_BYTEM_NORMAL_gc;
TCC0.INTCTRLA=TC_ERRINTLVL_OFF_gc | TC_OVFINTLVL_LO_gc;
TCC0.INTCTRLB = TC_CCDINTLVL_OFF_gc | TC_CCCINTLVL_OFF_gc | TC_CCBINTLVL_OFF_gc | TC_CCAINTLVL_OFF_gc;
HIRESF.CTRLA &= ~HIRES_HREN0_bm;
// Clear the interrupt flags
TCC0.INTFLAGS=TCF0.INTFLAGS;
// Set Counter register
TCC0.CNT=0x0000;
// Set Period register
TCC0.PER=0x7A11;//500 ms
// Set channel A Compare/Capture register
TCC0.CCA=0x0000;
// Set channel B Compare/Capture register
TCC0.CCB=0x0000;
// Set channel C Compare/Capture register
TCC0.CCC=0x0000;
// Set channel D Compare/Capture register
TCC0.CCD=0x0000;
SREG=s;
this code is work fine using codevisionAVR but in atmel studio7 the timer take more than 500ms to generate the interrupt have any suggestion please!