初级会员

- 积分
- 151
- 金钱
- 151
- 注册时间
- 2019-4-1
- 在线时间
- 40 小时
|

楼主 |
发表于 2019-7-28 10:14:22
|
显示全部楼层
这是我修改过的,还是不行,出来的值不对
#include "lcd12864.h"
#include "stm32f10x.h"
#include "fonts.h"
#include "key.h"
#include "Systick.h"
#include <stdio.h>
#include <string.h>
extern int testCNT;
extern char dispBuff[20];
extern int test;
extern char disp[20];
uint8_t shuru=0,queding=0,tuichu=0; //功能切换
uint8_t Key_Number=0,column=0,shuchu=0,suoding=0;
uint8_t ge=0,shi=0,bai=0; //个十百
uint8_t beilv,weishu;
uint8_t ESC=0,ENTERN=0,F1=0,F2=0,F3=0,F4=0;
char number[]={0,0,0,0}; //用来存放的数据的数组
//行做输出低电平,列读取电平
void KEY_L_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APBxPeriphClockCmd (RCC_APB2Periph_GPIOx,ENABLE);
//列
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Pin=KEY_L0_GPIO_PIN;
GPIO_Init(KEY_L0_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L1_GPIO_PIN;
GPIO_Init(KEY_L1_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L2_GPIO_PIN;
GPIO_Init(KEY_L2_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L3_GPIO_PIN;
GPIO_Init(KEY_L3_PORN,&GPIO_InitStructure);
//行
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=KEY_R0_PIN;
GPIO_Init(KEY_R0_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R1_PIN;
GPIO_Init(KEY_R1_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R2_PIN;
GPIO_Init(KEY_R2_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R3_PIN;
GPIO_Init(KEY_R3_PORN,&GPIO_InitStructure);
}
//列做输出高电平,行读取电平
void KEY_R_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APBxPeriphClockCmd (RCC_APB2Periph_GPIOx,ENABLE);
//列
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=KEY_L0_GPIO_PIN;
GPIO_Init(KEY_L0_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L1_GPIO_PIN;
GPIO_Init(KEY_L1_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L2_GPIO_PIN;
GPIO_Init(KEY_L2_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_L3_GPIO_PIN;
GPIO_Init(KEY_L3_PORN,&GPIO_InitStructure);
//行
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Pin=KEY_R0_PIN;
GPIO_Init(KEY_R0_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R1_PIN;
GPIO_Init(KEY_R1_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R2_PIN;
GPIO_Init(KEY_R2_PORN,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_R3_PIN;
GPIO_Init(KEY_R3_PORN,&GPIO_InitStructure);
}
void Keyboard_Scanning (void)
{
KEY_L_Init(); //行做输出低电平,列读取电平
L_KEY_R0; //各行输出低电平
L_KEY_R1;
L_KEY_R2;
L_KEY_R3;
if(KEY_L0==0)column=1; //如果第一列读到低电平
if(KEY_L1==0)column=2; //如果第二列读到低电平
if(KEY_L2==0)column=3; //如果第三列读到低电平
if(KEY_L3==0)column=4; //如果第四列读到低电平
SysTick_Delay_ms(10);
if(column==1) //如果第一列读到低电平
{
KEY_R_Init(); //列做输出高电平,行读取电平
H_KEY_L0; //第一列输出高电平
Key_Number=0;
SysTick_Delay_us(5);
if(KEY_R0==1)Key_Number=0;
if(KEY_R1==1)Key_Number=4;
if(KEY_R2==1)Key_Number=8;
if(KEY_R3==1)Key_Number=15; //第四行 F1
SysTick_Delay_ms(10);
}
if(column==2) //如果第二列读到低电平
{
KEY_R_Init();
H_KEY_L1; //第二列输出高电平
Key_Number=0;
SysTick_Delay_us(5);
if(KEY_R0==1)Key_Number=1;
if(KEY_R1==1)Key_Number=5;
if(KEY_R2==1)Key_Number=9;
if(KEY_R3==1)Key_Number=16; //F2
SysTick_Delay_ms(10);
}
if(column==3) //如果第三列读到低电平
{
KEY_R_Init();
H_KEY_L2; //第三列输出高电平
Key_Number=0;
SysTick_Delay_us(5);
if(KEY_R0==1)Key_Number=2;
if(KEY_R1==1)Key_Number=6;
if(KEY_R2==1)Key_Number=17; //退出ESC
if(KEY_R3==1)Key_Number=18; //F3
SysTick_Delay_ms(10);
}
if(column==4) //如果第四列读到低电平
{
KEY_R_Init();
H_KEY_L3; //第四列输出高电平
Key_Number=0;
SysTick_Delay_us(5);
if(KEY_R0==1)Key_Number=3;
if(KEY_R1==1)Key_Number=7;
if(KEY_R2==1)Key_Number=19; //ENTERN 输入
if(KEY_R3==1)Key_Number=20; //F4
SysTick_Delay_ms(10);
}
column=0; //列标记清零
if(shuru==1)
{
if(Key_Number<=10)
{
test=Key_Number;
sprintf(disp,"number:%d",test); //使用c标准库把变量转化成字符串
display_string_5x8_1(2,1,disp); //dispBuff为转换后的字符串
}
}
Analog_keyboard();
}
void Analog_keyboard(void)
{
SysTick_Delay_ms(10);
if((KEY_R0==1)||(KEY_R1==1)||(KEY_R2==1)||(KEY_R3==1)) //有按键按下
{
if(shuru==1) //只有输入键按下才存放数字
{
if(Key_Number<=9) //0到9时weishu递增
{
weishu=weishu+1;
number[weishu]=Key_Number; //number[1]位开始存数据
SysTick_Delay_ms(10);
}
if(weishu>=4) //0123 只是用123位来存数据
{
weishu=0; //清零
memset(number,0,sizeof (number)); //数组清零函数
}
}
if(Key_Number==19) //输入按键按下开始输入
{
memset(number,0,sizeof (number)); //数组清零函数
shuru=1;
tuichu=0;
queding=0;
ENTERN=1;
clear_screen();
display_string_5x8_1(1,1,"Please_enter");
}
if(Key_Number==15) //确认按键按下
{
shuru=0;
tuichu=0;
queding=1;
if(weishu==1)
{
ge=number[1]; //输入一个数字
shuchu=(ge+shi+bai);
}
if(weishu==2) //输入两个数字
{
ge=number[2];
shi=number[1]*10;
shuchu=(ge+shi+bai);
}
if(weishu==3) //输入三个数字
{
ge=number[3]; //输入的第三位
shi=number[2]*10; //输入的第二位
bai=number[1]*100; //输入的第一位
shuchu=(ge+shi+bai);
}
if(queding==1)
{
testCNT=shuchu;
sprintf(dispBuff,"number1:%d",testCNT); //使用c标准库把变量转化成字符串
display_string_5x8_1(3,1,dispBuff); //dispBuff为转换后的字符串
}
}
if(Key_Number==17) //按下退出键
{
shuru=0;
tuichu=1;
queding=0;
ENTERN=0;
memset(number,0,sizeof (number)); //数组清零函数
clear_screen();
if(tuichu==1)
{
display_string_5x8_1(1,1, "ESC" ); //dispBuff为转换后的字符串
}
}
}
}
|
|