stm32f4中断按键中断范例

stm32f4中断按键中断范例_第1张图片
stm32f4中断按键中断范例_第2张图片
stm32f4中断按键中断范例_第3张图片
stm32f4中断按键中断范例_第4张图片
exti.h

#ifndef __EXTI_H
#define __EXTI_H	 
#include "sys.h"  	
#include "oled.h"
#include "led.h"

#include "keySigle.h"
#include "delay.h" 
	
extern float kp,ki,kd;
extern float TargetDegree,NowDegree,ve;
extern float TargetPulse,NowPulse; 
extern float step;
extern u8 PageFlag;
extern u8 PageFlagChange;
void EXTIX_Init(void);	//外部中断初始化	
void oled_showPage1();
void oled_showPage2();
void oled_showPage3();
#endif








exti.c

#include "exti.h"
#include "show.h"
static u8 Page1Key=0;
static u8 Page2Key=0;
static u8 Page3Key=0;
extern  u8 PageFlag;
	static u8 keyc1=0;
	static u8 keyc2=0;
	static u8 keyc3=0;

//	 
//#define KEY0 		PCin(6)   	//PE4---led4
//#define KEY1 		PDin(7)		//PE3 -----3
//#define KEY2 		PEin(5)		//P32-----2
//#define KEY3 	  PEin(0)		//PA0----1
//#define KEY4 	  PBin(6)		//PA0----0
							  
		//012345678901234
		//#P:xx.xx#I:x.xx
		//#D:XX.XX
		//#To:XXX.XXX
		//#Now:XXX.XXX
		//#v:XXX.XXX

//外部中断0服务程序key3
void EXTI0_IRQHandler(void)
{

	delay_ms(20);	//消抖
	if(EXTI_GetFlagStatus(EXTI_Line0)!=RESET)
	{ 
		LED1=!LED1; //蜂鸣器翻转 	
		OLED_Clear();
		delay_us(30);
		switch(PageFlag)
		{
			case 0:oled_showPage1();break;
			case 1:oled_showPage2();break;
			case 2:	oled_showPage2();break;
			
		}
	
	
	//第二页
	//页面2--128/8=16
//0123456789012345
//#VP:XXX.XXX Vmod
//#VI:XXX.XXX #Run
//#VD:xxx.xxx #Stp
//#ToV:XXX.XXX 
//NowV:XXX.XXX
	 
	 EXTI_ClearITPendingBit(EXTI_Line0); //清除LINE0上的中断标志位 
 }
	
}

//外部中断2服务程序
void EXTI9_5_IRQHandler(void)
{
	static u8 pagecount=0;
	delay_ms(20);	//消抖
	
	if(EXTI_GetFlagStatus(EXTI_Line5)!=RESET)//加的按键  
	{
		 LED2=!LED2; //蜂鸣器翻?
		 switch(Page2Key)
		 {
			 case 0:kp=kp+0.01;break;
		   case 1:ki=ki+0.01;break;
			 case 2:kd=kd+0.01;break;
			 case 3:TargetDegree+=0.01;break;
		   default:break; 
		 }
		 EXTI_ClearITPendingBit(EXTI_Line5);
	}
	else if(EXTI_GetFlagStatus(EXTI_Line6)!=RESET)//减的按键
	{
		 if(KEY0==0)
				LED4=!LED4; //蜂鸣器翻转 
		 else if(KEY4==0)
				LED0=!LED0;
		 switch(Page3Key)
		 {
			 case 0:kp=kp-0.01;break;
		   case 1:ki=ki-0.01;break;
			 case 2:kd=kd-0.01;break;
			 case 3:TargetDegree-=0.01;break;
			 case 4:;break;
		   default:break; 
		 }
		 EXTI_ClearITPendingBit(EXTI_Line6);
	}	
 else if(EXTI_GetFlagStatus(EXTI_Line7)!=RESET)//确定的按键 K1
	{
		 LED3=!LED3; //蜂鸣器翻转 
		// PageFlagChange=1;
		 OLED_Clear();
		delay_ms(10);
		 PageFlag++;
		 if(PageFlag==3)PageFlag=0;
		 EXTI_ClearITPendingBit(EXTI_Line7);
	}	
	
}


//外部中断初始化程序//#define KEY0 		PCin(6)   	//PE4
//#define KEY1 		PDin(7)		//PE3 
//#define KEY2 		PEin(5)		//P32
//#define KEY3 	  PEin(0)		//PA0
//#define KEY4 	  PBin(6)		//PA0
//初始化PE2~4,PA0为中断输入.//C6 D7 E5 E0 B6
void EXTIX_Init(void)
{
	NVIC_InitTypeDef   NVIC_InitStructure;
	EXTI_InitTypeDef   EXTI_InitStructure;
	
	KEY_Init(); //按键对应的IO口初始化
 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
	
//Pb6 连接到中断线2
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource6);//Pc6 连接到中断线3
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource7);//PD7 连接到中断线4
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource0);//PE0 连接到中断线0
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource5);//PE5 连接到中断线5
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource6);//这个会把C6的中断线6屏蔽掉,所以实际只有4个按键起作用
//  /* 配置EXTI_Line0 */
//  EXTI_InitStructure.EXTI_Line = EXTI_Line0;//LINE0
//  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
//  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //上升沿触发 
//  EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE0
//  EXTI_Init(&EXTI_InitStructure);//配置
	
	/* 配置EXTI_Line2,3,4 */
	EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line6 |EXTI_Line7 | EXTI_Line5 ;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断线使能
  EXTI_Init(&EXTI_InitStructure);//配置
 
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//外部中断0
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
  NVIC_Init(&NVIC_InitStructure);//配置
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断1
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
  NVIC_Init(&NVIC_InitStructure);//配置
	
//	NVIC_InitStructure.NVIC_IRQChannel = EXTI7_IRQn;//外部中断2
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级30x03
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
//  NVIC_Init(&NVIC_InitStructure);//配置
//	
//	
//	NVIC_InitStructure.NVIC_IRQChannel = EXTI5_IRQn;//外部中断3
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级20x02
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
//  NVIC_Init(&NVIC_InitStructure);//配置
	
	
//	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中断4
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级1
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
//  NVIC_Init(&NVIC_InitStructure);//配置
//	   
}


void oled_showPage1()
{
	OLED_Clear();
		switch(keyc1)
		{
			case 0:
			{
//页面0--128/8=16        
//0123456789012345
//Running Mode
//#1:Position Mode
//#2:Velocity Mode
//#Yes  #No 
//     By LMF V1.0				
				
//				 	delay_ms(100);
					OLED_ShowChar(0,1,'#');
				
				 //OLED_ShowChar(0,3,'#',12,1) ;
				 //oled_show(0);
				break;
			}
			case 1:
			{ 
//				delay_ms(100);
				 OLED_ShowChar(0,2,'$');
				// oled_show(0);
				
				break;
			}
			case 2:
			{ 
//				delay_ms(100);
				OLED_ShowChar(0,3,'*');	
				
				// oled_show(0);
				break;
			}
		//	default:break;
		}
    delay_ms(2);
		keyc1++;
		if(keyc1==3)keyc1=0;	
	 
}


void oled_showPage2()
{
	switch(keyc2)
		{
			case 0:
			{
//页面1--128/8=16        
//0123456789012345
//#KP:XXX.XXX Pmod
//#KI:XXX.XXX #Run
//#KD:xxx.xxx #Stp
//#To:XXX.XXX 
//Now:XXX.XXX 
//				 	delay_ms(100);
				 OLED_ShowChar(0,0,'#') ;
				 //OLED_ShowChar(0,3,'#',12,1) ;
				 oled_show(1);
				break;
			}
			case 1:
			{

				// OLED_Clear();	
				delay_ms(100);
				 OLED_ShowChar(0,1,'#') ;
				 oled_show(1);
				
				break;
			}
			case 2:
			{

			//	 OLED_Clear();	
//				delay_ms(100);
				 OLED_ShowChar(0,2,'#') ;
				 oled_show(1);
				break;
			}
			case 3:
			{
       //  OLED_Clear();	
				delay_ms(100);
				 OLED_ShowChar(0,3,'#') ;
				 oled_show(1);
				break;
			}
			case 4:
			{
     //   OLED_Clear();
//					delay_ms(100);
				 OLED_ShowChar(1,12,'#') ;
				 oled_show(1);
				break;
			}
				case 5:
			{
        // OLED_Clear();
//					delay_ms(100);
				 OLED_ShowChar(2,12,'#') ;
				 oled_show(1);
				break;
			}
			default:break;
		}

		keyc2++;
		if(keyc2==6)keyc2=0;	
    Page2Key=keyc2;			 
}

/
void oled_showPage3()
{
	switch(keyc3)
		{
			case 0:
			{
//页面1--128/8=16        
//0123456789012345
//#KP:XXX.XXX Pmod
//#KI:XXX.XXX #Run
//#KD:xxx.xxx #Stp
//#To:XXX.XXX 
//Now:XXX.XXX 
				// OLED_Clear();
				delay_ms(100);
				 OLED_ShowChar(0,0,'#') ;
				 //OLED_ShowChar(0,3,'#',12,1) ;
				 oled_show(2);
				break;
			}
			case 1:
			{

			//	 OLED_Clear();
					delay_ms(100);
				 OLED_ShowChar(0,1,'#') ;
				 oled_show(2);
				
				break;
			}
			case 2:
			{

			//	 OLED_Clear();
					delay_ms(100);
				 OLED_ShowChar(0,2,'#') ;
				 oled_show(2);
				break;
			}
			case 3:
			{
      //   OLED_Clear();
					delay_ms(100);
				 OLED_ShowChar(0,3,'#') ;
				 oled_show(2);
				break;
			}
			case 4:
			{
       // OLED_Clear();
				 OLED_ShowChar(1,12,'#') ;
				 oled_show(2);
				break;
			}
				case 5:
			{
      //   OLED_Clear();
					delay_ms(100);
				 OLED_ShowChar(2,12,'#') ;
				 oled_show(2);
				break;
			}
			default:break;
		}

		keyc3++;
		if(keyc3==6)keyc3=0;	
    Page3Key=keyc3;	

}









你可能感兴趣的:(笔记,单片机)