开 发 板:FRDM MKL02Z4
模 块:GPIO模块
//例程:通过按键中断,同时控制LED,按键按下LED状态翻转
int main(void)
{
SIM_SCGC5 |=(1UL << 10); //使能PORTB模块的时钟
PORTB_PCR11 |=(1UL << 8) //PORTB的Pin11配置为GPIO模式
GPIOB_PDDR |= (1UL << 1 //GPIOB的Pin11配置为输出模式
PORTB_PCR10 |=(1UL << 8); //PORTB的Pin10配置为GPIO模式
GPIOB_PDDR |= (1UL << 10 //GPIOB的Pin10配置为输出模式
SIM_SCGC5 |=(1UL << 9); //使能PORTA模块的时钟
PORTA_PCR12 |=0x2u; //PORTA的Pin12上下拉使能
PORTA_PCR12 |=0x1u; //PORTA的Pin12配置为上拉
PORTA_PCR12 |=(1UL << 8); //PORTA的Pin12配置为GPIO模式
GPIOA_PDDR &= ~(1UL << 1 //GPIOA的Pin12配置为输入2);
PORTA_PCR12 &= ~(0xF0000u);
PORTA_PCR12 |=0xA0000u; //PORTA的中断触发方式为下降沿
NVIC->ISER[0] |= (1<<30); //使能PORTA的中断
//NVIC->ISER[0] = (1 << ((uint32_t)(30) & 0x1F));
while(1)
{
}
}
void PORTA_IRQHandler(void) //中断操作函数
{
if(PORTA_ISFR == (1<<12)) //检测中断,中断标志是否为1
{
PORTA_PCR12 |= 0x1000000u; //清除中断使能
if( (GPIOA_PDIR & (1 <<12))==0 ) //检测GPIOA的输入数据寄存器的12位是否为1
{
GPIOB_PTOR |= (1 << 11);//GPIOB的输出数据寄存器的位11翻转
GPIOB_PTOR |= (1 << 10);//GPIOB的输出数据寄存器的位10翻转
}
}
}
只因选择,所以热爱
只因有痛,所以珍惜
只因很笨,所以学习 -----不点记