- //PS2 keyboard test program, you can wrap lines, press and hold shift and then enter
- //Can output uppercase, press CAPS to output uppercase, press again to output lowercase
- //This program is only used for testing, the code is redundant and is for reference only. You can delete it as needed.
- //PA13->PS2.CLK
PA15->PS2.DATA -
- #include
- #include
"sys.h" - #include
"usart.h" - #include
"delay.h" - #include
"led.h" - #include
"key.h" - #include
"exti.h" - #include
"wdg.h" - #include
"timer.h" - #include
"lcd.h" -
- const
u8 unshifted[][2]= //decoding table of shift key not pressed - {
-
0x0e,'`', -
0x15,'q', -
0x16,'1', -
0x1a,'z', -
0x1b,'s', -
0x1c,'a', -
0x1d,'w', -
0x1e,'2', -
0x21,'c', -
0x22,'x', -
0x23,'d', -
0x24,'e', -
0x25,'4', -
0x26,'3', -
0x29,' ', -
0x2a,'v', -
0x2b,'f', -
0x2c,'t', -
0x2d,'r', -
0x2e,'5', -
0x31,'n', -
0x32,'b', -
0x33,'h', -
0x34,'g', -
0x35,'y', -
0x36,'6', -
0x39,',', -
0x3a,'m', -
0x3b,'j', -
0x3c,'u', -
0x3d,'7', -
0x3e,'8', -
0x41,',', -
0x42,'k', -
0x43,'i', -
0x44,'o', -
0x45,'0', -
0x46,'9', -
0x49,'.', -
0x4a,'/', -
0x4b,'l', -
0x4c,';', -
0x4d,'p', -
0x4e,'-', -
0x52,''', -
0x54,'[', -
0x55,'=', -
0x5b,']', -
0x5d,'\', -
0x61,'<', -
0x69,'1', -
0x6b,'4', -
0x6c,'7', -
0x70,'0', -
0x71,'.', -
0x72,'2', -
0x73,'5', -
0x74,'6', -
0x75,'8', -
0x79,'+', -
0x7a,'3', -
0x7b,'-', -
0x7c,'*', -
0x7d,'9', -
0,0 - };
- const
u8 shifted[][2]= //shift key pressed decoding table - {
-
0x0e,'~', -
0x15,'Q', -
0x16,'!', -
0x1a,'Z', -
0x1b,'S', -
0x1c,'A', -
0x1d,'W', -
0x1e,'@', -
0x21,'C', -
0x22,'X', -
0x23,'D', -
0x24,'E', -
0x25,'$', -
0x26,'#', -
0x29,' ', -
0x2a,'V', -
0x2b,'F', -
0x2c,'T', -
0x2d,'R', -
0x2e,'%', -
0x31,'N', -
0x32,'B', -
0x33,'H', -
0x34,'G', -
0x35,'Y', -
0x36,'^', -
0x39,'L', -
0x3a,'M', -
0x3b,'J', -
0x3c,'U', -
0x3d,'&', -
0x3e,'*', -
0x41,'<', -
0x42,'K', -
0x43,'I', -
0x44,'O', -
0x45,')', -
0x46,'(', -
0x49,'>', -
0x4a,'?', -
0x4b,'L', -
0x4c,':', -
0x4d,'P', -
0x4e,'_', -
0x52,'"', -
0x54,'{', -
0x55,'+', -
0x5b,'}', -
0x5d,'|', -
0x61,'>', -
0x69,'1', -
0x6b,'4', -
0x6c,'7', -
0x70,'0', -
0x71,'.', -
0x72,'2', -
0x73,'5', -
0x74,'6', -
0x75,'8', -
0x79,'+', -
0x7a,'3', -
0x7b,'-', -
0x7c,'*', -
0x7d,'9', -
0,0 - };
-
- void
Decode(u8 scancode); // declare function prototype - u8
bitcount=11,ascii=' '; // bitcount is the bit count value; ascii is the translated ASCII code, the initial value is space - u8
hang=0,lie=0; //Color screen display position -
- int
main(void) - {
-
Stm32_Clock_Init(9); //System clock settings -
delay_init(72); //delay initialization -
uart_init(72,9600); //Serial port 1 initialization -
EXTIX_Init(); -
LED_Init(); -
LCD_Init(); -
LCD_Clear(YELLOW); -
-
POINT_COLOR=RED; -
BACK_COLOR=YELLOW; -
-
while(1) -
{ -
if(hang>=240) //line break -
{hang=0;lie+=16;if(lie>=320)lie=0;} -
LCD_ShowChar(hang,lie,ascii,16,0); // Display keyboard characters -
} - }
-
-
- void
Decode(u8 scancode) - {
-
static u8 up=0,shift=0; //up is the on/off code mark, shift is the shift key pressed mark -
u8 i; -
if (!up) //The received 11-bit data is a pass code (up is 0) -
{ -
switch (scancode) //Start translating scan code -
{ -
case 0xF0: //Keyboard release flag (the following byte is a break code) -
up=1; //Set up as the code break flag -
break; -
case 0x12: //Left shift key pressed -
shift=1; //Set shift to pressed sign -
break; -
case 0x59: //right shift key pressed -
shift=1; //Set shift to pressed sign -
break; -
case 0x58: //CAPS key pressed -
shift=(shift==0)?1:0;//Invert shift -
break; -
case 0x66: // Processing when the backspace key is pressed -
//Processing when the backspace key is pressed -
break; -
case 0x5a:// Processing when the Enter key is pressed: line break -
ascii=' '; -
hang=0; -
lie=lie+16; -
break; -
default: -
if(!shift) //If the shift key is not pressed -
{ //Find the unshifted table, the left column in the table is the scan code, the right column is the corresponding ASCII code -
for(i=0;unshifted[i][0]!=scancode&&unshifted[i][0];i++); -
if (unshifted[i][0] == scancode) -
{ -
ascii=unshifted[i][1]; -
hang=hang+8; -
} -
} -
else //If the shift key is pressed -
{ //Find the shifted table -
for(i=0;shifted[i][0]!=scancode&&shifted[i][0];i++); -
if(shifted[i][0]==scancode) -
{ -
ascii=shifted[i][1]; -
hang=hang+8; -
} -
} -
break; -
} -
} -
else //The received 11-bit data is broken (up is 1) -
{ -
up = 0; //Reset the broken code flag -
switch (scancode) // detect shift key release -
{ -
case 0x12 : //left shift key -
shift = 0; -
break; -
case 0x59 : //right shift key -
shift = 0; -
break; -
default: -
break; -
} -
} - }
In addition, you need to modify the two functions in the interrupt folder
- void
EXTIX_Init(void) - {
-
RCC->APB2ENR|=1<<2; //Enable PORTA clock -
JTAG_Set(JTAG_SWD_DISABLE); //Disable JTAG and SWD -
-
GPIOA->CRL&=0XFFFFFFF0; //PA0 is set as input -
GPIOA->CRL|=0X00000008; -
GPIOA->CRH&=0X0F0FFFFF; //PA13,15 are set as input -
GPIOA->CRH|=0X80800000; -
GPIOA->ODR|=1<<13; //PA13 is pulled up, PA0 is pulled down by default -
GPIOA->ODR|=1<<15; //PA15 pull-up -
-
Ex_NVIC_Config(GPIO_A,0,RTIR); //Rising edge trigger -
//Ex_NVIC_Config(GPIO_A,13,FTIR);//Falling edge trigger -
Ex_NVIC_Config(GPIO_A,15,FTIR|RTIR); //Falling and rising edge trigger -
-
MY_NVIC_Init(2,2,EXTI0_IRQChannel,2); //Preempt 2, subpriority 2, group 2 -
MY_NVIC_Init(2,1,EXTI15_10_IRQChannel,2); //Preempt 2, subpriority 1, group 2 - }
-
- //External interrupt 15~10 service program
- void
EXTI15_10_IRQHandler(void) - {
-
-
static u8 data; // Declare local static variables to save scan codes -
if (!PAin(15)) // If the falling edge triggers the interrupt -
{ -
if(bitcount < 11 && bitcount > 2) // bits 3 to 10 are data, start bit, check bit and stop bit are ignored -
{ -
data = (data >> 1); // right shift to save data -
if(PAin(13)&0x01) -
{ -
data|=0x80; //store a '1' -
} -
} -
} -
else //If the rising edge triggers the interrupt -
{ -
if(--bitcount==0) //If all 11 bits are received -
{ -
Decode(data); //Translate the scan code into ASCII code -
bitcount = 11; // reset to 11 bits -
} -
} - //
delay_ms(10); //debounce - //
if(KEY0==0) //Key 0 - //
{ - //
LED0=!LED0; - //
}else if(KEY1==0)//Key 1 - //
{ - //
LED1=!LED1; - //
} - //
EXTI->PR=1<<13; // Clear the interrupt flag on LINE13 -
EXTI->PR=1<<15; //Clear the interrupt flag on LINE15 -
- }
Previous article:Comparison between LPC1114/LPC11U14 and LPC1343 (Part 2) GPIO
Next article:STM32F4xx FPU settings
- Popular Resources
- Popular amplifiers
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- 【Perf-V Review】Three-color LED
- Koreans are really unreliable!
- CC3200-LAUNCHXL Development Board Evaluation Report
- STM32MP157A-DK1 Evaluation (6) Programming Environment under Cortex-A7 Linux
- MSP430 Learning Clock
- 【Silicon Labs Development Kit Review】+Development Environment Construction
- In fact, there are many small circuits in RF, which are very useful. As we all know, RF modules are very expensive, and the better the performance, the more expensive it is. ...
- TK2683 Insulation Resistance Tester (Tesco Electronics)
- Can I set the I2C pin on ESP32?
- ESD protection is carried out using capacitors. I have many problems. Please help!