PIT設置_中斷優先級設置__MK20DX128VLH5_K20系列

PIT初始化

/* cpu:MK20DX128VLH5  */

#include "MK20D5.h"

/* 中斷優先級分組宏 */
#define NVIC_PriorityGroup_0         ((uint32_t)0x7) /*!< 0 bits for pre-emption priority   4 bits for subpriority */                                               
#define NVIC_PriorityGroup_1         ((uint32_t)0x6) /*!< 1 bits for pre-emption priority   3 bits for subpriority */                                                  
#define NVIC_PriorityGroup_2         ((uint32_t)0x5) /*!< 2 bits for pre-emption priority   2 bits for subpriority */                                                   
#define NVIC_PriorityGroup_3         ((uint32_t)0x4) /*!< 3 bits for pre-emption priority   1 bits for subpriority */                                                   
#define NVIC_PriorityGroup_4         ((uint32_t)0x3) /*!< 4 bits for pre-emption priority   0 bits for subpriority */

void GPIOInit(void);
void PITInit(void);

int main(void)
{
    //時鐘初始化在“systm_MK20D5.c”中配置爲48M
    GPIOInit();
    PITInit();
    while(1)
    {
    }
}

void GPIOInit(void)
{
    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTC_MASK;  //開端口時鐘門控

    /* PA.2、PC.3、PC.4配置爲通用IO  */
    PORTA->PCR[2] &= ~PORT_PCR_MUX_MASK;   
    PORTA->PCR[2] |= PORT_PCR_MUX(1);       
    PORTC->PCR[3] &= ~PORT_PCR_MUX_MASK;
    PORTC->PCR[3] |= PORT_PCR_MUX(1);
    PORTC->PCR[4] &= ~PORT_PCR_MUX_MASK;
    PORTC->PCR[4] |= PORT_PCR_MUX(1);

    /* PA.2上拉,PC.3下拉­  */
    PORTA->PCR[2] |= PORT_PCR_PE_MASK;
    PORTA->PCR[2] |= PORT_PCR_PS_MASK;
    PORTC->PCR[3] |= PORT_PCR_PE_MASK;
    PORTC->PCR[3] &= ~PORT_PCR_PE_MASK;    

    /* 端口方向配置:1-輸出,0-輸入 */
    PTA->PDDR |= 1 << 2;            //PA.2
    PTC->PDDR |= 1 << 3;            //PC.3
    PTC->PDDR &= ~(1<<4);          //PC.4

    /* 端口初始值設定 */
    PTA->PCOR |= 1 << 2;            //PA.2 = 0
    PTC->PSOR |= 1 << 3;            //PC.3 = 1

    /* 中斷配置:PC.3下降沿中斷 */
    PORTC->PCR[3] &= ~PORT_PCR_IRQC_MASK;
    PORTC->PCR[3] |= PORT_PCR_IRQC(0x0A);   //

    /* 中斷優先級配置 */
    NVIC_SetPriority(PORTC_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_1, 0, 1));
    /* 使能中斷 */
    NVIC_EnableIRQ(PORTC_IRQn);
}

/* PIT0配置 */
void PITInit(void)
{
    SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;   //PIT模塊時鐘
    PIT->MCR &= ~PIT_MCR_MDIS_MASK;        //PIT時鐘使能,該位比較特殊,對應位爲“0”時使能
    PIT->CHANNEL[0].LDVAL = 48000000-1;  //週期設置爲1s
    PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TIE_MASK;    //使能中斷

    /* 中斷優先級配置 */
    NVIC_SetPriority(PIT0_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_1, 0, 2));
    /* 使能中斷 */
    NVIC_EnableIRQ(PIT0_IRQn);

    PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN_MASK;    //使能定時器,定時器開始運行
}

/* PIT0中斷響應 */
void PIT0_IRQHandler(void)
{   
    /* 清中斷標誌 */
    PIT->CHANNEL[0].TFLG |= PIT_TFLG_TIF_MASK;
    /* PA.2翻轉 */
    PTA->PTOR |= 1 << 2;
}

/* PORTC中斷響應 */
void PORTC_IRQHandler(void)
{
    /* 清中斷標誌 */
    PORTC->ISFR |= (1<<3);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章