找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4731|回復(fù): 8
收起左側(cè)

初學(xué)者想做一個(gè)溫度傳感器的程序,但是現(xiàn)在數(shù)碼管根本不顯示,下邊是代碼,求大神幫忙

[復(fù)制鏈接]
ID:287946 發(fā)表于 2018-3-5 21:24 | 顯示全部樓層 |閱讀模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2;
uint temp;
float f_temp;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,           //有小數(shù)點(diǎn)
                                                        0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};           //無小數(shù)點(diǎn)        (1:0x86  1000 0110  第一位為小數(shù)點(diǎn))
uchar dispuf[]={10,10,10,10,10,10,10,10};
uchar discom=0;
void display(void);
void delay(uchar i)          //延時(shí)
{       
        i++;
}
bit inclear(void)                  //初始化
{        uchar dat=1;
        ds=1;
        delay(1);
        ds=0;
        delay(250);
        ds=1;
        dat=ds;
        delay(100);  
        ds=1;
       
        return(dat) ;                  
}
uchar read()                         //讀數(shù)據(jù)
{
        uchar i,j,dat;
        dat=0;
        for(i=0;i<8;i++)
        {
                ds=1;
                delay(1);
                ds=0;
                delay(3);
                ds=1;
                delay(2);
                dat=(j<<7|dat>>1);
                if(ds)
                        ds=(ds|0x80);
                delay(10);       
        }                                
        return(dat);
}
void write(uchar dat)                     //寫數(shù)據(jù)
{
        uchar i,t;
       
        for(i=0;i<8;i++)
        {
                ds=0;
                delay(2);
                dat=dat&0x01;
                ds=1;
            dat=dat>>1;
                delay(10);
                ds=1;               
        }
}
void tempchange()
{
                                   //獲取溫度并轉(zhuǎn)換
        inclear();
        delay(1);
        write(0xcc);
        write(0x44);
}
uint temp_get()
{
        uchar a,b;
        inclear();                                 //讀取數(shù)據(jù)
        delay(1);
        write(0xcc);
        write(0xbe);                          //讀
    a=read();                                  //低八位
        b=read();                                  //b=高八位
        temp=b;
        temp<<=8;
        temp=temp|a;                          //合并a和b
        f_temp=temp*0.0625;
        temp=f_temp*10+0.5;
        return temp;
}
void display(void)
{
        P2 = ((P2&0x1f)|0xe0);
        P0 = 0xff;
        P2 &= 0x1f;

        P2=((P2&0x1f)|0xc0);
        P0 = (1<<discom);
        P2=P2&0x1f;

        P2=((P2&0x1f)|0xe0);
        P0=~table[dispuf[discom]];
        P2=P2&0x1f;
        delay(2);

        if(++discom==8)
        discom=0;
}
void mian()
{
        uchar i;
        while(1)
        {
                tempchange();
                for(i=10;i>0;i--)
                {
                        display();
                        dispuf[5]=temp/100;
                        dispuf[6]=temp%100/10;
                        dispuf[7]=temp%100%10;
                }
        }
}


回復(fù)

使用道具 舉報(bào)

ID:212177 發(fā)表于 2018-3-5 22:30 來自觸屏版 | 顯示全部樓層
18b20一般都是延時(shí)問題導(dǎo)致度數(shù)有問題。自己檢查一下

評分

參與人數(shù) 2黑幣 +10 收起 理由
那人燈火闌珊處 + 5 我都看不懂
Yee.L + 5 贊一個(gè)!

查看全部評分

回復(fù)

使用道具 舉報(bào)

ID:287946 發(fā)表于 2018-3-6 10:27 | 顯示全部樓層
l6931639 發(fā)表于 2018-3-5 22:30
18b20一般都是延時(shí)問題導(dǎo)致度數(shù)有問題。自己檢查一下

我不知道我的延時(shí)函數(shù)是不是有問題,但是現(xiàn)在燒錄后數(shù)碼管不顯示任何數(shù)據(jù),就是沒反應(yīng)。
回復(fù)

使用道具 舉報(bào)

ID:288099 發(fā)表于 2018-3-6 10:56 | 顯示全部樓層
temp這個(gè)變量定義成什么了
回復(fù)

使用道具 舉報(bào)

ID:288108 發(fā)表于 2018-3-6 11:20 | 顯示全部樓層
你說的是顯示問題,個(gè)人建議調(diào)顯示可以幾個(gè)固定的顯示數(shù)據(jù),要先把顯示調(diào)試通過,再去弄18B20的讀寫,
另外數(shù)碼管的顯示和你的掃描頻率有關(guān)。

評分

參與人數(shù) 1黑幣 +5 收起 理由
Yee.L + 5 贊一個(gè)!

查看全部評分

回復(fù)

使用道具 舉報(bào)

ID:287946 發(fā)表于 2018-3-6 16:40 | 顯示全部樓層
peng1118 發(fā)表于 2018-3-6 11:20
你說的是顯示問題,個(gè)人建議調(diào)顯示可以幾個(gè)固定的顯示數(shù)據(jù),要先把顯示調(diào)試通過,再去弄18B20的讀寫,
另 ...

固定的值是可以顯示出來的 ,但是整體運(yùn)行又不行了
回復(fù)

使用道具 舉報(bào)

ID:287946 發(fā)表于 2018-3-6 16:43 | 顯示全部樓層
zhugesimida 發(fā)表于 2018-3-6 10:56
temp這個(gè)變量定義成什么了

轉(zhuǎn)換的溫度
回復(fù)

使用道具 舉報(bào)

ID:287946 發(fā)表于 2018-3-7 18:31 | 顯示全部樓層
Yee.L 發(fā)表于 2018-3-6 10:27
我不知道我的延時(shí)函數(shù)是不是有問題,但是現(xiàn)在燒錄后數(shù)碼管不顯示任何數(shù)據(jù),就是沒反應(yīng)。

已解決,是延時(shí)的問題,感謝
回復(fù)

使用道具 舉報(bào)

ID:288892 發(fā)表于 2018-3-7 20:35 | 顯示全部樓層
怎么解決的??
回復(fù)

使用道具 舉報(bào)

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

本版積分規(guī)則

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

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

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