stm32f103c8t6最小系统板和使用按键进行模式切换,加减数值
时间: 2023-08-02 11:07:05 浏览: 48
对于STM32F103C8T6最小系统板,你可以使用按键来进行模式切换和加减数值的操作。以下是一个简单的示例代码,可以帮助你实现这些功能:
```c
#include "stm32f10x.h"
#define BUTTON_PIN GPIO_Pin_0
#define BUTTON_GPIO GPIOA
// 定义模式枚举
typedef enum {
MODE_1,
MODE_2
} Mode;
Mode currentMode = MODE_1;
int value = 0;
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置按键引脚
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUTTON_GPIO, &GPIO_InitStructure);
}
void EXTI_Configuration(void) {
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 配置外部中断线路
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
// 配置中断线路
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
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;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
// 延时去抖动
for (int i = 0; i < 10000; i++);
if (currentMode == MODE_1) {
value++;
} else if (currentMode == MODE_2) {
value--;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
int main(void) {
// 配置GPIO和外部中断
GPIO_Configuration();
EXTI_Configuration();
while (1) {
// 在这里可以根据value的值执行相应的操作
}
}
```
在这个示例中,我们使用PA0引脚作为按键输入,通过外部中断触发按键事件。当按键被按下时,会根据当前的模式来进行数值的加减操作。你可以根据需要修改代码来实现你的具体功能。记得根据你的硬件连接情况,适当修改引脚和中断配置。希望能帮到你!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)