找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1391|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

使用stc8h單片機ds18b20傳感器通過串口顯示溫度沒報錯但是沒有讀取到溫度求個解答

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:784077 發(fā)表于 2023-5-4 09:50 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include <stdio.h>
#include <intrins.h>
#include <stc8h.h>
#define FOSC        11059200UL
#define BRT         (65536 - FOSC / 9600 / 4)

#define uchar unsigned char
#define uint unsigned int


sbit DQ=P0^1;
sbit LED=P2^3;

void Delay10us()
{
        unsigned char i;

        i = 35;
        while (--i);
}

void Delay20us()
{
        unsigned char i;

        _nop_();
        _nop_();
        i = 71;
        while (--i);
}

void Delay30us()
{
        unsigned char i;

        _nop_();
        _nop_();
        i = 108;
        while (--i);
}

void Delay100us()
{
        unsigned char i, j;

        i = 2;
        j = 109;
        do
        {
                while (--j);
        } while (--i);
}

void Delay200us()
{
        unsigned char i, j;

        i = 3;
        j = 221;
        do
        {
                while (--j);
        } while (--i);
}

void Delay500us()
{
        unsigned char i, j;

        _nop_();
        _nop_();
        i = 8;
        j = 43;
        do
        {
                while (--j);
        } while (--i);
}


void DS18B20_init()
{
        DQ = 1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        DQ = 0;
        Delay500us();

        DQ = 1;
        Delay30us();

        Delay200us();
        DQ = 1;
}

uchar Read_One_Byte()
{
        uchar i;
        uchar byte = 0;
        for(i = 0;i < 8;i++)
        {
                DQ = 1;
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                DQ = 0;
                byte >>= 1;
                Delay20us();
                DQ = 1;
                Delay10us();
                if(DQ)
                {        byte |= 0x80;}
                Delay100us();
                DQ = 1;
        }
        return(byte);
}

void Write_One_Byte(uchar byte)
{
        uchar i = 0;
        for(i = 0;i < 8;i++)
        {
                DQ = 1;
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                DQ = 0;
                DQ = byte & 0x01;
                Delay30us();
                DQ=1;
                byte >>= 1;
        }
}
int Read_Temp()
{
        int t;
        float tep;
        uchar a,b;
        DS18B20_init();
        Write_One_Byte(0xcc);
        Write_One_Byte(0x44);
        DS18B20_init();
        Write_One_Byte(0xcc);
        Write_One_Byte(0xbe);
        a = Read_One_Byte();
        b = Read_One_Byte();
        t = b;
        t <<= 8;
        t = t|a;
        tep = t*0.0625;
        t = tep*10 + 0.5;
        return(t);
}

void UartInit(void)
{
        SCON = 0x50;
        AUXR |= 0x40;
        AUXR &= 0xFE;
        TMOD &= 0x0F;
        TL1 = 0xE0;
        TH1 = 0xFE;
        ET1 = 0;
        TR1 = 1;
}

void SendByte(unsigned char dat)
{
        SBUF=dat;
        while(!TI);
        TI=0;
}
void UART1_int (void) interrupt UART1_VECTOR
{
    if(RI)
    {
        RI = 0;
    }

    if(TI)
    {
    }
}
char putchar(char c)
{
        SendByte(c);
        return(c);
}

void main()
{
        int temp;
        UartInit();
        P0M0 = 0x00;
        P0M1 = 0x00;
  P2M0 = 0x00;
        P2M1 = 0x00;
        while(1)
        {
                LED=0;
                Read_Temp();
                temp=Read_Temp();
                printf("%d\n\r",temp);


        }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:1034262 發(fā)表于 2023-5-4 12:07 | 只看該作者
不管什么單片機,按時序來就能正常,各種時間值按典型值。示波器看一下你的時序就知道了。
回復(fù)

使用道具 舉報

板凳
ID:1047954 發(fā)表于 2023-5-4 13:04 | 只看該作者
STC8H8K64U實驗箱V9.6版的演示程序包有 DS18B20的完整演示程序
回復(fù)

使用道具 舉報

地板
ID:584814 發(fā)表于 2023-5-5 12:10 | 只看該作者
用廠家的例程就可以解決,當然在本壇內(nèi)找合適的也可以的 。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表