苏嵌暑期实训----day19

姓名:祝俊宝                                 日期:2018.07.27

 

今日学习任务

1、复习昨天内容

2、实现4个按键分别控制4个彩灯亮灭

今日任务完成情况 均完成
今日开发问题汇总

今日未解决问题

今日开发收获 通过自主学习,消化了前两天的学习内容,对stm32单片机的理解加深了
自我评价 良好
部分代码
#include "bsp_key.h"


/**********按键与中断线对应关系******
                 |                  |
key1  = PA0      |    LED1 = PC1    |
key2  = PC13     |    LED2 = PC2    |
key3  = PA2      |    LED3 = PC3    |
key4  = PB5      |    LED4 = PC4    |
								 |                  |
************************************/





void Key_Exit_Config(void)
{	
//结构体变量声明
	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
/********************************************
	使能I/O口时钟,选择GPIOA,GPIOB,GPIOC三个口
	开启SYSCFG时钟
********************************************/
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOB | 
												 RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	
	
	
/*****************************
初始化I/O口为输入浮空模式,
根据指定的参数初始化GPIOX外设
*****************************/
	
	//浮空模式
	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_13);         //PC13
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
	//根据指定的参数初始化GPIOX外设
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	
	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5);         //PB5
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0 | GPIO_Pin_2);  //PA0和PA2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	

/***************************
配置GPIO口与中断线的映射关系
相当于把GPIOx口与中断线连起来
***************************/
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource13);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource5);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource2);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
	

/**************************
初始化中断线
配置触发条件,模式等参数
**************************/

	EXTI_InitStructure.EXTI_Line = (EXTI_Line13 | EXTI_Line5 | 
																	EXTI_Line0 | EXTI_Line2);   //选择需要用到的中断线
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; /* 上升沿触发 */ 
	EXTI_InitStructure.EXTI_LineCmd = ENABLE; 
	EXTI_Init(&EXTI_InitStructure);


/*************************
配置中断分组,并使能中断
配置中断通道,中断优先级等参数
**************************/

	//PC13
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;    //使能按键外部中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0E; //抢占优先级0x0e
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0E;        //相应优先级0x0e
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;							 //使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化
	
	//PB5
	NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;    //IRQ指的是中断请求
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	
	//PA0
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;    //IRQ指的是中断请求
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	
	//PA2
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;    //IRQ指的是中断请求
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0E;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	
}


/***********中断服务函数*********************
不同中断线对应不同中断函数,可根据需要写多个
中断函数
********************************************/


void EXTI0_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_Line0) != RESET)
	{ 
		//PA0,KEY1按下
		GPIO_ToggleBits(GPIOC,GPIO_Pin_1);
		EXTI_ClearITPendingBit(EXTI_Line0);
		printf("PA0 中断\r\n");
	}
}

void EXTI15_10_IRQHandler(void) 
{ 
		/* PC13, K2按下*/ 
	if (EXTI_GetITStatus(EXTI_Line13) != RESET)
	{ 
			GPIO_ToggleBits(GPIOC,GPIO_Pin_2);
			EXTI_ClearITPendingBit(EXTI_Line13);
			printf("PC13 中断\r\n"); 
	}
}

void EXTI2_IRQHandler(void)
{  
	//PA2,K3按下
	if (EXTI_GetITStatus(EXTI_Line2) != RESET)
	{ 
		GPIO_ToggleBits(GPIOC,GPIO_Pin_3);
		EXTI_ClearITPendingBit(EXTI_Line2);
		printf("PA2 中断\r\n");
	}
}

void EXTI9_5_IRQHandler(void)
{
	//PB5,K4按下
	if (EXTI_GetITStatus(EXTI_Line5) != RESET)
	{ 
		GPIO_ToggleBits(GPIOC,GPIO_Pin_4);
		EXTI_ClearITPendingBit(EXTI_Line5);
		printf("PB5中断\r\n");
	}
}

 

   

 

你可能感兴趣的:(苏嵌暑期实训----day19)