c2937655 发表于 2014-11-8 03:36:35

個人使用ATXMEGA32A4開發板的 第一個程式 PLL CLOCK+TIMER

剛學 ATXMEGA 配合簡易開發板內附的程式 及 小修改 CVAVR 自動生成SystemClock代碼,
以配合使用 WinARV 編譯的測試小程式。

文件還沒看足~ 不知這CCP=CCP_IOREG_gc;是做什麼用的?

/*
+------------------------------------------------------------------------------
| Project   : LED 0.5秒閃爍 使用 PLL 8倍頻 64Mhz for miniATX_DEMO ATMEGA32A4
+------------------------------------------------------------------------------
*/
#include <avr/io.h>
#include <avr/interrupt.h>

#define LED_ON()   PORTC.OUT&=~0x80
#define LED_OFF()PORTC.OUT|=0x80
#define LED_T()    PORTC.OUT^=0x80

/*
+------------------------------------------------------------------------------
| Function    : Initial_Timer
+------------------------------------------------------------------------------
*/
void Initial_Timer(void)
{
        TCC1.PER = 31250;                                        // PER = 500ms*64M/1024
        TCC1.INTCTRLA=TC_OVFINTLVL_LO_gc;        // Enable Timer Overflow interrupt
        TCC1.CTRLA=TC_CLKSEL_DIV1024_gc;        // Select clock source. 1024Div 2M/1024= 1.95K
        PMIC.CTRL |= PMIC_LOLVLEN_bm;                 // enable low_level interrupts
}
/*
+------------------------------------------------------------------------------
| Function    : Initial_SystemsClk & PLL
+------------------------------------------------------------------------------
|PLL 倍頻電路使能步驟~
|1.使能參考時鐘源 2.設置倍頻及選PLL的參考時鐘 3.等待時鐘穩定 4.使能PLL電路
|
+-------------------------------------------------------------------------------
*/
void Int_32M_RC_SystemsClk(void)
{
        unsigned char n;
                                                                // Internal 32 kHz RC oscillator initialization
        OSC.CTRL|=OSC_RC32KEN_bm;        // Enable the internal 32 kHz RC oscillator
        while ((OSC.STATUS & OSC_RC32KRDY_bm)==0);// Wait for the internal 32 kHz RC oscillator to stabilize

                                                                // Internal 32 MHz RC oscillator initialization
        OSC.CTRL|=OSC_RC32MEN_bm;        // Enable the internal 32 MHz RC oscillator
        OSC.DFLLCTRL&= ~(OSC_RC32MCREF_bm | OSC_RC2MCREF_bm);        // Internal 32 MHz RC osc. calibration reference
                                                                                                                // clock source: 32.768 kHz Internal Osc.
        DFLLRC32M.CTRL|=DFLL_ENABLE_bm;// Enable the autocalibration of the internal 32 MHz RC oscillator
        while ((OSC.STATUS & OSC_RC32MRDY_bm)==0);// Wait for the internal 32 MHz RC oscillator to stabilize

                                                                // PLL initialization // Set the PLL clock source and multiplication factor
        n=(OSC.PLLCTRL & (~(OSC_PLLSRC_gm | OSC_PLLFAC_gm))) |        OSC_PLLSRC_RC32M_gc | 8;
        CCP=CCP_IOREG_gc;                        // PLL clock cource: 32 MHz Internal Osc./4 // PLL multiplication factor: 8
        OSC.PLLCTRL=n;
        OSC.CTRL|=OSC_PLLEN_bm;                // Enable the PLL
        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;                // System Clock prescaler A division factor:1 , B&C division factors: B:1,C:1                       
        CLK.PSCTRL=n;                                // ClkPer4: 64 MHz // ClkPer2: 64 MHz // ClkPer:64 MHz // ClkCPU:64 MHz
        while ((OSC.STATUS & OSC_PLLRDY_bm)==0);                        // Wait for the PLL to stabilize

        n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_PLL_gc;        // Select the system clock source: Phase Locked Loop
        CCP=CCP_IOREG_gc;
        CLK.CTRL=n;                                       

        OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_XOSCEN_bm);        // Disable the unused oscillators: 2 MHz,
                                                                                                //external clock/crystal oscillator

        n=CLK.LOCK | CLK_LOCK_bm;                                                // Lock the CLK.CTRL and CLK.PSCTRL registers
        CCP=CCP_IOREG_gc;
        CLK.LOCK=n;                                       
                                                                                                // Peripheral Clock output: Disabled
        PORTCFG.CLKEVOUT=(PORTCFG.CLKEVOUT & (~PORTCFG_CLKOUT_gm)) | PORTCFG_CLKOUT_OFF_gc;
}
/*
+------------------------------------------------------------------------------
| Function    : main
+------------------------------------------------------------------------------
| Description : trun on the LED
+------------------------------------------------------------------------------
*/

int main(void)
{

        PORTC.DIR|=0x80;                         //Set Led Pin as Output
        Int_32M_RC_SystemsClk();         //32M
        Initial_Timer();
        sei();
        while(1);                                        //Stop wait for interrput

        return 0;
}

/*
+------------------------------------------------------------------------------
| Function    : interrput
+------------------------------------------------------------------------------
| Description : interrput for TCC1 overflow,tig the LED1
+------------------------------------------------------------------------------
*/
ISR(TCC1_OVF_vect)//500ms
{
        LED_T();
}

雨中的风铃 发表于 2014-11-11 20:08:49

谢谢分享!!!!!

lsy5110 发表于 2014-11-11 21:08:35

顶起{:smile:}

rundream 发表于 2017-5-13 13:25:48

顶顶顶,字数补丁

rundream 发表于 2017-5-14 19:33:12

CCP=CCP_IOREG_gc;关键的寄存器都是在保险箱的,想改变就得拿

rundream 发表于 2017-5-14 19:33:55

钥匙CCP_IOREG_gc,开锁CCP

rundream 发表于 2017-5-14 19:34:34

开锁后四个时钟改变作寄存器

rundream 发表于 2017-5-14 19:34:50

过了时间就不能操作了
页: [1]
查看完整版本: 個人使用ATXMEGA32A4開發板的 第一個程式 PLL CLOCK+TIMER