|
uint8_t func_index = 0;
uint16_t voltage,resistor;
void (*current_operation_index)();
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能PB端口时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); /*使能SWD 禁用JTAG*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure2;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure2);
}
uint8_t Key_GetNum(void)
{
uint8_t keynum = 0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 0);
Delay_ms(10);
keynum = 1;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == 0);
Delay_ms(10);
keynum = 2;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10) == 0);
Delay_ms(10);
keynum = 3;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == 0);
Delay_ms(10);
keynum = 4;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == 0);
Delay_ms(10);
keynum = 5;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) == 0);
Delay_ms(10);
keynum = 6;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0);
Delay_ms(10);
keynum = 7;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0)
{
Delay_ms(10);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0);
Delay_ms(10);
keynum = 8;
}
return keynum;
}
typedef struct
{
uint8_t current; //当前状态索引号
uint8_t up; //向上
uint8_t down; //向下
uint8_t left; //向左
uint8_t right; //向右
uint8_t enter; //确定
uint8_t one; //存储数据区1
uint8_t two; //存储数据区2
uint8_t three; //存储数据区3
void (*current_operation)(void); //当前状态应该执行的操作
} Menu_table;
Menu_table table[10]=
{
{0,0,0,3,1,0,4,5,6,(*fun0)},//一级界面 时钟界面
{1,1,1,0,2,1,1,1,1,(*fun1)},//二级界面第一行 CAN Mode
{2,2,2,1,3,2,2,2,2,(*fun2)},//二级界面第二行 485 Mode
{3,3,3,2,0,3,3,3,3,(*fun3)},//二级界面第三行 UART Mode
{4,4,4,4,0,7,4,4,4,(*fun4)},//二级界面第四行 BACK
{5,5,5,5,0,8,5,5,5,(*fun5)},
{6,6,6,6,0,9,6,6,6,(*fun6)},//三级界面第一行 back
{7,0,0,3,1,0,4,5,6,(*fun7)},
{8,0,0,3,1,0,4,5,6,(*fun8)},
{9,0,0,3,1,0,4,5,6,(*fun9)},
};
void Menu_key_set(void)
{
if(Key_GetNum()== 1)
{
func_index=table[func_index].enter;//按键enter按下后的索引号
OLED_Clear();
}
if(Key_GetNum()== 2)
{
func_index=table[func_index].left;//按键left按下后的索引号
OLED_Clear();
}
if(Key_GetNum()== 3)
{
func_index=table[func_index].right;//按键right按下后的索引号
OLED_Clear();
}
if(Key_GetNum()== 4)
{
func_index=table[func_index].down;//按键down按下后的索引号
OLED_Clear();
} if(Key_GetNum()== 5)
{
func_index=table[func_index].up;//按键up按下后的索引号
OLED_Clear();
}
if(Key_GetNum()== 6)
{
func_index=table[func_index].one;//按键one按下后的索引号
OLED_Clear();
} if(Key_GetNum()== 7)
{
func_index=table[func_index].two;//按键two按下后的索引号
OLED_Clear();
}
if(Key_GetNum()== 8)
{
func_index=table[func_index].three;//按键three按下后的索引号
OLED_Clear();
}
current_operation_index=table[func_index].current_operation;//执行当前索引号所对应的功能函数。
(*current_operation_index)();//执行当前操作函数
}
|
|