E3利用中断在按键后灯变换颜色(β版实操)task3

三个灯初始为红色,按键后触发中断变为绿色,再按键变为蓝色,在按键回到红色

#include "derivative.h" /* include peripheral declarations */
#define BIT(x) (1 << x)
unsigned char bMode=0;
void led4_init(void)
{
	SIM_SCGC5 |= BIT(10)+BIT(11);  //enable portc clk gating
	
	PORTC_PCR3 |= BIT(8); //config GPIO
	PORTC_PCR4 |= BIT(8); //config GPIO
	PORTC_PCR5 |= BIT(8); //config GPIO
	PORTB_PCR16|=BIT(8);//config GPIO
	PORTB_PCR17|=BIT(8);//config GPIO
	PORTB_PCR18|=BIT(8);//config GPIO
	PORTB_PCR19|=BIT(8);//config GPIO
	PORTC_PCR0|=BIT(8);//config GPIO
	PORTC_PCR2|=BIT(8);//config GPIO
	GPIOB_PDDR =0x0f0000;
	GPIOC_PDDR =0x003d;//配置PTC0、PTC4为输出 
	
	GPIOB_PDOR=0x090000;
	GPIOC_PDOR=0x0008;
	//GPIOC_PDDR |= (BIT(3)|BIT(4)|BIT(5)); //direction as output	
	//GPIOC_PDOR |= BIT(4); //green
}
void s2_init(void)
{
	SIM_SCGC5 |= BIT(11);  //enable portc clk gating
	
	PORTC_PCR7 |= (BIT(8)|BIT(1)|BIT(0)); //config GPIO
	PORTC_PCR7 |= (0xa << 16); //falling edge
	
	GPIOC_PDDR &= ~BIT(7); //direction as input
}


void PORTCD_IRQHandler(void)
{
	if(bMode==0)
	{
	GPIOB_PDOR=0x020000;
	GPIOC_PDOR=0x0011;
	bMode=1;
	}
	else if(bMode==1)
	{
	GPIOB_PDOR=0x040000;
	GPIOC_PDOR=0x0024;
	bMode=2;
	}
	else{
		GPIOB_PDOR=0x090000;
		GPIOC_PDOR=0x0008;
		bMode=0;
	}

	 PORTC_PCR7 |= BIT(24);
}
void __enable_irq(void)
{
	asm("CPSIE i");
}
 
void __enable_portc_irq(void)
{
	NVIC_ISER |= BIT(31);
}
int main(void)
{	__enable_irq();
	__enable_portc_irq();
	led4_init();
	s2_init();
	
	
	
	while(1);
	
	return 0;
}

在这里插入图片描述

1 Enable the interrupt

void __enable_irq(void)
{
	asm("CPSIE i");
}
 
void __enable_portc_irq(void)
{
	NVIC_ISER |= BIT(31);
}

首先打开全局中断
在KL26的手册中差NVIC的中断向量分配表table3-7
在这里插入图片描述
知道了中断向量为31

2 configure the corresponding interrupt source

void s2_init(void)
{
	SIM_SCGC5 |= BIT(11);  //enable portc clk gating
	
	PORTC_PCR7 |= (BIT(8)|BIT(1)|BIT(0)); //config GPIO
	PORTC_PCR7 |= (0xa << 16); //falling edge
	
	GPIOC_PDDR &= ~BIT(7); //direction as input
}

1首先打开portc的时钟
2看11章PORT
在这里插入图片描述
bit(8) GPIO
在这里插入图片描述
IRQC选择下降沿选10即是A,向左移16位
下降沿
3和4已经了解编写中断函数,对应kinetis_sysinit.c函数名,或自己重新编写

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值