新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于單片機(jī)的數(shù)碼管按鍵計(jì)算器程序

基于單片機(jī)的數(shù)碼管按鍵計(jì)算器程序

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

/*
*制作數(shù)碼管浮點(diǎn)數(shù)據(jù)
*/
void make_led_fnumber(float f_num)
{
bit s_flag=0;
int32 num;
int16 sit;
int8 i, decimal, dot_sit=0;

本文引用地址:http://m.butianyuan.cn/article/201611/324029.htm

if (f_num < 0)
{
s_flag = 1;
f_num = -f_num;
}

num = (int32)(f_num*10000+0.5);
for (decimal=4; decimal>0; decimal--)
{
if (num % 10 != 0)
break;
num /= 10;
}

dot_sit = decimal;
if (f_num >= 10000)
dot_sit = 0;
else if (f_num >= 1000)
if (decimal >= 1)
dot_sit = 1;
else if (f_num >= 100)
if (decimal >= 2)
dot_sit = 2;
else if (f_num >= 10)
if (decimal >= 3)
dot_sit = 3;
for (i=0; i f_num *= 10;
num = (int32)(f_num+0.5);
ET0 = 0;
for (i=4, sit=10000; i>=1; i--, sit/=10)
{
if (num >= sit)
break;
if (i == dot_sit)
break;
Led_buf[i] = Ledcode[10];
num -= num/sit*sit;
}
for (;i>=1; i--, sit/=10)
{
Led_buf[i] = Ledcode[num/sit];
num -= num/sit*sit;
}
Led_buf[0] = Ledcode[num];
Led_buf[dot_sit] &= 0x7F;
if (s_flag)
Led_buf[5] = Ledcode[11];
else
Led_buf[5] = Ledcode[10];
ET0 = 1;
}
/*
*數(shù)碼管顯示
*/
void show_num(uint8 *buf)
{
ENLED = 1;
switch(Led_n)
{
case 0:
ADDR0 = 0;
ADDR1 = 0;
ADDR2 = 0;
P0 = buf[0];
break;
case 1:
ADDR0 = 1;
ADDR1 = 0;
ADDR2 = 0;
P0 = buf[1];
break;
case 2:
ADDR0 = 0;
ADDR1 = 1;
ADDR2 = 0;
P0 = buf[2];
break;
case 3:
ADDR0 = 1;
ADDR1 = 1;
ADDR2 = 0;
P0 = buf[3];
break;
case 4:
ADDR0 = 0;
ADDR1 = 0;
ADDR2 = 1;
P0 = buf[4];
break;
case 5:
ADDR0 = 1;
ADDR1 = 0;
ADDR2 = 1;
P0 = buf[5];
break;
}
ENLED = 0;
if (Led_n >= 5)
Led_n = 0;
else
Led_n++;
}
/*
* 計(jì)算程序
*/
void calculate(int8 key_val)
{
float ans;
bit err=0;
if ((key_val >= FADD) && (key_val <= FDIV))
{
C_flag = key_val;
}
else if (key_val == FEQU)
{
switch(C_flag)
{
case FADD: ans = Tmp1+Tmp2; break;
case FSUB: ans = Tmp1-Tmp2; break;
case FMUL: ans = Tmp1*Tmp2; break;
case FDIV: if (Tmp2 == 0)
{
err = 1;
break;
}
else
{
ans = Tmp1/Tmp2;
break;
}
}
if (check_num(ans))
err = 1;
if (err)
make_led_error();
else
make_led_fnumber(ans);
C_flag = 0;
Tmp1 = 0;
Tmp2 = 0;
}
else if (key_val == FRES)
{
make_led_fnumber(0);
C_flag = 0;
Tmp1 = 0;
Tmp2 = 0;
}
else
{
if (C_flag == 0)
{
ans = Tmp1;
ans *= 10;
ans += key_val;
if (check_num(ans))
return;
else
Tmp1 = ans;
make_led_inumber(Tmp1);
}
else
{
ans = Tmp2;
ans *= 10;
ans += key_val;
if (check_num(ans))
return;
else
Tmp2 = ans;
make_led_inumber(Tmp2);
}
}
}

main()
{
int8 key_val;
ADDR3 = 1;
ENLED = 0;
make_led_inumber(0);
TMOD = 0x01;
TH0 = 0xF8;
TL0 = 0xCC;
TR0 = 1;
ET0 = 1;
EA = 1;
while (1)
{
key_val = scan_key();
if (key_val == -1)
continue;
calculate(key_val);
}
}

void time0() interrupt 1
{
TR0 = 0;
TH0 = 0xF8;
TL0 = 0xCC;
show_num(Led_buf);
TR0 = 1;
}


上一頁 1 2 下一頁

評論


技術(shù)專區(qū)

關(guān)閉