You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.1 KiB
70 lines
2.1 KiB
#include "Dip_Switch.h" |
|
|
|
static void PrintBinary(uint8_t value); |
|
static uint8_t Last_Dip_Switch_Address = 0; |
|
/** |
|
* 初始化拨码开关的GPIO配置 |
|
* |
|
* 本函数配置GPIOE的五个多路开关输入引脚,用于读取拨码开关的状态 |
|
* 由于拨码开关GPIO引脚电平状态不确定,因此需要配置GPIO为浮空输入模式 |
|
*/ |
|
void Dip_Switch_Init(void) |
|
{ |
|
GPIO_InitType GPIO_InitStructure; |
|
|
|
// 使能GPIOE时钟 |
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE); |
|
|
|
// 配置GPIOE的引脚9、10、11、12、13 |
|
GPIO_InitStructure.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13; |
|
// 设置GPIO模式为浮空输入模式 |
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
|
// 设置GPIO速度为50MHz |
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
|
|
|
// 初始化GPIOE配置 |
|
GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure); |
|
} |
|
|
|
/** |
|
* 读取拨码开关的状态 |
|
* |
|
* 本函数读取GPIOE的引脚9、10、11、12、13的状态,并返回一个5位的二进制数, |
|
* 0表示开关关闭,1表示开关打开 |
|
*/ |
|
void Dip_Switch_Read(void) |
|
{ |
|
uint8_t Dip_Switch_Address; |
|
Dip_Switch_Address = GPIO_ReadInputDataBit(GPIOE, GPIO_PIN_9) << 4 |
|
| GPIO_ReadInputDataBit(GPIOE, GPIO_PIN_10) << 3 |
|
| GPIO_ReadInputDataBit(GPIOE, GPIO_PIN_11) << 2 |
|
| GPIO_ReadInputDataBit(GPIOE, GPIO_PIN_12) << 1 |
|
| GPIO_ReadInputDataBit(GPIOE, GPIO_PIN_13); |
|
// SEGGER_RTT_printf(0, "Dip Switch Address: %d\r\n", Dip_Switch_Address); |
|
// PrintBinary(Dip_Switch_Address); |
|
if (Dip_Switch_Address != Last_Dip_Switch_Address) |
|
{ |
|
PrintBinary(Dip_Switch_Address); |
|
Last_Dip_Switch_Address = Dip_Switch_Address; |
|
} |
|
|
|
} |
|
|
|
static void PrintBinary(uint8_t value) |
|
{ |
|
char binary[9]; |
|
int index = 7; |
|
|
|
while (value > 0) { |
|
binary[index--] = (value & 1) ? '1' : '0'; |
|
value >>= 1; |
|
} |
|
|
|
// 填充剩余的0 |
|
while (index >= 0) { |
|
binary[index--] = '0'; |
|
} |
|
|
|
binary[8] = '\0'; // 终止符 |
|
SEGGER_RTT_printf(0, "Dip Switch Address: %s\r\n", binary); |
|
}
|
|
|