找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

單片機DS18B20溫度傳感器串口溫度顯示源代碼 VC++6.0

[復(fù)制鏈接]
ID:1001155 發(fā)表于 2024-6-29 07:50 | 顯示全部樓層 |閱讀模式
使用說明:
系統(tǒng)要求:WIN9X/ME/NT/2000 VC++6.0  且安裝了VC ACTIVEX控件(在VC6安裝時選上)
簡介:在VC++6.0中用MSComm控件編程,可以實現(xiàn)串口接收數(shù)據(jù)和發(fā)送數(shù)據(jù),數(shù)據(jù)分別顯示在接收框和發(fā)送框中。
如何建立工程:建立新文件夾,將文檔用WINZIP解壓后,雙擊 Scommtest.dsw 即可在VC6.0中打開工程文件。作者主頁上有詳細編程說明,每一步都列出來了,可以參考。

51hei.png 51hei.png

//安裝目錄下的EXE文件打開后可在電腦上顯示當(dāng)前溫度值
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;           //define interface
uint temp;             // variable of temperature
uchar flag1;            // sign of the result positive or negative
sbit dula=P2^6;
sbit wela=P2^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
                        0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
                        0x87,0xff,0xef};

void delay(uint count)      //delay
{
  uint i;
  while(count)
  {
    i=200;
    while(i>0)
    i--;
    count--;
  }
}
///////功能:串口初始化,波特率9600,方式1///////
void Init_Com(void)
{
     TMOD = 0x20;
     PCON = 0x00;
     SCON = 0x50;
     TH1 = 0xFd;
     TL1 = 0xFd;
     TR1 = 1;
}

void dsreset(void)       //send reset and initialization command
{
  uint i;
  DS=0;
  i=103;
  while(i>0)i--;
  DS=1;
  i=4;
  while(i>0)i--;
}

bit tmpreadbit(void)       //read a bit
{
   uint i;
   bit dat;
   DS=0;i++;          //i++ for delay
   DS=1;i++;i++;
   dat=DS;
   i=8;while(i>0)i--;
   return (dat);
}

uchar tmpread(void)   //read a byte date
{
  uchar i,j,dat;
  dat=0;
  for(i=1;i<=8;i++)
  {
    j=tmpreadbit();
    dat=(j<<7)|(dat>>1);   //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個字節(jié)在DAT里
  }
  return(dat);
}

void tmpwritebyte(uchar dat)   //write a byte to ds18b20
{
  uint i;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++)
  {
    testb=dat&0x01;
    dat=dat>>1;
    if(testb)     //write 1
    {
      DS=0;
      i++;i++;
      DS=1;
      i=8;while(i>0)i--;
    }
    else
    {
      DS=0;       //write 0
      i=8;while(i>0)i--;
      DS=1;
      i++;i++;
    }

  }
}

void tmpchange(void)  //DS18B20 begin change
{
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);  // address all drivers on bus
  tmpwritebyte(0x44);  //  initiates a single temperature conversion
}

uint tmp()               //get the temperature
{
  float tt;
  uchar a,b;
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);
  tmpwritebyte(0xbe);
  a=tmpread();
  b=tmpread();
  temp=b;
  temp<<=8;             //two byte  compose a int variable
  temp=temp|a;
  tt=temp*0.0625;
  temp=tt*10+0.5;
  return temp;
}

void readrom()          //read the serial
{
  uchar sn1,sn2;
  dsreset();
  delay(1);
  tmpwritebyte(0x33);
  sn1=tmpread();
  sn2=tmpread();
}


void delay10ms()            //delay
  {
    uchar a,b;
    for(a=10;a>0;a--)
      for(b=60;b>0;b--);
   }

void display(uint temp)            //顯示程序
{
   uchar A1,A2,A2t,A3,ser;
   ser=temp/10;
   SBUF=ser;
   A1=temp/100;
   A2t=temp%100;
   A2=A2t/10;
   A3=A2t%10;


   P0=0xff;
   wela=1;
   wela=0;
   P0=table[A1];        //顯示百位
   dula=1;
   dula=0;


   P0=0x7e;
   wela=1;
   delay(1);
   wela=0;
   delay(1);

   P0=0xff;
   wela=1;
   wela=0;
   P0=table1[A2];        //顯示十位
   dula=1;
   dula=0;


   P0=0x7d;
   wela=1;
   wela=0;
   delay(1);


   P0=0xff;
   wela=1;
   wela=0;
   P0=table[A3];        //顯示個位
   dula=1;
   dula=0;

   P0=0x7b;
   wela=1;
   wela=0;
   delay(1);
}


void main()
{
     uchar a;
     Init_Com();
      do
      {
    tmpchange();
   // delay(200);
    for(a=10;a>0;a--)
        {   
        display(tmp());
        }
      }               
    while(1);
}

vc++6.0源碼和單片機代碼下載: 8.DS18B20溫度傳感器.7z (6.17 MB, 下載次數(shù): 7)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報

ID:1126459 發(fā)表于 2024-7-1 12:11 | 顯示全部樓層
可以分享下仿真或者原理圖嗎?
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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