原创 stm32 4*4矩阵键盘中断

2011-7-6 19:02 3459 6 6 分类: MCU/ 嵌入式

#include<stm32f10x_lib.h>

#define KEYIO GPIOD
#define KEYIOEN 3
#define GPIO_D 3
#define FTRI 1
#define RTRI 2

void LED_Config(){
 RCC->APB2ENR|=1<<(2+4);
 GPIOE->CRL&=0x00000000;
 GPIOE->CRL|=0x00000003;
 GPIOE->ODR|=0x00000001;
}

void GPIO_Config(){
  RCC->APB2ENR|=1<<(2+KEYIOEN);
 KEYIO->CRL&=0x00000000;
 KEYIO->CRL|=0x33338888;
 KEYIO->ODR|=0x000000F0;
}

void NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM){
 u8 EXTIADDR=BITx/4;
 u8 EXTIOFFSET=(BITx%4)*4;
 RCC->APB2ENR|=0x00000001;
 AFIO->EXTICR[EXTIADDR]&=~(0x000F<<EXTIOFFSET);
 AFIO->EXTICR[EXTIADDR]|=GPIOx<<EXTIOFFSET;
 EXTI->IMR|=1<<BITx;
 if(TRIM&0x01)
  EXTI->FTSR|=1<<BITx;
 if(TRIM&0x02)
  EXTI->RTSR|=1<<BITx;
}

void PriorityGroup_Config(u8 NVIC_Group){
 SCB->AIRCR&=0x0000F8FF;
 SCB->AIRCR|=0x05FA0000;
 SCB->AIRCR|=((~NVIC_Group)&0x07)<<8;
}

void Priority_Config(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group){
 NVIC->ISER[NVIC_Channel/32]|=1<<(NVIC_Channel%32);
 NVIC->IPR[NVIC_Channel/4]=(((NVIC_PreemptionPriority<<(4-NVIC_Group))|(NVIC_SubPriority&(0x0F>>NVIC_Group)))&0x0000000F)<<((NVIC_Channel%4)*8+4);
}

void EXTI_Config(){
 NVIC_Config(GPIO_D,0,RTRI);
 NVIC_Config(GPIO_D,1,RTRI);
 NVIC_Config(GPIO_D,2,RTRI);
 NVIC_Config(GPIO_D,3,RTRI);
 PriorityGroup_Config(2);
 Priority_Config(2,2,EXTI0_IRQChannel,2);
 Priority_Config(2,2,EXTI1_IRQChannel,2);
 Priority_Config(2,2,EXTI2_IRQChannel,2);
 Priority_Config(2,2,EXTI3_IRQChannel,2);
}

void EXTI0_IRQHandler(){
 GPIOE->ODR&=0x00000000;
 EXTI->PR=1<<0;
}    

void EXTI1_IRQHandler(){
 GPIOE->ODR&=0x00000000;
 EXTI->PR=1<<1;

void EXTI2_IRQHandler(){
 GPIOE->ODR&=0x00000000;
 EXTI->PR=1<<2;
}

void EXTI3_IRQHandler(){
 GPIOE->ODR&=0x00000000;
 EXTI->PR=1<<3;
}

int main(){
 LED_Config();
 GPIO_Config();
 EXTI_Config();
 while(1){
 }
}

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /2 下一条