---
# System prepended metadata

title: F2812的初始化範例
tags: [F2812]

---

[TOC]


# 初始化講解
- 一開始的 #ifdef 區段是給run flash時使用的
- 後續筆記會更了解這些初始化的用途
- 進入while前的初始化大致分成三部分
    - InitSysCtrl
        - 設置時鐘相關暫存器
    - InitPie
        - 關閉中斷，避免後續初始化時因為中斷發生使得初始化不穩定、失敗
        - 初始化中斷向量表
        - <font color="red">在進入while前要把中斷總開關打開</font>
    - 其他Initialization(使用者)
        - 這邊的初始化是指把各暫存器設定成想要的功能、模式
        - 程式庫裡面有各功能初始化的範例，但幾乎都是寫入0
    - <font color="red">基本上照這樣的初始化步驟，只需要關注User Initializing Step即可，前面都不用動</font>

# 範例程式
- 修改自Generator生成的main.c
- 此檔案與專案可見FTP
- 

``` c++=
void main(void){

#ifdef Run_FLASH // EXAMPLE_FLASH, if defined, is in CCS project options
    //--------Copying Flash to RAM , must use "F2812_KST_Flash.cmd"----------
    memcpy(&UserRamFuncsRunStart, &UserRamFuncsLoadStart, (Uint32)&UserRamFuncsLoadSize);
#endif
    
    //----------------------------------------------------------------
    //----------------------------------------------------------------
    // Initialize System Control:
    //----------------------------------------------------------------
    //----------------------------------------------------------------

    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the DSP281x_SysCtrl.c file.
    //----------------------------------------------------------------
    InitSysCtrl();

    //----------------------------------------------------------------
    //----------------------------------------------------------------
    // Clear all interrupts and initialize PIE vector table
    // This step is needed to avoid the interrupt during the 
    // following initializing step.
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------

    // Disable CPU interrupts
    //---------------------------------------------------------------------------
    DINT;

    // Disable CPU interrupts and clear all CPU interrupt flags:
    //---------------------------------------------------------------------------
    IER = 0x0000;
    IFR = 0x0000;

    // Initialize PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    //---------------------------------------------------------------------------
    // This function is found in the DSP281x_PieCtrl.c file.
    InitPieCtrl();

    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example.  This is useful for debug purposes.
    // The shell ISR routines are found in DSP281x_DefaultIsr.c.
	//---------------------------------------------------------------------------
    // This function is found in DSP281x_PieVect.c.
    InitPieVectTable();


    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    // User Initializing Step
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------

    // Default Initializing Function in Library
    //---------------------------------------------------------------------------
    // InitPeripherals(); //this include InitEv, InitAdc..., found in DSP281x_InitPeripherals.c
    // InitGpio(); // found in DSP281x_Gpio.c

    // System Clock Initializing
    //---------------------------------------------------------------------------
    EALLOW; //This is needed to write to EALLOW protected registers
        SysCtrlRegs.HISPCP.all = HSPCLK_PSC_10;    // HSPCLK = SYSCLKOUT/HSPCLK_PSC_10
    EDIS;

    // Interrupts that are used in this example are re-mapped to
    // ISR functions found within this file. 
    //---------------------------------------------------------------------------
    EALLOW;	//This is needed to write to EALLOW protected registers
        PieVectTable.T1PINT = &eva_timer1_isr;	// Timer1 ISR
    EDIS;	//This is needed to disable write to EALLOW protected registers

    // PIE Register Initializing
    //---------------------------------------------------------------------------
    PieCtrlRegs.PIEIER2.all = M_INT4;// Enable PIE group 2 interrupt 4 for T1PINT

    // Event Manager Initializing
    //---------------------------------------------------------------------------
    // init_eva_timer1();

    // ADC Initializing
    //---------------------------------------------------------------------------
    //  AdcRegs.ADCTRL1.bit.ACQ_PS = ADC_SHCLK;
    //  AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS;
    //  AdcRegs.ADCTRL1.bit.SEQ_CASC = 1;        // 1  Cascaded mode
    //  AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x1;
    //  AdcRegs.ADCTRL1.bit.CONT_RUN = 1;       // Setup continuous run

    // Initialize GPIO
    //---------------------------------------------------------------------------
    // GPIO_select();

    // CPU Interrupt Enable
    //---------------------------------------------------------------------------
    // Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
    // and INT5 for T4PINT:
    //	IER |= (M_INT2 | M_INT3 | M_INT4 | M_INT5);
    IER |= M_INT2;


    // Variable Initalizing
    count = 0;

    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    // Enable global Interrupts and higher priority real-time debug events:
    // Final step of all the interrupt
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    EINT;   // Enable Global interrupt INTM
    ERTM;   // Enable Global realtime interrupt DBGM



    // Infinite Loop
    //---------------------------------------------------------------------------
    while(1){

    }

}
```

