三个灯初始为红色,按键后触发中断变为绿色,再按键变为蓝色,在按键回到红色
#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函数名,或自己重新编写