标题:
MCU 6x6的矩阵键盘,按键按下串口接收数据感觉多运行了一次,会显示多一个加6之后的数据
[打印本页]
作者:
maixueqiu
时间:
2022-5-16 16:29
标题:
MCU 6x6的矩阵键盘,按键按下串口接收数据感觉多运行了一次,会显示多一个加6之后的数据
d7dfd79b43eabd24ff641419ec11e72.png
(23.88 KB, 下载次数: 30)
下载附件
2022-5-16 16:29 上传
单片机源程序如下:
#include "XMCE003.h"
#include "Function_define.h"
//***************** The Following is in define in Fucntion_define.h ***************************
//****** Always include Function_define.h call the define you want, detail see main(void) *******
//***********************************************************************************************
/*------------------------------------------------
The main C function. Program execution starts
here after stack initialization.
------------------------------------------------*/
//P06 P07 串口 P02 P16 烧码 P20 复位 P30 晶振
//下面端口定义根据实际情况修改
#define P_IN_0 P00 //输入0号
#define P_IN_1 P10
#define P_IN_2 P11
#define P_IN_3 P12
#define P_IN_4 P13
#define P_IN_5 P14
#define P_OUT_0 P05 //输出0号
#define P_OUT_1 P20
#define P_OUT_2 P30
#define P_OUT_3 P17
#define P_OUT_4 P03
#define P_OUT_5 P01
#define BR_16M_115200 (0x8a)
#define SCAN_TIMES (10) //1个判决周期内扫描次数
#define PUSH_TIMES (5) //1个判决周期内多少次按下算真正按下
static uint8_t gUartBuf[5]; //串口数据缓冲
static uint8_t gScanCnt; //扫描计数
static uint8_t gKeyPressCnt[36]; //按键数
static uint8_t gKeyState[36]; //按键状态
void delay() //延时函数
{
uint16_t temp_u16 = 4000;
while(temp_u16--){nop;}
}
void IO_Init() //IO初始化
{
uint8_t i;
P00_PullUp_Mode;
P06_PullUp_Mode; //上拉
P10_PullUp_Mode;
P11_PullUp_Mode;
P12_PullUp_Mode;
P13_PullUp_Mode;
P14_PullUp_Mode;
P05_OpenDrain_Mode; //开漏
P20_OpenDrain_Mode;
P30_OpenDrain_Mode;
P17_OpenDrain_Mode;
P03_OpenDrain_Mode;
P01_OpenDrain_Mode;
P_OUT_0 = 1;
P_OUT_1 = 1;
P_OUT_2 = 1;
P_OUT_3 = 1;
P_OUT_4 = 1;
P_OUT_5 = 1;
for(i = 0; i < 36; i++)
{
gKeyPressCnt[i] = 0; //按键计数清零
gKeyState[i] = 0; //按键状态清零
}
gScanCnt = 0; //扫描计数清零
}
void Send_Data_To_UART(unsigned char r_data)
{
TI = 0;
SBUF = r_data;
while(TI==0);
}
void Send_Key_State(uint8_t Key, uint8_t UpDown)
{
uint8_t i;
uint8_t Check =0; //00000000
gUartBuf[0] = 0xaa; //10101010 10101010
gUartBuf[1] = 0x55; //01010101 11111111
gUartBuf[2] = Key; //00000000
gUartBuf[3] = UpDown; //00000000/00000001
for (i = 0; i < 4; i++)
{
Check ^= gUartBuf[i];
}
gUartBuf[4] = Check;
for (i = 0; i < 5; i++)
{
Send_Data_To_UART(gUartBuf[i]);
}
}
void KeyBoard_ReadInput(uint8_t Line)
{
Line *= 6; //Line=Line*6
if (P_IN_0 == 0)
{
gKeyPressCnt[Line]++;
}
if (P_IN_1 == 0)
{
gKeyPressCnt[Line + 1]++;
}
if (P_IN_2 == 0)
{
gKeyPressCnt[Line + 2]++;
}
if (P_IN_3 == 0)
{
gKeyPressCnt[Line + 3]++;
}
if (P_IN_4 == 0)
{
gKeyPressCnt[Line + 4]++;
}
if (P_IN_5 == 0)
{
gKeyPressCnt[Line + 5]++;
}
}
void UART_Init() //串口初始化
{
P06 = 1;
P07 = 1;
TI = 0; //标志位清零
RI = 0;
B8EN = 0; //关闭串口第九位
SOVRH = 0x00; //波特率高两位
SOVRL = BR_16M_115200; //波特率低八位
REN = 1; //串口使能位
}
void KeyBoard_ScanOnce(void)
{
uint8_t i;
P_OUT_0 = 0;
KeyBoard_ReadInput(0);
P_OUT_0 = 1;
P_OUT_1 = 0;
KeyBoard_ReadInput(1);
P_OUT_1 = 1;
P_OUT_2 = 0;
KeyBoard_ReadInput(2);
P_OUT_2 = 1;
P_OUT_3 = 0;
KeyBoard_ReadInput(3);
P_OUT_3 = 1;
P_OUT_4 = 0;
KeyBoard_ReadInput(4);
P_OUT_4 = 1;
P_OUT_5 = 0;
KeyBoard_ReadInput(5);
P_OUT_5 = 1;
gScanCnt++;
if (gScanCnt >= SCAN_TIMES) //扫描次数大于上线
{
gScanCnt = 0; //扫描次数清零
for(i = 0; i < 36; i++)
{
if (gKeyPressCnt[i] >= PUSH_TIMES)//判断按键按下
{
if (!gKeyState[i]) // 如果按键按下
{
gKeyState[i] = 1; //串口发送按键按下
Send_Key_State(i,1);//发送按键状态1
}
}
else
{
if (gKeyState[i]) //按键松开
{
gKeyState[i] = 0; //串口发送按键抬起
Send_Key_State(i,0);//发送按键状态0
}
}
}
for(i = 0; i < 36; i++)
{
gKeyPressCnt[i] = 0; //按键计数清零
}
}
else
{
delay();
}
}
void main (void)
{
Set_All_GPIO_PushPull_Mode; // Define in Function_define.h
UART_Init(); //初始化
IO_Init(); //初始化
WDTEN;
while(1)
{
WDTCLR;
KeyBoard_ScanOnce();
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1