seg7.h
#ifndef _SEG7_H_
#define _SEG7_H_
#include<reg51.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#define SEG7_PORT P0
sbit SEGA = P2^2;
sbit SEGB = P2^3;
sbit SEGC = P2^4;
extern uchar code SEG_DODE[17];
void SEGShow(float value, uchar precision);
#endif
seg7.c
#include<seg7.h>
#include<math.h>
uchar code SEG_DODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void SEGShow(float value, uchar precision)
{
uchar i,j;
uchar showFlag = 0;
uint temp;
uint wei[5];
uchar displayData[8] = 0;
value = value + pow(0.1,precision + 1);
for(i = 0; i < precision; i++)
{
value = value * 10;
}
temp = value;
wei[0] = temp/10000;
wei[1] = temp%10000/1000;
wei[2] = temp%1000/100;
wei[3] = temp%100/10;
wei[4] = temp%10;
displayData[0] = SEG_DODE[wei[0]];
displayData[1] = SEG_DODE[wei[1]];
displayData[2] = SEG_DODE[wei[2]];
displayData[3] = SEG_DODE[wei[3]];
displayData[4] = SEG_DODE[wei[4]];
for(i=0;i<5;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
SEGA=1;SEGB=1;SEGC=0; break;//显示第0位
case(1):
SEGA=0;SEGB=0;SEGC=1; break;//显示第1位
case(2):
SEGA=1;SEGB=0;SEGC=1; break;//显示第2位
case(3):
SEGA=0;SEGB=1;SEGC=1; break;//显示第3位
case(4):
SEGA=1;SEGB=1;SEGC=1; break;//显示第4位
}
if((showFlag == 0 && displayData[i] == 0x3f && i != 4 - precision) )
{
SEG7_PORT=0x00;
}else
{
if(i == 4 - precision)
SEG7_PORT=displayData[i] | 0x80;
else
SEG7_PORT=displayData[i];//发送段码
showFlag = 1;
}
j=100; //扫描间隔时间设定
while(j--);
SEG7_PORT=0x00;//消隐
//SEGA=0;SEGB=0;SEGC=0;
}
}