七段数码管显示

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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值