标题: protues单片机数码管显示DS18B20,数据一直是00,会不会是延时时间出错 [打印本页]

作者: captaine    时间: 2022-12-4 10:31
标题: protues单片机数码管显示DS18B20,数据一直是00,会不会是延时时间出错
单片机源程序如下:
#include <REGX51.H>
#include "intrins.h"

sbit DQ = P3^0;
uchar i;

int tem;
unsigned char NixieTable[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};


void Nixie(unsigned char location,unsigned char number)
{
        P2 = 0x00;
                switch(location)
        {
                case 1:P2_0 = 1; P2_1 = 0; P2_2 = 0; P2_3 = 0; break;
                case 2:P2_0 = 0; P2_1 = 1; P2_2 = 0; P2_3 = 0; break;
                case 3:P2_0 = 0; P2_1 = 0; P2_2 = 1; P2_3 = 0; break;
                case 4:P2_0 = 0; P2_1 = 0; P2_2 = 0; P2_3 = 1; break;
        }
        P0 = NixieTable[number];
        Delay1ms(10);
}


int        DS18B20_Init(void)
{
        uint Ack;
        DQ = 1;
        Delay1us(10);
        
        DQ = 0;
        Delay1us(480);
        DQ = 1;
        Delay1us(40);
        
        Ack = DQ;
        return Ack;
}

void SendByte(uchar Byte)
{
        DQ=1;
        Delay1us(1);
        for(i=0;i<8;i++)
        {
                DQ=0;
                DQ=Byte&0x01;
                Delay1us(5);
                DQ=1;
                Byte>>=1;
        }
}

uchar RcvByte(void)
{
        uchar Byte;

        DQ=1;
        Delay1us(1);
        for (i=8;i>0;i--)
        {
                DQ=0;
                Byte>>=1;
                DQ=1;
                Delay1us(1);
                if(DQ)
                Byte|=0x80;
                Delay1us(30);
                DQ=1;

        }
        return Byte;
}
float Get_temp(void)
{
        uchar TL, TH;
        int temp;                float T;
        DS18B20_Init();
        SendByte(0xcc);
        SendByte(0x44);
        Delay1us(5);
                DS18B20_Init();
                SendByte(0xcc);
                SendByte(0x44);
                SendByte(0xBE);
        TL = RcvByte();
        TH = RcvByte();
        temp = (TH << 8) | TL;
        T = temp * 0.0625;
        return T;
        
}


void main()
{
        
        while(1)
        {               
                tem = (int)Get_temp();
                Nixie(1,tem/10);
                Nixie(2,tem%10);
        }
        
}


屏幕截图 2022-12-04 102858.png (53.82 KB, 下载次数: 55)

屏幕截图 2022-12-04 102858.png

作者: wc86110    时间: 2022-12-4 16:03
撇开程序不说单单温度探头复位电路都有点问题
作者: captaine    时间: 2022-12-4 18:48
已经改了,温度 是需要把那个电阻删除吗
作者: man1234567    时间: 2022-12-5 14:55
会不会是延时时间出错 ? 试试就知道了哈




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1