#include<reg52.h>
#include<absacc.h>
#include"delay10nms.h"
#include"digit.h"
#define DELAY_TIME 20000
sbit OE_DIGIT = P3^7;
sbit LE_DIGIT = P3^6;
sbit OE_KEY = P3^5;
sbit LE_KEY = P3^4;
void tc0_srv(void) interrupt 1
{
unsigned char key_h,key_v,key_tmp,key_val,last_key_scan_val;
unsigned char last_key_val;//last_key_val记录上一次按键按下的键值,oprt(operation)作为运算符的变量
double Ans = 0,fig = 0;
TH0 = (65535 - DELAY_TIME) >> 8;
TL0 = (65535 - DELAY_TIME) & 0xff;
key_val = 0;//键盘扫描开始
for(key_h = 4;key_h < 8;key_h ++)
{
P0 = (1 << key_h);
OE_KEY = 0;
LE_KEY = 1;
LE_KEY = 0;
P0 |= 0x0f;
key_tmp = P0 & 0x0f;//用key_tmp取出P0口低四位
OE_KEY = 1;
if(key_tmp)
{
for(key_v = 0;key_v < 4;key_v ++)
{
if(key_tmp & (1 << key_v))
{
key_val = (key_h - 4) * 4 + key_v + 1;
break;
}
}
}
if(key_val) break;?
}//键盘扫描结束
if(last_key_scan_val != key_val)//按键消抖处理,如果一个按键被按下时间短于10ms则不是可靠按下
{
last_key_scan_val = key_val;
}
else
{
if(last_key_scan_val)//开始对得到的键值key_val进行处理分析
{
if(Ans)
{
switch (last_key_val)
{
case 0:
{
if(key_val == 1) last_key_val = 1;
if(key_val == 5) last_key_val = 2;
if(key_val == 9) last_key_val = 3;
if(key_val == 13) last_key_val = 4;
if(key_val == 2) Ans = Ans + fig;
if(key_val == 4) {Ans = 0;digit(0);}
else
{
Ans = Ans*10+key_val;
digit(Ans);
}
}
case 1:
{
if(key_val == 1);
if(key_val == 5) last_key_val = 2;
if(key_val == 9) last_key_val = 3;
if(key_val == 13) last_key_val = 4;
if(key_val == 2) {last_key_val = 0;Ans = 0;}
if(key_val == 4) {Ans = 0;digit(0);}
else
{
fig = fig*10+key_val;
digit(fig);
}
}
}
}
else
{
}//就是这一段关键性的代码写不好,我的想法是用last_key_val记录上一次按键的键值,
然后根据上一次的键值来决定如何做
}
}
}
void init_tc0(void)
{
TMOD = 1;
TH0 = (65535 - DELAY_TIME) >> 8;
TL0 = (65535 - DELAY_TIME) & 0xff;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
init_tc0();
EA = 1;
P1 = 0;
while(1);
}
/*digit()函数可以直接让参数显示在数码管上*/
#include<reg52.h>
#include<absacc.h>
#include"pow10.h"
#include"delay10nms.h"
sbit OE_DIGIT = P3^7;
sbit LE_DIGIT = P3^6;
void digit(double n)
{
unsigned char i;
double m;
unsigned char n_bit[8];
unsigned char digit[] =?
{
0xC0,//11000000,0
0xF9,//11111001,1
0xA4,//10100100,2
0xB0,//10110000,3
0x99,//10011001,4
0x92,//10010010,5
0x82,//10000010,6
0xF8,//11111000,7
0x80,//10000000,8
0x90,//10010000,9
};//数码管段选
for(i = 8; i > 0;i --)
{
m = pow10(i);
if(!(n/m))
{
n_bit[8 - i] = 0;?
}
else
{
n_bit[8-i] = n/m;
n = n - n_bit[8-i] * m;
}
}
OE_DIGIT = 0;
LE_DIGIT = 1;
while(1)
{
unsigned char mon = 0 ;//监控是否已经显示到第一位非0数字 ??
P2 = 0x80;
i = 0;
while(P2 != 0)
{
if(mon == 0)
{ ?
if(n_bit
== 0) P1 = 0xff;
else
{
P1 = digit[n_bit];
mon = 1;
}
}
else
{
P1 = digit[n_bit];
}
delay10nms(1);
P1 = 0xff;
P2 = P2 >> 1; ?
i ++;
}
}
}
/*延时函数*/
void delay10nms(unsigned int n)
{
unsigned p,q;
while(n --)
{
for(p = 128;p > 0;p --)
for(q = 10;q > 0;q --);
}
}//这段延时太不靠谱了
/*pow10()函数*/
double pow10(unsigned char n)
{
unsigned char k;
double s = 1;
for(k = 1;k < n;k ++)
{
s = s * 10;
}
return(s);
}
求高人给意见,可以延续我这个思路,也可以给点其他的思路
|