为什么我的数码管显示小数,有时候最后一位会减少“1”,例如 想输出10.15,但4位数码管显示的是10.14
代码如下
#include <STC15F2K60S2.H>
#include <delay.h>
#include <usart.h>
unsigned char Point_Pos;
code unsigned char tab[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
unsigned char Bits[]={0,0,0,0};
void display()
{
int i;
int j=Point_Pos;
unsigned char pos=0x7f;
unsigned char k=0xc0;
P2=pos;
for(i=0;i<4;i++)
{
P0=tab[Bits[i]];
if(i==(j-1)){P0=P0+0x80;}
delayxms(1);
P0=0xff;
P2=P2^k;
k=k>>1;
}
}
void judge_num(double num)
{
unsigned long int int_part;
double dec_part;
int_part=(unsigned long int)num;
dec_part=num-int_part;
if(int_part<=9)
{
Bits[0] = int_part;
Bits[1] = (unsigned long int)(dec_part * 10);
Bits[2] = (unsigned long int)(dec_part * 100 - Bits[1] * 10);
Bits[3] = (unsigned long int)(dec_part * 1000 - Bits[1] * 100 - Bits[2] * 10);
Point_Pos = 1;
}
else if (int_part >= 10 && int_part <= 99)
{
Bits[0] = int_part / 10;
Bits[1] = int_part - Bits[0] * 10;
Bits[2] = (unsigned long int)(dec_part * 10);
Bits[3] = (unsigned long int)(dec_part * 100 - Bits[2] * 10);
Point_Pos = 2;
}
else if (int_part >= 100 && int_part <= 999)
{
Bits[0] = int_part / 100;
Bits[1] = (int_part - Bits[0] * 100) / 10;
Bits[2] = int_part - Bits[0] * 100 - Bits[1] * 10;
Bits[3] = (unsigned long int)(dec_part * 10);
Point_Pos = 3;
}
else if (int_part >= 1000 && int_part <= 9999)
{
Bits[0] = int_part / 1000;
Bits[1] = (int_part - Bits[0] * 1000) / 100;
Bits[2] = (int_part - Bits[0] * 1000 - Bits[1] * 100) / 10;
Bits[3] = int_part % 10;
Point_Pos = 0;
}
}