立即注冊 登錄
返回首頁

uid:261715的個人空間

日志

2017-12-18

已有 503 次閱讀2017-12-18 15:22

;單片機DS18B20溫度計C語言程序 (2008-09-27 17:01:06)

;標簽:ds18b20c語言程序   

 

  #include<reg51.h>

  #include<intrins.h>

  #include <math.H>  //要用到取絕對值函數(shù)abs()

//通過DS18B20測試當(dāng)前環(huán)境溫度, 并通過數(shù)碼管顯示當(dāng)前溫度值, 目前顯示范圍: -55~ +125

  sbit wela = P2^7;  //數(shù)碼管位選

  sbit dula = P2^6;  //數(shù)碼管段選

  sbit ds = P2^2;

  int tempValue;

 

 //0-F數(shù)碼管的編碼(共陽極)

 unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,

    0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

 //0-9數(shù)碼管的編碼(共陽極), 帶小數(shù)點

 unsigned char code tableWidthDot[]={0x40, 0x79, 0x24, 0x30,

 

0x19, 0x12, 0x02,0x78, 0x00, 0x10};

 //延時函數(shù), 對于11.0592MHz時鐘, i=10,則大概延時10ms.

 void delay(unsigned int i)

 {

  unsigned int j;

   while(i--)

    {

        for(j = 0; j < 125; j++);

    }

 }

 

//初始化DS18B20

 //DS18B20一段相對長時間低電平, 然后一段相對非常短時間高電平, 即可啟動

 void dsInit()

 {

   //對于11.0592MHz時鐘, unsigned int型的i, 作一個i++操作的時間大于?us

   unsigned int i;

   ds = 0;

    i = 100;   //拉低約800us, 符合協(xié)議要求的480us以上

    while(i>0) i--;

    ds = 1;    //產(chǎn)生一個上升沿, 進入等待應(yīng)答狀態(tài)

     i = 4;

   while(i>0) i--;

 }

 

 void dsWait()

 {

     unsigned int i;

     while(ds);

    while(~ds);  //檢測到應(yīng)答脈沖

    i = 4;

    while(i > 0) i--;

}

 

//DS18B20讀取一位數(shù)據(jù)

//讀一位, DS18B20一小周期低電平, 然后兩小周期高電平,

//之后DS18B20則會輸出持續(xù)一段時間的一位數(shù)據(jù)

bit readBit()

{

    unsigned int i;

   bit b;

   ds = 0;

  i++;   //延時約8us, 符合協(xié)議要求至少保持1us

  ds = 1;

  i++; i++;  //延時約16us, 符合協(xié)議要求的至少延時15us以上

   b = ds;

    i = 8;

    while(i>0) i--;  //延時約64us, 符合讀時隙不低于60us要求

   return b;

}

 

//讀取一字節(jié)數(shù)據(jù), 通過調(diào)用readBit()來實現(xiàn)

unsigned char readByte()

{

   unsigned int i;

     unsigned char j, dat;

    dat = 0;

   for(i=0; i<8; i++)

   {

        j = readBit();

       //最先讀出的是最低位數(shù)據(jù)

       dat = (j << 7) | (dat >> 1);

    }

    return dat;

}

 

//DS18B20寫入一字節(jié)數(shù)據(jù)

void writeByte(unsigned char dat)

{

 unsigned int i;

   unsigned char j;

    bit b;

  for(j = 0; j < 8; j++)

   {

       b = dat & 0x01;

       dat >>= 1;

       //"1", DQ拉低15us, 15us~60us內(nèi)將DQ拉高, 即完成寫1

        if(b)

       {

            ds = 0;

           i++; i++;  //拉低約16us, 符號要求15~60us內(nèi)

            ds = 1;  

           i = 8; while(i>0) i--;  //延時約64us, 符合寫時隙不低于60us要求

       }

      else  //"0", DQ拉低60us~120us

           ds = 0;

            i = 8; while(i>0) i--;  //拉低約64us, 符號要求

            ds = 1;

           i++; i++;  //整個寫0時隙過程已經(jīng)超過60us, 這里就不用像寫1那樣, 再延時64us

     

    }

}

 

//DS18B20發(fā)送溫度轉(zhuǎn)換命令

void sendChangeCmd()

{

   dsInit();    //初始化DS18B20, 無論什么命令, 首先都要發(fā)起初始化

   dsWait();   //等待DS18B20應(yīng)答

   delay(1);    //延時1ms, 因為DS18B20會拉低DQ 60~240us作為應(yīng)答信號

    writeByte(0xcc); //寫入跳過序列號命令字 Skip Rom

   writeByte(0x44); //寫入溫度轉(zhuǎn)換命令字 Convert T

}

 

//DS18B20發(fā)送讀取數(shù)據(jù)命令

void sendReadCmd()

{

   dsInit();

   dsWait();

   delay(1);

   writeByte(0xcc); //寫入跳過序列號命令字 Skip Rom

   writeByte(0xbe); //寫入讀取數(shù)據(jù)令字 Read Scratchpad

}

 

//獲取當(dāng)前溫度值

int getTmpValue()

{

    unsigned int tmpvalue;

   int value; //存放溫度數(shù)值

  float t;

    unsigned char low, high;

   sendReadCmd();

    //連續(xù)讀取兩個字節(jié)數(shù)據(jù)

  low = readByte();

  high = readByte();

    //將高低兩個字節(jié)合成一個整形變量

    //計算機中對于負數(shù)是利用補碼來表示的

    //若是負值, 讀取出來的數(shù)值是用補碼表示的, 可直接賦值給int型的

 

value

    tmpvalue = high;

    tmpvalue <<= 8;

    tmpvalue |= low;

   value = tmpvalue;

 

    //使用DS18B20的默認分辨率12, 精確度為0.0625, 即讀回數(shù)據(jù)的最低位代表0.0625

   t = value * 0.0625;

    //將它放大100, 使顯示時可顯示小數(shù)點后兩位, 并對小數(shù)點后第三進行45

   //t=11.0625, 進行計數(shù)后, 得到value = 1106, 11.06

   //t=-11.0625, 進行計數(shù)后, 得到value = -1106, -11.06

    value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于00.5, 小于00.5

   return value;

}

 

unsigned char const timeCount = 3; //動態(tài)掃描的時間間隔

//顯示當(dāng)前溫度值, 精確到小數(shù)點后一位

//若先位選再段選, 由于IO口默認輸出高電平, 所以當(dāng)先位選會使數(shù)碼管出現(xiàn)亂碼

void display(int v)

{

    unsigned char count;

   unsigned char datas[] = {0, 0, 0, 0, 0};

   unsigned int tmp = abs(v);

    datas[0] = tmp / 10000;

   datas[1] = tmp % 10000 / 1000;

   datas[2] = tmp % 1000 / 100;

    datas[3] = tmp % 100 / 10;

    datas[4] = tmp % 10;

   if(v < 0)

    {

       //關(guān)位選, 去除對上一位的影響

       P0 = 0xff;

       wela = 1; //打開鎖存, 給它一個下降沿量

       wela = 0;

       //段選

      P0 = 0x40; //顯示"-"

       dula = 1;  //打開鎖存, 給它一個下降沿量

      dula = 0;

 

       //位選

      P0 = 0xfe;

       wela = 1; //打開鎖存, 給它一個下降沿量

        wela = 0;

      delay(timeCount);

    }

    for(count = 0; count != 5; count++)

    {

        //關(guān)位選, 去除對上一位的影響

       P0 = 0xff;

      wela = 1; //打開鎖存, 給它一個下降沿量

       wela = 0;

        //段選

        if(count != 2)

      {

     

               P0 = table[datas[count]];  //顯示數(shù)字

        }

       else

        {

            P0 = tableWidthDot[datas[count]]; //顯示帶小數(shù)點數(shù)字

       }

        dula = 1;  //打開鎖存, 給它一個下降沿量

        dula = 0;

 

        //位選

        P0 = _crol_(0xfd, count); //選擇第(count + 1) 個數(shù)碼管

       wela = 1; //打開鎖存, 給它一個下降沿量

       wela = 0;

       delay(timeCount);

    }

}

 

void main()

{

  unsigned char i;

 

   while(1)

    {

        //啟動溫度轉(zhuǎn)換

       sendChangeCmd();

       //顯示5

        for(i = 0; i < 40; i++)

        {

           display(tempValue);

     }

        tempValue = getTmpValue();

    }


路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

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

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

返回頂部