1 #include "stm32f10x.h" 2 #include "stm32f10x_gpio.h" 3 4 //内核,(NVIC) 5 #include "misc.h" 6 7 //点亮红色灯 PB5 8 //step1:使能 9 Rcc_APB2PeriPhClockCmd( 10 Rcc_APB2PeriPh_GPIOB,ENABLE); 11 //step2:定义GPIO初始化结构体变量 12 GPIO_InitTypeDef a; 13 a.GPIO_Speed = GPIO_Speed_50MHz; 14 a.GPIO_Pin = GPIO_Pin_5; 15 //推挽输出 16 a.GPIO_Mode = GPIO_Mode_OUT_PP; 17 //step3:调用GPIO初始化函数 18 GPIO_Init(GPIOB,&a); 19 //step4:设置PB5为低电平 20 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 21 22 //读取引脚的电平 23 int i = GPIO_ReadDataInputBit( 24 GPIOB,GPIO_Pin_5); 25 26 /*按键初始化*/ 27 Rcc_APB2PeriPhClockCmd( 28 Rcc_APB2PeriPh_GPIOA,ENABLE); 29 //step2:定义GPIO初始化结构体变量 30 GPIO_InitTypeDef a; 31 a.GPIO_Speed = GPIO_Speed_50MHz; 32 a.GPIO_Pin = GPIO_Pin_0; 33 //浮空输入 34 a.GPIO_Mode = GPIO_Mode_IN_FLOATING; 35 //step3:调用GPIO初始化函数 36 GPIO_Init(GPIOA,&a); 37 38 39 /*循环读取按键 PA0*/ 40 while(1){ 41 int i = GPIO_ReadDataInputBit( 42 GPIOB,GPIO_Pin_5); 43 if(i == 0){ 44 //按键按下,点亮LED 45 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 46 }else{ 47 //松开,熄灭灯 48 GPIO_SetBits(GPIOB,GPIO_Pin_5); 49 } 50 } 51 while(1){ 52 if(GPIO_ReadDataInputBit( 53 GPIOA,GPIO_Pin_0) == 0){ 54 //按键按下进入这里执行 55 if(GPIO_ReadDataInputBit( 56 GPIOB,GPIO_Pin_5) == 0){ 57 //每按一次,灯的状态反转 58 GPIO_SetBits(GPIOB,GPIO_Pin_5); 59 }else{ 60 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 61 } 62 } 63 } 64 65 /*NVIC*/ 66 //step1:优先级分组 67 NVIC_PriorityGroupConfig( 68 NVIC_PriorityGroup_1; 69 ); 70 //step2:NVIC初始化结构体 71 NVIC_InitTypeDef b; 72 //中断源 73 b.NVIC_IRQChannel = EXTI0_IRQn; 74 //抢占优先级 75 b.NVIC_IRQChannelPreemptionPriority = 1; 76 //子优先级 77 b.NVIC_IRQChannelSubPriority = 1; 78 //使能 79 b.NVIC_IRQChannelCmd = ENABLE; 80 //step3:NVIC初始化函数 81 NVIC_Init(&b); 82 83 //中断处理函数 84 void 中断名_IRQHandler(){ 85 } 86 void EXTI0_IRQHandler(){ 87 88 }
1 #include "stm32f10x.h" 2 #include "stm32f10x_gpio.h" 3 #include "stm32f10x_exti.h" 4 5 //内核,(NVIC) 6 #include "misc.h" 7 8 //点亮红色灯 PB5 9 //step1:使能 10 Rcc_APB2PeriPhClockCmd( 11 Rcc_APB2PeriPh_GPIOB,ENABLE); 12 //step2:定义GPIO初始化结构体变量 13 GPIO_InitTypeDef a; 14 a.GPIO_Speed = GPIO_Speed_50MHz; 15 a.GPIO_Pin = GPIO_Pin_5; 16 //推挽输出 17 a.GPIO_Mode = GPIO_Mode_OUT_PP; 18 //step3:调用GPIO初始化函数 19 GPIO_Init(GPIOB,&a); 20 //step4:设置PB5为低电平 21 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 22 23 //读取引脚的电平 24 int i = GPIO_ReadDataInputBit( 25 GPIOB,GPIO_Pin_5); 26 27 /*按键初始化*/ 28 Rcc_APB2PeriPhClockCmd( 29 Rcc_APB2PeriPh_GPIOA,ENABLE); 30 //step2:定义GPIO初始化结构体变量 31 GPIO_InitTypeDef a; 32 a.GPIO_Speed = GPIO_Speed_50MHz; 33 a.GPIO_Pin = GPIO_Pin_0; 34 //浮空输入 35 a.GPIO_Mode = GPIO_Mode_IN_FLOATING; 36 //step3:调用GPIO初始化函数 37 GPIO_Init(GPIOA,&a); 38 39 40 /*循环读取按键 PA0*/ 41 while(1){ 42 int i = GPIO_ReadDataInputBit( 43 GPIOB,GPIO_Pin_5); 44 if(i == 0){ 45 //按键按下,点亮LED 46 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 47 }else{ 48 //松开,熄灭灯 49 GPIO_SetBits(GPIOB,GPIO_Pin_5); 50 } 51 } 52 while(1){ 53 if(GPIO_ReadDataInputBit( 54 GPIOA,GPIO_Pin_0) == 0){ 55 //按键按下进入这里执行 56 if(GPIO_ReadDataInputBit( 57 GPIOB,GPIO_Pin_5) == 0){ 58 //每按一次,灯的状态反转 59 GPIO_SetBits(GPIOB,GPIO_Pin_5); 60 }else{ 61 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 62 } 63 } 64 } 65 66 /*NVIC*/ 67 //step1:优先级分组 68 NVIC_PriorityGroupConfig( 69 NVIC_PriorityGroup_1; 70 ); 71 //step2:NVIC初始化结构体 72 NVIC_InitTypeDef b; 73 //中断源 74 b.NVIC_IRQChannel = EXTI0_IRQn; 75 //抢占优先级 76 b.NVIC_IRQChannelPreemptionPriority = 1; 77 //子优先级 78 b.NVIC_IRQChannelSubPriority = 1; 79 //使能 80 b.NVIC_IRQChannelCmd = ENABLE; 81 //step3:NVIC初始化函数 82 NVIC_Init(&b); 83 84 //中断处理函数 85 void 中断名_IRQHandler(){ 86 } 87 88 void EXTI0_IRQHandler(){ 89 90 } 91 92 /*外部中断配置*/ 93 //外部中断结构体 94 EXTI_InitTypeDef c; 95 c.EXTI_Line = EXTI_Line0; 96 c.EXTI_Mode = EXTI_Mode_Interrupt; 97 c.EXTI_Trigger = EXTI_Trigger_Rising; 98 c.EXTI_LineCmd = ENABLE; 99 //外部中断初始化函数 100 EXTI_Init(&c); 101 102 //配置外部中断的触发引脚 103 GPIO_EXTILineConfig( 104 GPIOPortSourceGPIOC,GPIOPinSourcePin0 105 ); 106 Rcc_APB2PeriPhClockCmd( 107 Rcc_APB2PeriPh_GPIOC,ENABLE); 108 GPIO_InitTypeDef a; 109 a.GPIO_Speed = GPIO_Speed_50MHz; 110 a.GPIO_Pin = GPIO_Pin_0; 111 //浮空输入 112 a.GPIO_Mode = GPIO_Mode_IN_FLOATING; 113 GPIO_Init(GPIOC,&a); 114 115 void EXTI0_IRQHandler(){ 116 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 117 118 手动把中断标志位清零 119 EXTI_ClearITPendingBit(EXTI_Line0); 120 }