stm32_ps2鍵盤顯示測試程序
- //PS2鍵盤測試程序,可換行,按shift不放接著輸入
- //可輸出大寫,按下CAPS輸出大寫,再次按下輸出小寫
- //此程序只用來測試,代碼冗余,僅供參考,可根據(jù)需要自行刪減
- //PA13->PS2.CLKPA15->PS2.DATA
- #include<stm32f10x_lib.h>
- #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"
- constu8unshifted[][2]=//shift鍵沒按下譯碼表
- {
- 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
- };
- constu8shifted[][2]=//shift鍵按下譯碼表
- {
- 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
- };
- voidDecode(u8scancode);//聲明函數(shù)原型
- u8bitcount=11,ascii=;//bitcount為位計數(shù)值;ascii為翻譯后的ASCII碼,初值為空格
- u8hang=0,lie=0;//彩屏顯示位置
- intmain(void)
- {
- Stm32_Clock_Init(9);//系統(tǒng)時鐘設置
- delay_init(72);//延時初始化
- uart_init(72,9600);//串口1初始化
- EXTIX_Init();
- LED_Init();
- LCD_Init();
- LCD_Clear(YELLOW);
- POINT_COLOR=RED;
- BACK_COLOR=YELLOW;
- while(1)
- {
- if(hang>=240)//換行
- {hang=0;lie+=16;if(lie>=320)lie=0;}
- LCD_ShowChar(hang,lie,ascii,16,0);//顯示鍵盤字符
- }
- }
- /*******************************************
- 函數(shù)名稱:Decode
- 功能:
- 參數(shù):scancode--需要翻譯的掃描碼
- 返回值:無
- ********************************************/
- voidDecode(u8scancode)
- {
- staticu8up=0,shift=0;//up為通、斷碼標志,shift為shift鍵按下標志
- u8i;
- if(!up)//已接收的11位數(shù)據(jù)是通碼(up為0)
- {
- switch(scancode)//開始翻譯掃描碼
- {
- case0xF0://鍵盤釋放標志(隨后的一個字節(jié)是斷碼)
- up=1;//設置up為斷碼標志
- break;
- case0x12://左shift鍵按下
- shift=1;//設置shift為按下標志
- break;
- case0x59://右shift鍵按下
- shift=1;//設置shift為按下標志
- break;
- case0x58://CAPS鍵按下處理
- shift=(shift==0)?1:0;//shift取反
- break;
- case0x66://backspace鍵按下時處理
- //backspace鍵按下時處理
- break;
- case0x5a://回車鍵按下時處理:換行
- ascii=;
- hang=0;
- lie=lie+16;
- break;
- default:
- if(!shift)//如果shift鍵沒有按下
- {//查找unshifted表,表中左列是掃描碼,右列是對應的ASCII碼
- for(i=0;unshifted[i][0]!=scancode&&unshifted[i][0];i++);
- if(unshifted[i][0]==scancode)
- {
- ascii=unshifted[i][1];
- hang=hang+8;
- }
- }
- else//如果shift鍵按下
- {//查找shifted表
- 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//已接收的11位數(shù)據(jù)是斷碼(up為1)
- {
- up=0;//將斷碼標志復位
- switch(scancode)//檢測shift鍵釋放
- {
- case0x12://左shift鍵
- shift=0;
- break;
- case0x59://右shift鍵
- shift=0;
- break;
- default:
- break;
- }
- }
- }
此外還需修改中斷文件夾中的兩個函數(shù)
[cpp]view plaincopy
- voidEXTIX_Init(void)
- {
- RCC->APB2ENR|=1<<2;//使能PORTA時鐘
- JTAG_Set(JTAG_SWD_DISABLE);//關閉JTAG和SWD
- GPIOA->CRL&=0XFFFFFFF0;//PA0設置成輸入
- GPIOA->CRL|=0X00000008;
- GPIOA->CRH&=0X0F0FFFFF;//PA13,15設置成輸入
- GPIOA->CRH|=0X80800000;
- GPIOA->ODR|=1<<13;//PA13上拉,PA0默認下拉
- GPIOA->ODR|=1<<15;//PA15上拉
- Ex_NVIC_Config(GPIO_A,0,RTIR);//上升沿觸發(fā)
- //Ex_NVIC_Config(GPIO_A,13,FTIR);//下降沿觸發(fā)
- Ex_NVIC_Config(GPIO_A,15,FTIR|RTIR);//下降、上升沿觸發(fā)
- MY_NVIC_Init(2,2,EXTI0_IRQChannel,2);//搶占2,子優(yōu)先級2,組2
- MY_NVIC_Init(2,1,EXTI15_10_IRQChannel,2);//搶占2,子優(yōu)先級1,組2
- }
- //外部中斷15~10服務程序
- voidEXTI15_10_IRQHandler(void)
- {
- staticu8data;//聲明局部靜態(tài)變量來保存掃描碼
- if(!PAin(15))//如果是下降沿觸發(fā)中斷
- {
- if(bitcount<11&&bitcount>2)//3到10位是數(shù)據(jù),起始位,校驗位和停止位忽略
- {
- data=(data>>1);//右移保存數(shù)據(jù)
- if(PAin(13)&0x01)
- {
- data|=0x80;//存儲一個1
- }
- }
- }
- else//如果是上升沿觸發(fā)中斷
- {
- if(--bitcount==0)//如果11位全部接收完畢
- {
- Decode(data);//將掃描碼翻譯成ASCII碼
- bitcount=11;//重新設為11位數(shù)據(jù)
- }
- }
- //delay_ms(10);//消抖
- //if(KEY0==0)//按鍵0
- //{
- //LED0=!LED0;
- //}elseif(KEY1==0)//按鍵1
- //{
- //LED1=!LED1;
- //}
- //EXTI->PR=1<<13;//清除LINE13上的中斷標志位
- EXTI->PR=1<<15;//清除LINE15上的中斷標志位
- }
評論