文档库 最新最全的文档下载
当前位置:文档库 › XS12单片机4X4键盘

XS12单片机4X4键盘

4X4键盘我用XS12单片机做的出了很大的问题 单独行扫描正确 单独列也正确 但是和在一起就不正确了!求高手指点啊!!
后来用扫描法解决了!
#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
/*********************************************************************************************************
* 宏定义
*********************************************************************************************************/
#define delay1us {_asm nop;}
/*#define DATA 0xff
#define LCD_EN 0x04 //B2
#define LCD_RS 0x01 //B0
#define LCD_RW 0x02 //B1
#define LCD_DataPort PORTA // A口作为数据口
#define LCD_ENPort PORTB
#define LCD_RWPort PORTB
#define LCD_RSPort PORTB //B0 B1 B2 */
/*********************************************************************************************************
* 全局变量声明
*********************************************************************************************************/
uchar Scan_Code[16]={0x7e,0x7d,0x7b,0x77,0xbe,0xbd,0xbb,0xb7,0xde,0xdd,0xdb,0xd7,
0xee,0xed,0xeb,0xe7};
uchar Word_Shape[17]={0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xf0,0x00,0x30,0x10,0x06,
0x8c,0x42,0x0c,0x1c,0xff};
uchar Pot_Read=0,Code_Fin=0,row=0,column=0;
void delaynus(byte cnt)
{
byte i;
for(i = 0; i < cnt; i++)
{
delay1us;
}
}
void delay1ms(void)
{uint i;
for(i=0;i<1140;i++);
}
void delaynms(uint n)
{ uint i;
for(i=0;idelay1ms();
}
/*void LCD_WC(uchar cmd,uchar data)
{
if(cmd==0) LCD_RSPort&=~LCD_RS; //cmd=0 write the command 1:write the data
else LCD_RSPort|=LCD_RS;
LCD_DataPort=data;
delaynms(10);
LCD_ENPort|=LCD_EN; //rising and falling
delaynus(10);
LCD_ENPort&=~LCD_EN;
delaynus(10);

}
void LCD_init(void)
{
DDRA = DATA;
DDRB|=LCD_EN|LCD_RS|LCD_RW;
LCD_RWPort &=~ LCD_RW;
LCD_DataPort=0x30; // 3:5*7 4:16x2 5:8bit
LCD_ENPort|= LCD_EN;
delaynus(1);
LCD_ENPort&=~LCD_EN;
delaynus(40);
LCD_WC(0,0x38); //显示模式设置
LCD_WC(0,0x0c); //显示开及光标设置
LCD_WC(0,0x01); //显示清屏
LCD_WC(0,0x06); //显示光标移动位置
}
void LCD_XY(uchar x,uchar y)
{
uchar addr;

if(y==0) addr=0x80+x; // first line 0x80
else addr=0xc0+x; //second line 0xc0
LCD_WC(0,addr);
}
void LCD_Wstr(uchar X,uchar Y,char *str) //x : line increase direction y:column
{
LCD_XY(X,Y);
while(*str)
{
LCD_WC(1,*str); //cmd=1 write the string
str++;
}
} */
void key(void)
{
DDRA=0xff;
PORTA = 0xf7; //低四位输出为零
for(;;)
{
PUCR=1;
DDRB=0x0f;
PORTB &= 0xf0;
//Pot_Read=PORTB;
if(PORTB!=0xf0)
{

delaynms(8);

if(PORTB!=0xf0)
{

PORTB&=0xf0;
Pot_Read=PORTB;
switch( PORTB)
{
case 0xe0:column=0;break;
case 0xd0:column=1;break;
case 0xb0:column=2;break;
case 0x70:column=3;break;
default:break;
}
PORTB = 0x0e;
if(Pot_Read==PORTB) row=0;
PORTB = 0x0d;
if(Pot_Read==PORTB) row=1;
PORTB = 0x0b;
if(Pot_Read==PORTB) row=2;
PORTB = 0x07;
if(Pot_Read==PORTB) row=3;
Pot_Read&=0xf0;

/*switch(Pot_Read)
{
case 0xe0:row=0;break;
case 0xd0:row=1;break;
case 0xb0:row=2;break;
case 0x70:row=3;break;
default:break;
} */
Code_Fin=(row*4+column);
delaynms(10);
PORTA=Word_Shape[Code_Fin];

//LCD_Wstr(0,0,"Code_Fin");
//delaynms(1000);
/*switch(Code_Fin) {
case 0: LCD_Wstr(8,0,"0");break;
case 1: LCD_Wstr(8,0,"1");break;
case 2: LCD_Wstr(8,0,"2");break;
case 3: LCD_Wstr(8,0,"3");break;
case 4: LCD_Wstr(8,0,"4");break;
case 5: LCD_Wstr(8,0,"5");break;
case 6: LCD_Wstr(8,0,"6");break;
case 7: LCD_Wstr(8,0,"7");break;
case 8: LCD_Wstr(8,0,"8");break;
case 9: LCD_Wstr(8,0,"9");break;
case 10 CD_Wstr(8,0,"10");break;
case 11: LCD_Wstr(8,0,"11"); break;
case 12: LCD_Wstr(8,0,"12"); break;
case 13: LCD_Wstr(8,0,"13"); break;
case 14: LCD_Wstr(8,0,"14"); break;
case 15: LCD_Wstr(8,0,"15"); break;

default:break;
}*/
// }
}
}
}
}
/*void display(void)
{
uchar state;
state=key();
DDRB=0Xff;
PORTB=0x00;
delaynms(10);
PORTB=0xc0;
PORTB=Word_Shape[Code_Fin];
}*/
void main(void)
{
//LCD_init();
for(;;)
key();

}

相关文档
相关文档 最新文档