此内容不属于实验内容,因自己手头有一STM32F103,故验证性的进行代码实验,按照老师课堂ppt进行了一下复现。
通过按钮控制LED灯的亮灭(状态取反)。
main.c代码:
cpp
#include "STM32F10X.h"
#include "stdio.h"
void NVIC_Conf(void){
NVIC_InitTypeDef Nvic;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
Nvic.NVIC_IRQChannel = EXTI9_5_IRQn;
Nvic.NVIC_IRQChannelPreemptionPriority = 0;
Nvic.NVIC_IRQChannelSubPriority = 0;
Nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&Nvic);
}
void LED_Conf(void){ //pa8
GPIO_InitTypeDef led;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
led.GPIO_Pin = GPIO_Pin_8;
led.GPIO_Speed = GPIO_Speed_50MHz;
led.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &led);
}
void KEY_Conf(void){ //pc5 key0
GPIO_InitTypeDef key;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
key.GPIO_Pin = GPIO_Pin_5;
key.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &key);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
}
void EXTI_conf(void){
EXTI_InitTypeDef exti;
exti.EXTI_Line = EXTI_Line5;
exti.EXTI_Mode = EXTI_Mode_Interrupt;
exti.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&exti);
}
int main(void){
NVIC_Conf();
LED_Conf();
KEY_Conf();
EXTI_conf();
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
while(1){
}
}
it.c部分代码:
cpp
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) != RESET)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_8,(BitAction)((1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8))));
EXTI_ClearITPendingBit(EXTI_Line5);
}
}