stm32矩阵键盘扫描数据通过USB发送

                                                                                                             Keyboard.c

#include "keyboard.h"
#include "my_usb.h"
#include "gpio.h"
#include "stdio.h"
#include "sys.h"
#include "LedControl.h"

int ScanCodeToMakeCode(int ScanCode); //通码
int ScanCodeToBreakCode(int ScanCode); //断码
void KeyboardLineScan(int select);
int GetLineScanCode(int line); //»ñÈ¡ÐÐɨÃèÂë
int LineMultiKeyDetec(int ScanCode);
void KeySendToUsb(void);
void Delayms(int ms);

KEYBOARD_T KeyboardDrive = {
.ScanCodeToMakeCode = ScanCodeToMakeCode,
.ScanCodeToBreakCode = ScanCodeToBreakCode,
.KeyboardLineScan = KeyboardLineScan,
.GetLineScanCode = GetLineScanCode,
.LineMultiKeyDetec = LineMultiKeyDetec,
.KeySendToUsb = KeySendToUsb
};

 

int ScanCodeToMakeCode(int ScanCode)
{
switch(ScanCode)
{
case 0xbf0001: return 0x0029; //`~
case 0x7f0001: return 0x0002; //1!
case 0x7f0002: return 0x0003; //2@
case 0x7f0004: return 0x0004; //3#
case 0x7f0008: return 0x0005; //4$
case 0xbf0008: return 0x0006; //5%
case 0xbf0010: return 0x0007; //6^
case 0x7f0010: return 0x0008; //7&
case 0x7f0020: return 0x0009; //8*
case 0x7f0040: return 0x000A; //9(
case 0x7f0400: return 0x000B; //0)
case 0xbf0400: return 0x000C; //-_
case 0xbf0020: return 0x000D; //=+
case 0xfd0200: return 0x000E; //Backspace
case 0xfd0001: return 0x000F; //Tab
case 0xfe0001: return 0x0010; //qQ
case 0xfe0002: return 0x0011; //wW
case 0xfe0004: return 0x0012; //eE
case 0xfe0008: return 0x0013; //rR
case 0xfd0008: return 0x0014; //tT
case 0xfd0010: return 0x0015; //yY
case 0xfe0010: return 0x0016; //uU
case 0xfe0020: return 0x0017; //iI
case 0xfe0040: return 0x0018; //oO
case 0xfe0400: return 0x0019; //pP
case 0xfd0400: return 0x001A; //[{
case 0xfd0020: return 0x001B; //]}
case 0xfb0100: return 0x002B; //\|
case 0xfd0002: return 0x003A; //Caps lock
case 0xfb0001: return 0x001E; //aA
case 0xfb0002: return 0x001F; //sS
case 0xfb0004: return 0x0020; //dD
case 0xfb0008: return 0x0021; //fF
case 0xf70008: return 0x0022; //gG
case 0xf70010: return 0x0023; //hH
case 0xfb0010: return 0x0024; //jJ
case 0xfb0020: return 0x0025; //kK
case 0xfb0040: return 0x0026; //lL
case 0xfb0400: return 0x0027; //;:
case 0xf70400: return 0x0028; //'"
case 0xef0200: return 0x001C; //Return/Enter
case 0xfd0100: return 0x002A; //Left Shift
case 0xef0001: return 0x002C; //zZ
case 0xef0002: return 0x002D; //xX
case 0xef0004: return 0x002E; //cC
case 0xef0008: return 0x002F; //vV
case 0xdf0008: return 0x0030; //bB
case 0xdf0010: return 0x0031; //nN
case 0xef0010: return 0x0032; //mM
case 0xef0020: return 0x0033; //,<
case 0xef0040: return 0x0034; //.>
case 0xdf0400: return 0x0035; ///?
case 0xef0800: return 0x0036; //Rgiht Shift
case 0xbf1000: return 0x001D; //Left Control
case 0xf70800: return 0x0038; //Left Alt
case 0xf70100: return 0x0039; //Space
case 0xbf0800: return 0xE038; //Right Alt
case 0xef1000: return 0xE01D; //Right Control
case 0xbf0100: return 0xE037; //Print Screen
case 0xdf0080: return 0xE04B; //Left Arrow
case 0xf70080: return 0xE048; //Up Arrow
case 0xdf0100: return 0xE050; //Down Arrow
case 0xef0100: return 0xE04D; //Right Arrow
case 0x7f0100: return 0xE052; //Insert
case 0xf70001: return 0x0001; //Escape
case 0xbf0002: return 0x003B; //F1
case 0xbf0004: return 0x003C; //F2
case 0xfd0004: return 0x003D; //F3
// case 0xfd0004: return 0x003B; //F3

case 0xf70004: return 0x003E; //F4
case 0x7f1000: return 0x003F; //F5
case 0xf70020: return 0x0040; //F6
case 0xfb0200: return 0xE053; //Delete
case 0xfe2000: return 0xE047; //CW
case 0xf72000: return 0xE049; //PW
case 0xef2000: return 0xE04F; //CFM
case 0xdf2000: return 0xE051; //THI
case 0xbf2000: return 0xE035; //Patient
case 0x7f2000: return 0x0037; //M
case 0xfe4000: return 0x004A; //ScanArea
case 0xfd4000: return 0x004E; //BodyMark
case 0xfb4000: return 0xE01C; //Calc
case 0xf74000: return 0x0053; //Clear
case 0xef4000: return 0x0052; //Cursor
case 0xdf4000: return 0x004F; //R
case 0xbf4000: return 0x0050; //L
case 0x7f4000: return 0x0051; //B
case 0xfe8000: return 0x004B; //Measure
case 0xfd8000: return 0x004C; //Set
case 0xfb8000: return 0x004D; //Save
case 0xf78000: return 0x0047; //P1
case 0xef8000: return 0x0048; //Freeze
case 0xdf8000: return 0x0049; //P2
case 0xbf8000: return 0xE019; //Probe
case 0x7f8000: return 0xE010; //P3
case 0xfd0080: return 0xE06C; //Auto
case 0xdf0800: return 0xE021; //Depth
case 0x7f0080: return 0xE06B; //STE
case 0xef0080: return 0xE024; //Reserved

case 0xfd2000: return 0xE022; //EC1
case 0xfb2000: return 0xE020; //EC2
case 0xfd1000: return 0xE030; //EC3
case 0xfb1000: return 0xE02E; //EC4
case 0xf71000: return 0xE06D; //EC5
case 0xdf1000: return 0xE065; //Up
case 0xdf0001: return 0xE032; //Down
case 0xdf0002: return 0xE06A; //Left
case 0xdf0004: return 0xE069; //Right
case 0xfe1000: return 0x0045;
case 0xfd0040: return 0x0041;
case 0xbf0040: return 0x0042;
case 0xbf0200: return 0x0043;
case 0x7f0200: return 0x0044;
case 0xf70200: return 0x0057;
case 0xdf0200: return 0x0058;
case 0x7f0800: return 0x0046;
case 0xfd0800: return 0xE05B;
case 0xfb0800: return 0xE05C;
case 0xdf0040: return 0xE05D;
default: return 0x0000;
}
}

int ScanCodeToBreakCode(int ScanCode)
{
switch(ScanCode)
{
case 0xbf0001: return 0x00A9; //`~
case 0x7f0001: return 0x0082; //1!
case 0x7f0002: return 0x0083; //2@
case 0x7f0004: return 0x0084; //3#
case 0x7f0008: return 0x0085; //4$
case 0xbf0008: return 0x0086; //5%
case 0xbf0010: return 0x0087; //6^
case 0x7f0010: return 0x0088; //7&
case 0x7f0020: return 0x0089; //8*
case 0x7f0040: return 0x008A; //9(
case 0x7f0400: return 0x008B; //0)
case 0xbf0400: return 0x008C; //-_
case 0xbf0020: return 0x008D; //=+
case 0xfd0200: return 0x008E; //Backspace
case 0xfd0001: return 0x008F; //Tab
case 0xfe0001: return 0x0090; //qQ
case 0xfe0002: return 0x0091; //wW
case 0xfe0004: return 0x0092; //eE
case 0xfe0008: return 0x0093; //rR
case 0xfd0008: return 0x0094; //tT
case 0xfd0010: return 0x0095; //yY
case 0xfe0010: return 0x0096; //uU
case 0xfe0020: return 0x0097; //iI
case 0xfe0040: return 0x0098; //oO
case 0xfe0400: return 0x0099; //pP
case 0xfd0400: return 0x009A; //[{
case 0xfd0020: return 0x009B; //]}
case 0xfb0100: return 0x00AB; //\|
case 0xfd0002: return 0x00BA; //Caps lock
case 0xfb0001: return 0x009E; //aA
case 0xfb0002: return 0x009F; //sS
case 0xfb0004: return 0x00A0; //dD
case 0xfb0008: return 0x00A1; //fF
case 0xf70008: return 0x00A2; //gG
case 0xf70010: return 0x00A3; //hH
case 0xfb0010: return 0x00A4; //jJ
case 0xfb0020: return 0x00A5; //kK
case 0xfb0040: return 0x00A6; //lL
case 0xfb0400: return 0x00A7; //;:
case 0xf70400: return 0x00A8; //'"
case 0xef0200: return 0x009C; //Return/Enter
case 0xfd0100: return 0x00AA; //Left Shift
case 0xef0001: return 0x00AC; //zZ
case 0xef0002: return 0x00AD; //xX
case 0xef0004: return 0x00AE; //cC
case 0xef0008: return 0x00AF; //vV
case 0xdf0008: return 0x00B0; //bB
case 0xdf0010: return 0x00B1; //nN
case 0xef0010: return 0x00B2; //mM
case 0xef0020: return 0x00B3; //,<
case 0xef0040: return 0x00B4; //.>
case 0xdf0400: return 0x00B5; ///?
case 0xef0800: return 0x00B6; //Rgiht Shift
case 0xbf1000: return 0x009D; //Left Control
case 0xf70800: return 0x00B8; //Left Alt
case 0xf70100: return 0x00B9; //Space
case 0xbf0800: return 0xE0B8; //Right Alt
case 0xef1000: return 0xE09D; //Right Control
case 0xbf0100: return 0xE0B7; //Print Screen
case 0xdf0080: return 0xE0CB; //Left Arrow
case 0xf70080: return 0xE0C8; //Up Arrow
case 0xdf0100: return 0xE0D0; //Down Arrow
case 0xef0100: return 0xE0CD; //Right Arrow
case 0x7f0100: return 0xE0D2; //Insert
case 0xf70001: return 0x0081; //Escape
case 0xbf0002: return 0x00BB; //F1
case 0xbf0004: return 0x00BC; //F2
// case 0xbf0004: return 0x00BB; //F2

case 0xfd0004: return 0x00BD; //F3
case 0xf70004: return 0x00BE; //F4
case 0x7f1000: return 0x00BF; //F5
case 0xf70020: return 0x00C0; //F6
case 0xfb0200: return 0xE0D3; //Delete
case 0xfe2000: return 0xE0C7; //CW
case 0xf72000: return 0xE0C9; //PW
case 0xef2000: return 0xE0CF; //CFM
case 0xdf2000: return 0xE0D1; //THI
case 0xbf2000: return 0xE0B5; //Patient
case 0x7f2000: return 0x00B7; //M
case 0xfe4000: return 0x00CA; //ScanArea
case 0xfd4000: return 0x00CE; //BodyMark
case 0xfb4000: return 0xE09C; //Calc
case 0xf74000: return 0x00D3; //Clear
case 0xef4000: return 0x00D2; //Cursor
case 0xdf4000: return 0x00CF; //R
case 0xbf4000: return 0x00D0; //L
case 0x7f4000: return 0x00D1; //B
case 0xfe8000: return 0x00CB; //Measure
case 0xfd8000: return 0x00CC; //Set
case 0xfb8000: return 0x00CD; //Save
case 0xf78000: return 0x00C7; //P1
case 0xef8000: return 0x00C8; //Freeze
case 0xdf8000: return 0x00C9; //P2
case 0xbf8000: return 0xE099; //Probe
case 0x7f8000: return 0xE090; //P3
case 0xfd0080: return 0xE0EC; //Auto
case 0xdf0800: return 0xE0A1; //Depth
case 0x7f0080: return 0xE0EB; //STE
case 0xef0080: return 0xE0A4; //Reserved

case 0xfd2000: return 0xE0A2; //EC1
case 0xfb2000: return 0xE0A0; //EC2
case 0xfd1000: return 0xE0B0; //EC3
case 0xfb1000: return 0xE0AE; //EC4
case 0xf71000: return 0xE0ED; //EC5
case 0xdf1000: return 0xE0E5; //Up
case 0xdf0001: return 0xE0B2; //Down
case 0xdf0002: return 0xE0EA; //Left
case 0xdf0004: return 0xE0E9; //Right
case 0xfe1000: return 0x00C5;
case 0xfd0040: return 0x00C1;
case 0xbf0040: return 0x00C2;
case 0xbf0200: return 0x00C3;
case 0x7f0200: return 0x00C4;
case 0xf70200: return 0x00D7;
case 0xdf0200: return 0x00D8;
case 0x7f0800: return 0x00C6;
case 0xfd0800: return 0xE0DB;
case 0xfb0800: return 0xE0DC;
case 0xdf0040: return 0xE0DD;
default: return 0x0000;
}
}

void KeyboardLineScan(int select)
{
switch(select)
{
case 0:
KeyR0 = 0;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
case 1:
KeyR0 = 1;
KeyR1 = 0;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
case 2:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 0;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
case 3:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 0;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
case 4:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 0;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
case 5:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 0;
KeyR6 = 1;
KeyR7 = 1;
break;
case 6:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 0;
KeyR7 = 1;
break;
case 7:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 0;
break;
default:
KeyR0 = 1;
KeyR1 = 1;
KeyR2 = 1;
KeyR3 = 1;
KeyR4 = 1;
KeyR5 = 1;
KeyR6 = 1;
KeyR7 = 1;
break;
}
}

int GetLineScanCode(int line) //»ñÈ¡ÐÐɨÃèÂë
{
int ColumnData;
int KeyData;
KeyboardLineScan(line);
ColumnData = GPIO_ReadInputData(GPIOF);
KeyboardLineScan(8);
if (ColumnData != 0x0000FFFF)
KeyData = ((~ColumnData) & 0x0000FFFF) | ((~(0x00010000 << line)) & 0x00FF0000);
else
KeyData = 0x00000000;
return KeyData;
}

int LineMultiKeyDetec(int ScanCode)
{
int i;
int MultiKeyNum;
int Temp;
int ScanCodeHighBit;
MultiKeyNum = 0;
ScanCodeHighBit = ScanCode & 0xFFFF0000;

for(i=0;i<16;i++)
MultiKeyBuffer[i] = 0;

for(i=0;i<16;i++)
{
Temp = (ScanCode >> i) & 0x00000001;
if(Temp == 1)
{
MultiKeyBuffer[MultiKeyNum] = ScanCodeHighBit | (0x00000001 << i);
MultiKeyNum = MultiKeyNum + 1;
}
}
return MultiKeyNum;
}

void KeySendToUsb(void)
{
int i,j;
int LineScanCode;
int MakeCoding,BreakCoding;
int MultiKeyNum;
int CurScanCodeCnt = 0;
int SameFlag = 0;
uint8_t UsbDataSend[4];
UsbDataSend[0] = 0x01;
UsbDataSend[1] = 0x00;

CurScanCodeCnt = 0;
for(i=0;i<CombKeyNum;i++)
CurScanCode[i] = 0;

if (KeyboardTimeCnt > 2)
{
KeyboardTimeCnt = 0;
for(i=0;i<8;i++)
{
LineScanCode = KeyboardDrive.GetLineScanCode(i);
if(LineScanCode != 0x00000000)
{
if(KeyboardDrive.ScanCodeToMakeCode(LineScanCode) != 0x00000000)
{
CurScanCode[CurScanCodeCnt] = LineScanCode;
CurScanCodeCnt = CurScanCodeCnt + 1;
}
else
{
MultiKeyNum = KeyboardDrive.LineMultiKeyDetec(LineScanCode);
for(j=CurScanCodeCnt;j<CurScanCodeCnt+MultiKeyNum;j++)
CurScanCode[j] = MultiKeyBuffer[j-CurScanCodeCnt];
CurScanCodeCnt = CurScanCodeCnt + MultiKeyNum;
}
}
}

// LedConfigure.KeyToLed();

SameFlag = 0;
for(i=0;i<CurScanCodeCnt;i++)
{
for(j=0;j<CombKeyNum;j++)
{
if(CurScanCode[i] == OldScanCode[j])
SameFlag = 1;
}
if(SameFlag == 0)
{
MakeCoding = KeyboardDrive.ScanCodeToMakeCode(CurScanCode[i]);
LastSendData = MakeCoding;
SendEna = 1;

// LedConfigure.KeyMakeCurState(CurScanCode[i]);

// UsbDataSend[2] = (MakeCoding & 0xFF00) >> 8;
// UsbDataSend[3] = MakeCoding & 0x00FF;
// UsbSendData(UsbDataSend,4);
}
SameFlag = 0;
}

SameFlag = 0;
for(i=0;i<CombKeyNum;i++)
{
if(OldScanCode[i] != 0)
{
for(j=0;j<CurScanCodeCnt;j++)
{
if(OldScanCode[i] == CurScanCode[j])
SameFlag = 1;
}
if(SameFlag == 0)
{
BreakCoding = KeyboardDrive.ScanCodeToBreakCode(OldScanCode[i]);
SendEna = 0;
// LedConfigure.KeyBreakCurState(OldScanCode[i]);
UsbDataSend[2] = (BreakCoding & 0xFF00) >> 8;
UsbDataSend[3] = BreakCoding & 0x00FF;
UsbSendData(UsbDataSend,4);
}
SameFlag = 0;
}
}

for(i=0;i<CombKeyNum;i++)
OldScanCode[i] = CurScanCode[i];

}

if(LastSendTimeCnt > 5)
{
LastSendTimeCnt = 0;
if(SendEna == 1)
{
UsbDataSend[2] = (LastSendData & 0xFF00) >> 8;
UsbDataSend[3] = LastSendData & 0x00FF;
UsbSendData(UsbDataSend,4);
}
}
}

 

                                                                                                 My_usb.c

#include "stm32f10x.h"
#include "usb_lib.h"
#include "hw_config.h"
#include "usb_istr.h"
#include "my_usb.h"
#include "TgcConfig.h"

void UsbInitial()
{
Set_System();
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
}

void UsbSendData(uint8_t*Data,unsigned int DataLength)
{
UsbSendTimeCnt = 0;
//while(GetEPTxStatus(ENDP1) == EP_TX_VALID && UsbSendTimeCnt < 30);
while(GetEPTxStatus(ENDP1) == EP_TX_VALID);
UserToPMABufferCopy(Data,GetEPTxAddr(ENDP1),DataLength);
SetEPTxCount(ENDP1,DataLength);
SetEPTxValid(ENDP1);
}


void Delay(__IO uint32_t nCount)
{
for(; nCount!= 0;nCount--);
}

 

posted @ 2019-03-15 15:56  一人之下冯宝宝  阅读(706)  评论(0编辑  收藏  举报