STM32F103ZET6 按键 外部中断
如何去实现
STM32F103ZET6按键的外部中断代码呢?
回帖(1)
2021-12-13 11:53:18
主函数main.c
#include “sys.h”
#include “usart.h”
#include “delay.h”
#include “led.h”
#include “key.h”
#include “exti.h”
int main(void)
{
Stm32_Clock_Init(9);
uart_init(72,9600);
delay_init(72);
LED_Init();
BEEP_Init();
KEY_Init();
EXTIX_Init();
LED0=0;
while(1)
{
printf(“OKrn”);
delay_ms(1000);
}
}
中断函数exti.c
#include “exti.h”
#include “delay.h”
#include “led.h”
#include “usart.h”
#include “key.h”
#define FTIR 1 //下降沿触发
#define RTIR 2 //上升沿触发
void EXTI0_IRQHandler(void)
{
delay_ms(1);
if(KEY0==0)
{
LED1=!LED1;
printf(“exti0rn”);
}
EXTI->PR=1<<0;
}
void EXTI15_10_IRQHandler(void)
{
delay_ms(1);
if(KEY1==0)
{
LED0=!LED0;
printf(“exti1rn”);
}
EXTI->PR=1<<13;
}
void EXTIX_Init(void)
{
KEY_Init();
Ex_NVIC_Config(GPIO_E,0,FTIR);// FTIR下降沿触发
Ex_NVIC_Config(GPIO_C,13,FTIR);
MY_NVIC_Init(2,2,EXTI0_IRQn,2);//抢占2,子优先级3,组2
MY_NVIC_Init(2,1,EXTI15_10_IRQn,2);
}
exti.h中内容
#ifndef __EXTI_H
#define __EXIT_H
#include “sys.h”
void EXTIX_Init(void);
#endif
led函数
#define LED0 PDout(13)// DS0
#define LED1 PDout(14)// DS1
void LED_Init(void)
{
RCC->APB2ENR|=1<<5;
GPIOD->CRH&=0xf00fffff;
GPIOD->CRH|=0X03300000;
GPIOD->ODR|=0X00006000;
}
#include “key.h”
#include “delay.h”
#define KEY0 PEin(0) //PE0
#define KEY1 PCin(13) //PC13
#define KEY_DOWN 2
#define KEY_RIGHT 1
void KEY_Init(void)
{
RCC->APB2ENR|=1<<6;
RCC->APB2ENR|=1<<4;
GPIOE->CRL&=0XFFFFFFF0;
GPIOE->CRL|=0X00000008;
GPIOE->ODR|=1<<0;
GPIOC->CRH&=0XFF0FFFFF;
GPIOC->CRH|=0X00800000;
GPIOC->ODR|=1<<13;
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&(KEY00||KEY10))
{
delay_ms(10);
key_up=0;
if(KEY00)return 1;
else if(KEY10)return 2;
}else if(KEY01&&KEY11)key_up=1;
return 0;
}
主函数main.c
#include “sys.h”
#include “usart.h”
#include “delay.h”
#include “led.h”
#include “key.h”
#include “exti.h”
int main(void)
{
Stm32_Clock_Init(9);
uart_init(72,9600);
delay_init(72);
LED_Init();
BEEP_Init();
KEY_Init();
EXTIX_Init();
LED0=0;
while(1)
{
printf(“OKrn”);
delay_ms(1000);
}
}
中断函数exti.c
#include “exti.h”
#include “delay.h”
#include “led.h”
#include “usart.h”
#include “key.h”
#define FTIR 1 //下降沿触发
#define RTIR 2 //上升沿触发
void EXTI0_IRQHandler(void)
{
delay_ms(1);
if(KEY0==0)
{
LED1=!LED1;
printf(“exti0rn”);
}
EXTI->PR=1<<0;
}
void EXTI15_10_IRQHandler(void)
{
delay_ms(1);
if(KEY1==0)
{
LED0=!LED0;
printf(“exti1rn”);
}
EXTI->PR=1<<13;
}
void EXTIX_Init(void)
{
KEY_Init();
Ex_NVIC_Config(GPIO_E,0,FTIR);// FTIR下降沿触发
Ex_NVIC_Config(GPIO_C,13,FTIR);
MY_NVIC_Init(2,2,EXTI0_IRQn,2);//抢占2,子优先级3,组2
MY_NVIC_Init(2,1,EXTI15_10_IRQn,2);
}
exti.h中内容
#ifndef __EXTI_H
#define __EXIT_H
#include “sys.h”
void EXTIX_Init(void);
#endif
led函数
#define LED0 PDout(13)// DS0
#define LED1 PDout(14)// DS1
void LED_Init(void)
{
RCC->APB2ENR|=1<<5;
GPIOD->CRH&=0xf00fffff;
GPIOD->CRH|=0X03300000;
GPIOD->ODR|=0X00006000;
}
#include “key.h”
#include “delay.h”
#define KEY0 PEin(0) //PE0
#define KEY1 PCin(13) //PC13
#define KEY_DOWN 2
#define KEY_RIGHT 1
void KEY_Init(void)
{
RCC->APB2ENR|=1<<6;
RCC->APB2ENR|=1<<4;
GPIOE->CRL&=0XFFFFFFF0;
GPIOE->CRL|=0X00000008;
GPIOE->ODR|=1<<0;
GPIOC->CRH&=0XFF0FFFFF;
GPIOC->CRH|=0X00800000;
GPIOC->ODR|=1<<13;
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&(KEY00||KEY10))
{
delay_ms(10);
key_up=0;
if(KEY00)return 1;
else if(KEY10)return 2;
}else if(KEY01&&KEY11)key_up=1;
return 0;
}
举报
更多回帖