1
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ What means "more than 500ms" ? Please be a little bit exactlier. \$\endgroup\$
    – Mike
    Commented Nov 14, 2019 at 9:19
  • \$\begingroup\$ it generate the interrupt every 4 seconds, in atmel studio simulator, i notice that the (RC2MEN) bit in OSC.CTRL register always set to 1 even i cleared it \$\endgroup\$ Commented Nov 14, 2019 at 9:29
  • \$\begingroup\$ check oscillator frequency. \$\endgroup\$
    – Mike
    Commented Nov 14, 2019 at 9:36
  • \$\begingroup\$ the same code work fine in codevision AVR at the same board \$\endgroup\$ Commented Nov 14, 2019 at 10:17

1 Answer 1

1
\$\begingroup\$

the problem was solved the compiler was output the following warning

# warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"

so when i set the optimization level to any optimization level the oscilloscope give me the 500ms timer-interrupt

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.