搜索
bottom↓
回复: 7

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

[复制链接]

出0入0汤圆

发表于 2014-11-8 03:36:35 | 显示全部楼层 |阅读模式
剛學 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();
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

发表于 2014-11-11 20:08:49 | 显示全部楼层
谢谢分享!!!!!

出0入0汤圆

发表于 2014-11-11 21:08:35 | 显示全部楼层
顶起

出0入0汤圆

发表于 2017-5-13 13:25:48 来自手机 | 显示全部楼层
顶顶顶,字数补丁

出0入0汤圆

发表于 2017-5-14 19:33:12 来自手机 | 显示全部楼层
CCP=CCP_IOREG_gc;关键的寄存器都是在保险箱的,想改变就得拿

出0入0汤圆

发表于 2017-5-14 19:33:55 来自手机 | 显示全部楼层
钥匙CCP_IOREG_gc,开锁CCP

出0入0汤圆

发表于 2017-5-14 19:34:34 来自手机 | 显示全部楼层
开锁后四个时钟改变作寄存器

出0入0汤圆

发表于 2017-5-14 19:34:50 来自手机 | 显示全部楼层
过了时间就不能操作了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-24 12:28

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表