找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2020|回复: 3
收起左侧

大佬看一下呗 (单片机温控系统,数码管显示温度)为啥数码管总是闪烁 怎么改啊

[复制链接]
ID:711880 发表于 2020-3-22 17:31 | 显示全部楼层 |阅读模式
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
char code xuanwei[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P2^5;
sbit DSPORT=P3^7;
sbit moto=P1^0;
int q,w;
float Y,M,Z;
bit fg=1; //温度正负标志
unsigned int sdata;//测量到的温度的整数部分
unsigned char xiaoshu1;//小数第一位

uchar charac[4];
void delay(uint xms)
{
        xms=xms-M;
        while(xms--);
        for(w=0;w<M;w++)
        moto=1;moto=0;
        if(q==5)
        {beep=~beep;q=0;}
}
void datapros(int temp)          //温度读取处理转换函数
{
           float tp;  
        if(temp< 0)                                //当温度值为负数
          {
                charac[0]=0x40;
                temp=temp-1;//因为读取的温度是实际温度的补码,所以减1,再取反求出原码
                temp=~temp;
                tp=temp;
                temp=tp*0.0625*10+0.5;       
                //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
                //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
                //算加上0.5,还是在小数点后面。
                Y=temp;
                if(Y*(-1)/10<-10)
                q=q+1;
}

        else
          {                       
                charac[0]=0x00;
                tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
                //如果温度是正的那么,那么正数的原码就是补码它本身
                temp=tp*0.0625*10+0.5;       
                //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
                //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
                //算加上0.5,还是在小数点后面。
                Y=temp;Z=temp;Z=Z/10;
                if(Y/10>30)
                q=q+1;
                if(Z>=10&&Z<=30)
                {M=(Z-10)*10;}
                }
                charac[1]=xuanwei[temp/100];       
                charac[2]=xuanwei[temp%100/10]+0x80;       
                charac[3]=xuanwei[temp%10];
}
void Delay1ms(uint y)
{
        uint x;
        for( ; y>0; y--)
        {
                for(x=110; x>0; x--);
        }
}
uchar Ds18b20Init() //初始化函数
{
        uchar i;
        DSPORT = 0;                         //将总线拉低480us~960us
        i = 80;       
        while(i--);//延时642us
        DSPORT = 1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
        i = 0;
        while(DSPORT)        //等待DS18B20拉低总线
        {
                Delay1ms(1);
                i++;
                if(i>5)//等待>5MS
                {
                        return 0;//初始化失败
                }
       
        }
        return 1;//初始化成功
}
void Ds18b20WriteByte(uchar dat)//写入函数
{
        uint i, j;
        for(j=0; j<8; j++)
        {
                DSPORT = 0;                       //每写入一位数据之前先把总线拉低1us
                i++;
                DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
                i=6;
                while(i--); //延时68us,持续时间最少60us
                DSPORT = 1;        //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
                dat >>= 1;
        }
}
uchar Ds18b20ReadByte()//读取函数
{
        uchar byte, bi;
        uint i, j;       
        for(j=8; j>0; j--)
        {
                DSPORT = 0;//先将总线拉低1us
                i++;
                DSPORT = 1;//然后释放总线
                i++;
                i++;//延时6us等待数据稳定
                bi = DSPORT;         //读取数据,从最低位开始读取
                //将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。
                byte = (byte >> 1) | (bi << 7);                                                  
                i = 4;                //读取完之后等待48us再接着读取下一个数
                while(i--);
        }                               
        return byte;
}
void  Ds18b20ChangTemp()//转换
{
        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);                //跳过ROM操作命令                 
        Ds18b20WriteByte(0x44);            //温度转换命令
        //Delay1ms(25);        //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
}

void  Ds18b20ReadTempCom()//读取温度
{       

        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);         //跳过ROM操作命令
        Ds18b20WriteByte(0xbe);         //发送读取温度命令
}

int Ds18b20ReadTemp()//ds18b20读取温度总函数
{
        uint temp=0;
        uchar tmh, tml;
        Ds18b20ChangTemp();                                 //先写入转换命令
        Ds18b20ReadTempCom();                        //然后等待转换完后发送读取温度命令
        tml = Ds18b20ReadByte();                //读取温度值共16位,先读低字节
        tmh = Ds18b20ReadByte();                //再读高字节
        temp = tmh;
        temp <<= 8;
        temp |= tml;
        sdata = tml/16+tmh*16; //整数部分
        return temp;
       
}
void DigDisplay()
{
                LSA=0;LSB=0;LSC=0; //显示第0位
                P0=charac[3];
                delay(1000); //间隔一段时间扫描       
                P0=0x00;//消隐
                LSA=1;LSB=0;LSC=0;//显示第1位
                P0=charac[2];
                delay(1000); //间隔一段时间扫描       
                P0=0x00;//消隐
                LSA=0;LSB=1;LSC=0; //显示第2位
                P0=charac[1];
                delay(1000); //间隔一段时间扫描       
                P0=0x00;//消隐
                LSA=1;LSB=1;LSC=0; //显示第3位
                P0=charac[0];//发送段码
                delay(1000); //间隔一段时间扫描       
                P0=0x00;//消隐
       
}
int te;
void main()//主函数
{
                q=0;w=0;
                while(1)
                {
                        beep=0;moto=0;
                        te=Ds18b20ReadTemp();
                        datapros(te);
                        DigDisplay();
       
               
       
                }
}         


回复

使用道具 举报

ID:452950 发表于 2020-3-22 21:36 | 显示全部楼层
本帖最后由 xxpp2011 于 2020-3-22 23:10 编辑

或许是延时1000ms有点长吧
回复

使用道具 举报

ID:687694 发表于 2020-3-22 23:45 | 显示全部楼层
顺序执行任务单次循环时间太长,导致数码管刷新频率下降,24Hz以下就出现闪烁。
回复

使用道具 举报

ID:698701 发表于 2020-3-23 22:19 | 显示全部楼层
数码管不稳定,你直接选用液晶的替代呗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表