金牌会员
- 积分
- 1451
- 威望
- 737
- 贡献
- 426
- 兑换币
- 424
- 注册时间
- 2013-9-25
- 在线时间
- 144 小时
|
2#
楼主 |
发表于 2014-5-11 21:17:15
|
只看该作者
#include <hidef.h>
#include <MC9S12XS128.h>
#include "OLED.h"
#include "PWM.h"
#include "math.h"
#define TESTPORT PORTA
uchar temp=0;
uchar key=0xff;
uchar key_1,key_2;
byte f[3];
byte ff[2];
void delay_0(uchar i)
{ uchar j=100;
while(i--)
while(j--);
}
uchar keyscan(void)
{
delay_0(15);
DDRA=0xff;
TESTPORT=0xF0;//行扫描
DDRA=0x00;
delay_0(5);
temp=TESTPORT;
temp=temp&0xF0;
temp=~((temp>>4)|0xF0);//读取列值
switch(temp)
{
case 1:key=0;break;
case 2:key=1;break;
case 4:key=2;break;
case 8:key=3;break;
default:key=0xff;break;
}
DDRA=0xff;
TESTPORT=0x0F;//列扫描
DDRA=0x00;
delay_0(5);
temp=TESTPORT;
temp=temp&0x0F;
temp=~(temp|0xF0);//读取行值
switch(temp)
{
case 1:key=key+0;break;
case 2:key=key+4;break;
case 4:key=key+8;break;
case 8:key=key+12;break;
default:key=0xff;break;
}
while(TESTPORT!=0xF0)//判断按键是否释放
{
delay_0(5);
DDRA=0xff;
TESTPORT=0xF0;
DDRA=0x00;
}
return key;
}
uchar key_judge()
{
DDRA=0xf0;
TESTPORT=0xF0;
DDRA=0x00;
delay(1);
if(TESTPORT!=0xF0)
delay(1);
if(TESTPORT!=0xF0)
{//是否有键按下
return 1;
}
else
{
return 0;
}//如果按键释放则返回键值
}
uchar i;
uchar j,kk[]={49,50,51,65,52,53,54,66,55,56,57,67,42,48,35,68};
void main(void)
{
set_80Mhz();
LCD_Init(); //LCD初始化
EnableInterrupts;
while(1)
{
key_flag=0;
key_1=keyscan();
j=kk[key_1];
key_2=key_1+1;
i=key_judge(); //i 可用作指示是否有键按下
f[0]=key_2/10+'0';
f[1]=key_2%10+'0';
f[2]='\0'; //'\0'表示结束,'0'表示48,0的ascii码值
LCD_P8x16Str(0,0,"i:");
LCD_P8x16Str(40,0,f);
ff[0]=j;
ff[1]='\0'; //'\0'表示结束,'0'表示48,0的ascii码值
LCD_P8x16Str(0,2,"j:");
LCD_P8x16Str(40,2,ff);
}
}
}
|
|