stm32_ps2 keyboard display test program

Publisher:脑电风暴Latest update time:2015-09-09 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. //PS2 keyboard test program, you can wrap lines, press and hold shift and then enter  
  2. //Can output uppercase, press CAPS to output uppercase, press again to output lowercase  
  3. //This program is only used for testing, the code is redundant and is for reference only. You can delete it as needed.  
  4. //PA13->PS2.CLK   PA15->PS2.DATA  
  5.   
  6. #include   
  7. #include  "sys.h"  
  8. #include  "usart.h"        
  9. #include  "delay.h"    
  10. #include  "led.h"   
  11. #include  "key.h"  
  12. #include  "exti.h"  
  13. #include  "wdg.h"  
  14. #include  "timer.h"  
  15. #include  "lcd.h"         
  16.   
  17. const  u8  unshifted[][2]=             //decoding table of shift key not pressed  
  18.         
  19.   0x0e,'`',  
  20.   0x15,'q',  
  21.   0x16,'1',  
  22.   0x1a,'z',  
  23.   0x1b,'s',  
  24.   0x1c,'a',  
  25.   0x1d,'w',  
  26.   0x1e,'2',  
  27.   0x21,'c',  
  28.   0x22,'x',  
  29.   0x23,'d',  
  30.   0x24,'e',  
  31.   0x25,'4',  
  32.   0x26,'3',  
  33.   0x29,'  ',  
  34.   0x2a,'v',  
  35.   0x2b,'f',  
  36.   0x2c,'t',  
  37.   0x2d,'r',  
  38.   0x2e,'5',  
  39.   0x31,'n',  
  40.   0x32,'b',  
  41.   0x33,'h',  
  42.   0x34,'g',  
  43.   0x35,'y',  
  44.   0x36,'6',  
  45.   0x39,',',  
  46.   0x3a,'m',  
  47.   0x3b,'j',  
  48.   0x3c,'u',  
  49.   0x3d,'7',  
  50.   0x3e,'8',  
  51.   0x41,',',  
  52.   0x42,'k',  
  53.   0x43,'i',  
  54.   0x44,'o',  
  55.   0x45,'0',  
  56.   0x46,'9',  
  57.   0x49,'.',  
  58.   0x4a,'/',  
  59.   0x4b,'l',  
  60.   0x4c,';',  
  61.   0x4d,'p',  
  62.   0x4e,'-',  
  63.   0x52,''',  
  64.   0x54,'[',  
  65.   0x55,'=',  
  66.   0x5b,']',  
  67.   0x5d,'\',  
  68.   0x61,'<',  
  69.   0x69,'1',  
  70.   0x6b,'4',  
  71.   0x6c,'7',  
  72.   0x70,'0',  
  73.   0x71,'.',  
  74.   0x72,'2',  
  75.   0x73,'5',  
  76.   0x74,'6',  
  77.   0x75,'8',  
  78.   0x79,'+',  
  79.   0x7a,'3',  
  80.   0x7b,'-',  
  81.   0x7c,'*',  
  82.   0x7d,'9',  
  83.   0,0  
  84. };  
  85. const  u8  shifted[][2]=       //shift key pressed decoding table  
  86.  
  87.   0x0e,'~',  
  88.   0x15,'Q',  
  89.   0x16,'!',  
  90.   0x1a,'Z',  
  91.   0x1b,'S',  
  92.   0x1c,'A',  
  93.   0x1d,'W',  
  94.   0x1e,'@',  
  95.   0x21,'C',  
  96.   0x22,'X',  
  97.   0x23,'D',  
  98.   0x24,'E',  
  99.   0x25,'$',  
  100.   0x26,'#',  
  101.   0x29,'  ',  
  102.   0x2a,'V',  
  103.   0x2b,'F',  
  104.   0x2c,'T',  
  105.   0x2d,'R',  
  106.   0x2e,'%',  
  107.   0x31,'N',  
  108.   0x32,'B',  
  109.   0x33,'H',  
  110.   0x34,'G',  
  111.   0x35,'Y',  
  112.   0x36,'^',  
  113.   0x39,'L',  
  114.   0x3a,'M',  
  115.   0x3b,'J',  
  116.   0x3c,'U',  
  117.   0x3d,'&',  
  118.   0x3e,'*',  
  119.   0x41,'<',  
  120.   0x42,'K',  
  121.   0x43,'I',  
  122.   0x44,'O',  
  123.   0x45,')',  
  124.   0x46,'(',  
  125.   0x49,'>',  
  126.   0x4a,'?',  
  127.   0x4b,'L',  
  128.   0x4c,':',  
  129.   0x4d,'P',  
  130.   0x4e,'_',  
  131.   0x52,'"',  
  132.   0x54,'{',  
  133.   0x55,'+',  
  134.   0x5b,'}',  
  135.   0x5d,'|',  
  136.   0x61,'>',  
  137.   0x69,'1',  
  138.   0x6b,'4',  
  139.   0x6c,'7',  
  140.   0x70,'0',  
  141.   0x71,'.',  
  142.   0x72,'2',  
  143.   0x73,'5',  
  144.   0x74,'6',  
  145.   0x75,'8',  
  146.   0x79,'+',  
  147.   0x7a,'3',  
  148.   0x7b,'-',  
  149.   0x7c,'*',  
  150.   0x7d,'9',  
  151.   0,0  
  152. };  
  153.   
  154. void  Decode(u8  scancode); //  declare function prototype  
  155. u8  bitcount=11,ascii='  ';    //  bitcount is the bit count value; ascii is the translated ASCII code, the initial value is space  
  156. u8  hang=0,lie=0;   //Color screen display position  
  157.   
  158. int  main(void)  
  159.         
  160.   Stm32_Clock_Init(9); //System clock settings  
  161.     delay_init(72);      //delay initialization  
  162.     uart_init(72,9600);  //Serial port 1 initialization    
  163.     EXTIX_Init();  
  164.     LED_Init();  
  165.     LCD_Init();  
  166.     LCD_Clear(YELLOW);  
  167.       
  168.     POINT_COLOR=RED;  
  169.     BACK_COLOR=YELLOW;  
  170.       
  171.   while(1)   
  172.                 
  173.         if(hang>=240)  //line break  
  174.         {hang=0;lie+=16;if(lie>=320)lie=0;}   
  175.         LCD_ShowChar(hang,lie,ascii,16,0);       //  Display keyboard characters  
  176.                                                     
  177.  
  178.   
  179.   
  180. void  Decode(u8  scancode)  
  181.  
  182.  static  u8  up=0,shift=0;     //up is the on/off code mark, shift is the shift key pressed mark  
  183.  u8  i;  
  184.  if  (!up)           //The received 11-bit data is a pass code (up is 0)  
  185.   
  186.   switch  (scancode) //Start translating scan code  
  187.    
  188.    case  0xF0:       //Keyboard release flag (the following byte is a break code)  
  189.         up=1;       //Set up as the code break flag  
  190.         break;  
  191.    case  0x12:       //Left shift key pressed  
  192.         shift=1;    //Set shift to pressed sign  
  193.         break;  
  194.    case  0x59:       //right shift key pressed  
  195.         shift=1;    //Set shift to pressed sign  
  196.         break;     
  197.      case  0x58:   //CAPS key pressed  
  198.           shift=(shift==0)?1:0;//Invert shift  
  199.             break;  
  200.      case  0x66:  //  Processing when the backspace key is pressed  
  201.             //Processing when the backspace key is pressed  
  202.             break;  
  203.      case  0x5a://  Processing when the Enter key is pressed: line break  
  204.             ascii='  ';  
  205.             hang=0;  
  206.             lie=lie+16;  
  207.             break;  
  208.    default:          
  209.         if(!shift)             //If the shift key is not pressed  
  210.                             //Find the unshifted table, the left column in the table is the scan code, the right column is the corresponding ASCII code  
  211.            for(i=0;unshifted[i][0]!=scancode&&unshifted[i][0];i++);  
  212.            if (unshifted[i][0] == scancode)   
  213.             
  214.             ascii=unshifted[i][1];  
  215.                      hang=hang+8;  
  216.             
  217.           
  218.         else                   //If the shift key is pressed  
  219.                             //Find the shifted table  
  220.            for(i=0;shifted[i][0]!=scancode&&shifted[i][0];i++);  
  221.            if(shifted[i][0]==scancode)   
  222.             
  223.             ascii=shifted[i][1];  
  224.                       hang=hang+8;  
  225.             
  226.             
  227.         break;  
  228.    
  229.    
  230.  else                    //The received 11-bit data is broken (up is 1)  
  231.   
  232.   up  0;                //Reset the broken code flag  
  233.   switch  (scancode)      // detect shift key release  
  234.    
  235.    case  0x12           //left shift key  
  236.         shift  0;  
  237.         break;  
  238.    case  0x59           //right shift key  
  239.         shift  0;  
  240.         break;  
  241.    default:  
  242.         break;  
  243.    
  244.   
  245.  

 

In addition, you need to modify the two functions in the interrupt folder

[cpp]  view plain copy
  1. void  EXTIX_Init(void)  
  2.  
  3.     RCC->APB2ENR|=1<<2;      //Enable PORTA clock  
  4.     JTAG_Set(JTAG_SWD_DISABLE); //Disable JTAG and SWD     
  5.   
  6.     GPIOA->CRL&=0XFFFFFFF0; //PA0 is set as input        
  7.     GPIOA->CRL|=0X00000008;     
  8.     GPIOA->CRH&=0X0F0FFFFF; //PA13,15 are set as input        
  9.     GPIOA->CRH|=0X80800000;                   
  10.     GPIOA->ODR|=1<<13;     //PA13 is pulled up, PA0 is pulled down by default  
  11.     GPIOA->ODR|=1<<15;     //PA15 pull-up  
  12.   
  13.     Ex_NVIC_Config(GPIO_A,0,RTIR);  //Rising edge trigger  
  14.     //Ex_NVIC_Config(GPIO_A,13,FTIR);//Falling edge trigger  
  15.     Ex_NVIC_Config(GPIO_A,15,FTIR|RTIR); //Falling and rising edge trigger  
  16.   
  17.     MY_NVIC_Init(2,2,EXTI0_IRQChannel,2);     //Preempt 2, subpriority 2, group 2  
  18.     MY_NVIC_Init(2,1,EXTI15_10_IRQChannel,2); //Preempt 2, subpriority 1, group 2      
  19.  
  20.   
  21. //External interrupt 15~10 service program  
  22. void  EXTI15_10_IRQHandler(void)  
  23.             
  24.       
  25.     static  u8  data;    //  Declare local static variables to save scan codes  
  26.   if  (!PAin(15))                 //  If the falling edge triggers the interrupt  
  27.    
  28.   if(bitcount  11  &&  bitcount  2)     // bits 3 to 10 are data, start bit, check bit and stop bit are ignored  
  29.     
  30.     data  (data  >>  1);                   // right shift to save data  
  31.     if(PAin(13)&0x01)  
  32.      
  33.       data|=0x80;                       //store a '1'  
  34.      
  35.    
  36.    
  37.  else                     //If the rising edge triggers the interrupt  
  38.    
  39.   if(--bitcount==0)       //If all 11 bits are received  
  40.    
  41.    Decode(data);      //Translate the scan code into ASCII code     
  42.    bitcount  11;         // reset to 11 bits  
  43.    
  44.   
  45. //   delay_ms(10);     //debounce              
  46. //   if(KEY0==0)       //Key 0  
  47. //    
  48. //       LED0=!LED0;  
  49. //   }else  if(KEY1==0)//Key 1  
  50. //    
  51. //       LED1=!LED1;  
  52. //    
  53. //   EXTI->PR=1<<13;      // Clear the interrupt flag on LINE13    
  54.     EXTI->PR=1<<15;      //Clear the interrupt flag on LINE15    
  55.   

Keywords:stm32 Reference address:stm32_ps2 keyboard display test program

Previous article:Comparison between LPC1114/LPC11U14 and LPC1343 (Part 2) GPIO
Next article:STM32F4xx FPU settings

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号