通过4*4键盘矩阵按键在LCD12864中显示1~16,原理图如下:
:
输出结果为乱码,按键状态是否有扫描到?
请问我的程序该怎么改呢?
单片机源程序如下:
- /*-----------------------------------------------
- ------------------------------------------------*/
- #include <STC12C5A60S2.H>
- #include <intrins.h>
- #include "delay.h"
- #include <lcd12864.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ufloat unsigned float
- sbit BLA=P2^3;
- //sbit RL=P1^2;
- sbit K1=P1^4;
- sbit K2=P1^5;
- sbit K3=P1^6;
- sbit K4=P1^7;
- sbit K5=P3^4;
- sbit K6=P3^5;
- sbit K7=P3^6;
- sbit K8=P3^7;
- unsigned char KeyPro(void);
- //unsigned char dofly_code[]={'1','2','3','15','4','5','6','14','7','8','9','13','10','0','11','12'};
- unsigned char code TAB[]={1,2,3,15,4,5,6,14,7,8,9,13,10,0,11,12};
- /*------------------------------------------------
- Ö÷3ìDò
- ------------------------------------------------*/
- main()
- {
- unsigned char i,j,num;
- //unsigned char s[];
- BLA=1;
- CGRAM(); /
- while (1)
- {
- Init_ST7920(); //
- LCD_PutString(0,1,"请输入键盘");
- DisplayCGRAM(7,2);/
-
- num=KeyPro(); //
- if(num!=0xff) //
- DelayMs(200);
- if(num!=0xff)
- LCD_PutString(0+i,1+j,TAB[num]);//
- i++;
- if(i==128)//
- {
- i=0;j++;
- if(j==64)//
- {
- j=0;
-
- }
- }
- //ClrScreen();
- //DelayMs(20000);
- }
- }
- unsigned char SanKeyA(void)
- {
- unsigned char PA;
- if(K1 ==1)//扫描第一行
- {
- PA=0x01;
- return PA;
- }
- if(K2==1)//扫描第二行
- {
- PA=0x01<<1;
- return PA;
- }
- if(K3 ==1)/
- {
- PA=0x01<<2;
- return PA;
- }
- if(K4==1)/
- {
- PA=0x01<<3;
- return PA;
- }
- }
- unsigned char SanKeyB(void)
- {
- unsigned char PB;
- if(K5 ==1)
- {
- PB=0x01<<4;扫描第一列
- return PB;
- }
- if(K6==1)//扫描第二行
- {
- PB=0x01<<5;
- return PB;
- }
- if(K7 ==1)
- {
- PB=0x01<<6;
- return PB;
- }
- if(K8==1)
- {
- PB=0x01<<7;
- return PB;
- }
- }
- unsigned char KeyPro(void)
- {
- unsigned char sum;
- sum=SanKeyA()+SanKeyB();
- switch(sum)
- {
- case 0x11:return 1 ;break;//°′ÏÂÏàó|μļüÏÔê¾Ïà¶Ôó|μÄÂëÖμ
- case 0x21:return 2 ;break;//
- case 0x41:return 3 ;break;//
- case 0x81:return 15;break;//
- case 0x12:return 4 ;break;//
- case 0x22:return 5 ;break;//
- case 0x42:return 6 ;break;//
- case 0x82:return 14;break;//
- case 0x14:return 7 ;break;//
- case 0x24:return 8 ;break;//
- case 0x44:return 9 ;break;//
- case 0x84:return 13;break;//
- case 0x18:return 10;break;//
- case 0x28:return 0; break;//
- case 0x48:return 11;break;//
- case 0x88:return 12;break;//
- default:return 0xff;break;
- }
- }
复制代码
|