新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2020-7-13
- 在线时间
- 3 小时
|
楼主 |
发表于 2020-7-30 09:51:24
|
显示全部楼层
#include <STC15F2K60S2.H>
#include <intrins.h>
#define uchar unsigned char
static uchar key_flag1;
static uchar num_1;
static uchar ge,shi,bai;
uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
static uchar key_value;
void init(void)
{
P2=(P2&0x1f)|0x80;
P0=0xff;
P2=(P2&0x1f)|0xa0;
P0=0xaf;
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2&=0x1f;
P3=0xff;
}
void timersInit(void)
{
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0x28;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 108;
j = 144;
do
{
while (--j);
} while (--i);
}
void display()
{
bai=num_1/100;
shi=(num_1%100)/10;
ge=num_1%10;
P2=(P2&0x1f)|0xc0;
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=table[ge];
Delay10ms();
P2=(P2&0x1f)|0xc0;
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=table[shi];
Delay10ms();
P2=(P2&0x1f)|0xc0;
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=table[bai];
Delay10ms();
P2&=0x1f;
}
void read_key(void)
{
static uchar key_temp, key_temp_1, key_temp_2, key_temp_3, key_temp_4;
static uchar key_press=0;
P3 = 0xcf; P42 = 0; P44 = 0;
key_temp = P3;
key_temp = key_temp & 0xcf;
if(key_temp != 0xcf) key_press++;
else key_press = 0;
if( key_press == 3 )
{
P3=0xef; //0xef--11101111
P42=1; P44=1;
key_temp_1 = P3;
key_temp_1 = key_temp_1 & 0xef;
switch(key_temp_1)
{
case 0xee:
key_value = 19; key_flag1=1; break;
case 0xed:
key_value = 18; key_flag1=1; break;
case 0xeb:
key_value = 17; key_flag1=1; break;
case 0xe7:
key_value = 16; key_flag1=1; break;
}
while(key_temp_1 != 0xef); //等待按键释放
P3=0xdf; //0xdf--11011111
P42=1; P44=1;
key_temp_2 = P3;
key_temp_2 = key_temp_2 & 0xdf;
switch(key_temp_2)
{
case 0xde:
key_value = 15; key_flag1=1; break;
case 0xdd:
key_value = 14; key_flag1=1; break;
case 0xdb:
key_value = 13; key_flag1=1; break;
case 0xd7:
key_value = 12; key_flag1=1; break;
}
while(key_temp_2 != 0xdf);
P3=0xff;
P42=0; P44=1;
key_temp_3 = P3;
key_temp_3 = key_temp_3 & 0xff;
switch(key_temp_3)
{
case 0xfe:
key_value = 11; key_flag1=1;break;
case 0xfd:
key_value = 10; key_flag1=1;break;
case 0xfb:
key_value = 9; key_flag1=1;break;
case 0xf7:
key_value = 8; key_flag1=1;break;
}
while(key_temp_3 != 0x3f);
P3=0xff;
P42=1;
P44=0;
key_temp_4 = P3;
key_temp_4 = key_temp_4 & 0xff;
switch(key_temp_4)
{
case 0xfe:
key_value = 7; key_flag1=1; break;
case 0xfd:
key_value = 6; key_flag1=1; break;
case 0xfb:
key_value = 5; key_flag1=1; break;
case 0xf7:
key_value = 4; key_flag1=1; break;
}
while(key_temp_4 != 0xff);
}
}
uchar key_handle(uchar num)
{
switch(num)
{
case 4: num++; break;
case 5: num--; break;
case 8: num=num+2; break;
case 9: num=num-2; break;
case 12: num=num+5; break;
case 13: num=num-5; break;
case 16: num=num+10; break;
case 17: num=num-10; break;
case 15: num=num+50; break;
case 14: num=num-50; break;
case 19: num=num+100; break;
case 18: num=num-100; break;
case 6: num=0; break;
case 7: num=999; break;
case 10: num=300; break;
case 11: num=700; break;
case 0: num=num; break;
}
if(num>999)
{
num=999;
}
return num;
}
void time0_sevice() interrupt 1
{
if(key_flag1) //只有当按键按下并且读到键值,才会进行下一步的键值处理
{
key_flag1=0;
num_1=key_handle(key_value);
}
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main ()
{
init();
timersInit();
while(1)
{
display();
}
}
|
|