⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 温度电脑.c

📁 stc52+ds18b20 发送给电脑
💻 C
字号:
#include <AT89x52.h>
#include <intrins.h>

   
  sbit         DS18b20_DQ = P1^6 ;//定义管脚
 
  unsigned char *p;   

  unsigned char num;



/******定义函数****************/

# define uchar unsigned char
# define uint unsigned int





/////////////////////////////////////////////////
/********************延时程序*******************/
/////////////////////////////////////////////////

/*短延时程序16us为单位 延时时间=24+Num*16 微秒*/
void Delay_short(unsigned int n)
{
 unsigned int i;
 for(i=0;i<n;i++)	
 {}
}

/*长延时程序,以5ms为单位
void Delay_long(unsigned char N)
{ unsigned char i;
  unsigned int j;
   for(i=0;i<N;i++)
   { for(j=0;j<315;j++)  //一个循环16us,共5ms
      {}
   }
}*/
/////////////////////////////////////////
/***********DS18B20部分*****************/
/////////////////////////////////////////

/*DS18B20接口定义*/
sbit DQ  =P1^6;			  //单总线接口

/*全局变量定义*/
unsigned char DS_Temp[2];


/*DS18B20指令定义*/
#define  READ_ROM      0x33	   //读ROM指令
#define  MATCH_ROM	   0x55	   //匹配ROM指令
#define  SKIP_ROM      0xCC	   //跳过ROM指令
#define  ALARM_SEARCH  0xEC	   //报警搜索指令
#define  CONVERT_T     0x44	   //温度转换指令
#define  WRITE_SCR     0x4E    //写暂存器指令
#define  READ_SCR      0xBE	   //读暂存器指令
#define  COPY_SCR      0x48	   //复制暂存器指令



/*ds18b20初始化函数 返回初始化状态*/
unsigned char DS_Reset(void) 
{
  unsigned char presence;
  DQ = 1;          //DQ复位
  _nop_();;  //稍做延时
  DQ = 0;          //单片机将DQ拉低
  Delay_short(40); //精确延时480us
  DQ = 1;          //拉高总线
  Delay_short(3);  //等待存在信号
  presence=DQ;     //稍做延时后 如果presence=0则初始化成功 =1则初始化失败
  Delay_short(25);
  if(presence==0)
    return 1;	   //初始化成功返回1
  else
    return 0;	   //初始化失败返回0
   Delay_short(25);
   DQ = 1;          //拉高总线

} 

/*从ds18b20读一个字节 dat为返回值*/  
unsigned char DS_ReadByte(void)
{
  unsigned char i=0;
  unsigned char j=0;
  unsigned char dat=0;
	for (i=0;i<8;i++)
	 {
		  DQ = 0;               //给脉冲信号
		  dat>>=1;              //右移位等待接收一位数据
		  DQ = 1;               //给脉冲信号
		  for(j=0;j<3;j++) ;    //延时15微秒
		  if(DQ)                //从低位开始接收数据
		  dat|=0x80; 
		  Delay_short(6);       //等待复位
	 }
   return(dat);
}

/*向ds18b20写一个字节 dat为待写入字节*/ 
void DS_WriteByte(unsigned char dat)
{
 	unsigned char i=0;
 	for (i=0; i<8; i++)
 	{
  		DQ = 0;  //给脉冲信号
        _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
	    _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
		_nop_();  _nop_();  _nop_();  _nop_();  _nop_();
 		DQ = dat&0x01;	//从低位开始发送数据
    	Delay_short(1);
		_nop_();  _nop_();  _nop_();  _nop_();  _nop_();
 		DQ = 1;	 //给脉冲信号
    	dat>>=1; //右移位等待发送
    }
  
}
/*读取ds18b20当前温度 和Th,Tl              */
/*入口参数:器件编号(0-4)                 */
/*全局变量DS_Temp[5][3] DS_Th[5] DS_Tl[5]*/

void DS_ReadTemp(void)
{
	unsigned char temp_lsb=0; //温度寄存器低字节
	unsigned char temp_msb=0; //温度寄存器高字节
	unsigned char Temp=0;

	DS_Reset();			            //初始化
	DS_WriteByte(SKIP_ROM);        	//跳过读序号列号的操作
	DS_WriteByte(CONVERT_T); 	    //启动温度转换

	//Delay_short(5);                 //延时过程
	DS_Reset();			            //初始化
	DS_WriteByte(SKIP_ROM);
	DS_WriteByte(READ_SCR);         //读取温度寄存器前两个就是温度

	temp_lsb=DS_ReadByte();    	    //读取温度寄存器低字节
	temp_msb=DS_ReadByte();   	    //读取温度寄存器高字节
	//DS_Th=DS_ReadByte();		//读取TH
	//DS_Tl=DS_ReadByte();		//读取TL
	
	

	DS_Temp[0]=(temp_msb&0x07)<<4;         //取温度整数部分的高3位
	DS_Temp[0]+=(temp_lsb&0xf0)>>4; //取温度整数部分
	DS_Temp[1]=temp_lsb&0x0f;       //取温度小数部分    
	//DS_Temp[0]=0x03;
//	DS_Temp[1]=0x17;
    //num=(DS_Temp[0]*256+DS_Temp[1]*1)*0.0625;
	//return(num);
}

 



//初始化232
void Init_RSC232()
{
    //波特率:1200bit/s ,1位起始位,1位停止位,8位数据位
	TMOD = 0x20;
	TL1  = 0xE8;
	TH1  = 0xE8;
	TR1  = 1;
	PCON = 0x00;
	SCON = 0x50;
	
}

void printc(unsigned char c)
{
    TI   =  0 ;
	SBUF =  c ;
	while(TI==0)
	     ;
	TI   = 0  ;
}

void prints(unsigned char c[])
{
   unsigned char index = 0 ;
   while(c[index]!='\0')
   {
      printc(c[index]);
	  index++;
   }
}

void printhex(unsigned char i)
{
   unsigned char j;
       j = i + 0x30;
   printc(j);

}






void delay_n40us(uint n)

{    
       uint i;

      uchar j;            

        for(i=n;i>0;i--)

           for(j=0;j<15;j++)
		          ;          //

}                                         //

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

//*********主函数*****************

void main(void)

{     //int tempture;
      //float tempp;
	  Init_RSC232();   
      //DS_Reset();
	
	
  	
	 while(1)
		 {
		    
		     DS_ReadTemp();		  
		    // tempture=(int)num;
            // printhex(/100) ;		       //百位
             printhex((DS_Temp[0]%100)/10);       //十位
             printhex(DS_Temp[0]%10);		       //个位
             prints(".");							   //小数点
             printhex(DS_Temp[1]*10/16) ;	       //十分位
	       //printhex((num-tempture)*100%10) ;	       //百分位
			  prints(".C\n");
			 delay_n40us(1000);
		 }        
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -