欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: 使用stc8h單片機ds18b20傳感器通過串口顯示溫度沒報錯但是沒有讀取到溫度求個解答 [打印本頁]

作者: ll20012    時間: 2023-5-4 09:50
標題: 使用stc8h單片機ds18b20傳感器通過串口顯示溫度沒報錯但是沒有讀取到溫度求個解答
#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);


        }
}



作者: coody_sz    時間: 2023-5-4 12:07
不管什么單片機,按時序來就能正常,各種時間值按典型值。示波器看一下你的時序就知道了。
作者: 余音裊裊    時間: 2023-5-4 13:04
STC8H8K64U實驗箱V9.6版的演示程序包有 DS18B20的完整演示程序
作者: man1234567    時間: 2023-5-5 12:10
用廠家的例程就可以解決,當然在本壇內找合適的也可以的 。




歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1