单片机 > 单片机程序设计 > 详情

DS18B20测温显示程序

发布时间:2020-06-19 发布时间:
|
#include

#include
sbit TSOR=P2^0;
#define Busy 0x80;
#define uchar unsigned char
#define uint unsigned int
#define reset() {WDT=0x1e;WDT=0xe1;}
sfr WDT=0xa6;
bit ON_OFF;
bit outflag;
sbit en =P1^7;    /*DTLED-6显示芯片使能端*/ 
sbit dout=P1^6;    /*DTLED-6显示芯片数据读入端*/
sbit clk=P1^3;    /*DTLED-6显示芯片/时钟芯片DS1302时钟输入端*/
//============================================
uchar dispbuf[9];    /*显示值存放阵列*/
uchar temp1,temp2;
uchar cnt;
char set,T;
//===========================================
void gettempTS(void);
void initTS(void);
uchar readBTS(void);
void writeBTS(uchar byte);
void resetTS(void);
bit readTS(void);
void write1TS(void);
void write0TS(void);
void delay15us(void);
void delay60us(void);
//*************延时K×1ms,12.000MHz***********
void delay(uint k)
{
uint i,j;                          //定义i,j为无符号整形变量
for(i=0;i {
for(j=0;j<60;j++)
{;}
reset()}
}
//*******************************************
void delay15us(void)
{
uint i;                          //定义i为无符号整形变量
for(i=0;i<4;i++);
}
//***************************************
void delay60us(void)
{
uint i;                          //定义i为无符号整形变量
for(i=0;i<15;i++);
}
//**********************************************
void write0TS(void)
{
TSOR=1;TSOR=0;
delay15us();delay15us();delay15us();delay15us();
TSOR=1;
_nop_();_nop_();
}
//**************************************************
void write1TS(void)
{
TSOR=1;TSOR=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
TSOR=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
delay15us();delay15us();delay15us();
}
//********************************************
bit readTS(void)
{
bit b;
TSOR=1;
TSOR=0;
_nop_();_nop_();_nop_();_nop_();
TSOR=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
b=TSOR;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();
return b;
}
//***************************************************
void resetTS(void)
{uchar i;
TSOR=1;TSOR=0;
for(i=0;i<8;i++)delay60us();
TSOR=1;
while(TSOR);
for(i=0;i<8;i++)delay60us();
}
//***********************************************
void writeBTS(uchar byte)
{uchar i;
for(i=0;i<8;i++)
{
if(byte&0x01)
write1TS();
else
write0TS();
byte=byte>>1;
}
}
//**********************************************
uchar readBTS(void)
{uchar i,j;
bit b;
j=0;
for(i=0;i<8;i++)
{
b=readTS();
if(b) j+=1;
j=_cror_(j,1);
}
return j;
}
//*****************************************8
void initTS(void)
{
resetTS();
writeBTS(0xcc);
writeBTS(0x4e);
writeBTS(0x64);
writeBTS(0x8a);
writeBTS(0x1f);
}
//******************************************
void gettempTS(void)
{
resetTS();
writeBTS(0xcc);
writeBTS(0x44);
delay(200);
resetTS();
writeBTS(0xcc);
writeBTS(0xbe);
temp2=readBTS();
temp1=readBTS();
readBTS();readBTS();readBTS();readBTS();
readBTS();readBTS();readBTS();
temp1=temp1<<4;
temp1+=(temp2&0xf0)>>4;
temp2=(temp2&0x0f)?5:0;
}
//********************************************
void delayus (uint value)  /*延时副程式*/
 {
      while (value!=0) value--;  /*10US延时*/
 }

/*********************************************************************/

//串行发送DTLED-6显示
//dispbuf0-dispbuf1-dispbuf2-dispbuf3-dispbuf4-dispbuf5-dispbuf6-dispbuf7-dispbuf8
// 小数点 - 数码管1- 数码管2- 数码管3- 数码管4- 数码管5- 数码管6-ID识别码-BZ识别码
//00000xxx-0000xxxx-0000xxxx-0000xxxx-0000xxxx-0000xxxx-0000xxxx-0000xxxx-0000xxxx
//小数点=1~6,其它为不显示             
//数码管1-6=0~15,
//ID识别码=0~15   
//BZ识别码=BZ.3为蜂鸣输出控制位,0=关闭,1=打开
//     BZ0~2为蜂鸣频率输出设定为,000=不分频,  最高频率输出=4800Hz;
//             001=一次分频,频率输出=4800/ 2 =2400Hz;
//             010=二次分频,频率输出=4800/ 4 =1200Hz;
//          011=三次分频,频率输出=4800/ 8 =600Hz;
//             100=四次分频,频率输出=4800/ 16=300Hz;
//             101=五次分频,频率输出=4800/ 32=150Hz;
//             110=六次分频,频率输出=4800/ 64=75Hz;    
//             111=七次分频,频率输出=4800/128=38Hz;      
//======================================================================
void tsled(void)   /*串行发送DTLED-6显示副程式*/ 
{   char ts0,i,j;
 clk=0;
 en=0;
 delayus(10);
 for(j=0;j<9;j++)
   { ts0=dispbuf[j];
  for(i=0;i<4;i++)  /*发小数点码8,4,2,1*/
  {
   clk=1;
  ts0=ts0<<1; 
  if(AC==1) 
  dout=1;
  else      dout=0;
  clk=0;
  _nop_();
  delayus(3);
  }
   }  
 en=1;
}  /*返回主程式*/

void main(void)
{
 uchar T=28;
 initTS();
 reset()
 dispbuf[7]=0x01; /*ID号码*/
 dispbuf[8]=0x00; /*蜂鸣器关闭*/
 while(1)
 {
 gettempTS();
 dispbuf[0]=2;         //关闭小数点位显示//数据显示
    dispbuf[1]=(temp2/10+0x30);
 dispbuf[2]=((temp1-2)%10+0x30);
 dispbuf[3]=((temp1-2)/10+0x30); 
 dispbuf[4]=0;
 dispbuf[5]=(T%10+0x30);
 dispbuf[6]=(T/10+0x30);
    tsled();
    delayus(50000);  
  }
 }

关键字:DS18B20测温  显示程序 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机按键切换下一首歌