关闭

GPIO口中断例程_MK20DX128VLH5_K20系列

标签: interrupt
401人阅读 评论(0) 收藏 举报
分类:
/* cpu:MK20DX128VLH5  */

#include "MK20D5.h"
#include "system_MK20D5.h"

void LEDConfig(void);
void GPIOConfig(void);
void Delay_ms(unsigned int ms);
void InterruptGpioConfig(void);

int main(void)
{   
    LEDConfig();
    InterruptGpioConfig();  
    PTA->PSOR |= (1<<2);
    while(1)
    {
        Delay_ms(10);
        PTA->PTOR |= (1<<13);   //翻转PA13    
    }
}

void LEDConfig(void)    //PA2配置为输出,在中断例程中翻转
{
    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
    PORTA->PCR[2] &= ~PORT_PCR_MUX_MASK;
    PORTA->PCR[2] |= PORT_PCR_MUX(1);
    PTA->PDDR |= (1<<2);
    PTA->PCOR |= (1<<2);
}

//================================================
//  J2_1#: PA13, 主循环中输出周期波形
//================================================
void GPIOConfig(void)
{
    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
    PORTA->PCR[13] &= ~PORT_PCR_MUX_MASK;
    PORTA->PCR[13] |= PORT_PCR_MUX(1);
    PTA->PDDR |= (1<<13);
}

//================================================
//  J2_2#: PA12,配置为下降沿中断
//================================================
void InterruptGpioConfig(void)
{
    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
    PORTA->PCR[12] &= ~PORT_PCR_MUX_MASK;
    PORTA->PCR[12] |= PORT_PCR_MUX(1);
    PORTA->PCR[12] &= ~PORT_PCR_IRQC_MASK;
    PORTA->PCR[12] |= PORT_PCR_IRQC(0x0A); //配置为下降沿中断
    PTA->PDDR &= ~(1<<12); 
    NVIC_EnableIRQ(PORTA_IRQn);
}

void Delay_ms(unsigned int ms)
{
    unsigned int Val;
    Val = SystemCoreClock/1000/4*ms;
    while(--Val != 0);      
}    

void PORTA_IRQHandler(void)
{
    /*进中断后先清楚中断标志位,不然,可能会出问题 */
    PORTA->ISFR |= (1<<12);
    //  PORTA->PCR[12] |= PORT_PCR_ISF_MASK;  //仿真调试显示这一句和上面一句效果一样
    PTA->PTOR |= (1<<2); //翻转PA2 
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32316次
    • 积分:578
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:11篇
    • 译文:0篇
    • 评论:1条
    文章分类