四极管:温度监控之 AVR 18B20

本文主要介绍使用AVR微控制器配合18B20传感器进行温度监控的实现。通过四极管连接和编程,实现对环境温度的实时监测。
摘要由CSDN通过智能技术生成

主函数

四极管:MAIN.C

#include<avr/io.h>
#include<util/delay.h>
#include<MyBit.h>	
#include<1602driver.c>
#include<18b20.c>
unsigned long   temperature;
unsigned long   temp_2;


const unsigned char temp_1[]="0123456789";

void main(void)
{   

    Init_1602();
	Displaypstr(0,1,"18B20--LCD1602--");
	unsigned char th,tl;
	
    while(1) 
    {  
	
		temperature = Read_18b20();
		th = ((((temperature&0xff00)>>8)&0x07)<<4)|((temperature&0x00f0)>>4);
		tl = (temperature&0x000f)*6.25;
		
		Displaychar(0,0,temp_1[th/10%10]);
		Displaychar(1,0,temp_1[th%10]);
		Displaychar(2,0,'.');
		Displaychar(3,0,temp_1[tl/10%10]);
        Displaychar(4,0,temp_1[tl%10]); 	
     }	
}
//这种方法有问题,有时间再回来看看~~~~~~	
	/*
        temp_2 = temperature*0.0625*10000;        //不要直接乘625,否则数据可能溢出
//temp_2 =1234567;
         
	  Displaychar(8,0,temp_1[temp_2%10]);
	  //if(temp_2>=10)
	  Displaychar(7,0,temp_1[temp_2/10%10]);
	  //if(temp_2>=100)
	  Displaychar(6,0,temp_1[temp_2/100%10]);
	  //if(temp_2>=1000)
	  
	  Displaychar(5,0,temp_1[temp_2/1000%10]);
	  Displaychar(4,0,'.');
	  //if(temp_2>=10000)
	  Displaychar(3,0,temp_1[temp_2/10000%10]);

	  //if(temp_2>=100000)
	  Displaychar(2,0,temp_1[temp_2/100000%10]);
	  //if(temp_2>=1000000)
	  Displaychar(1,0,temp_1[temp_2/1000000%10]);
	  //if(temp_2>=10000000)
	  Displaychar(0,0,temp_1[temp_2/10000000%10]); 
	*/	
	/*	//=Read_18b20();     
        // Read_DS18B20();
		Displaychar(0,0,Data[temp_int/10]);
		Displaychar(1,0,Data[temp_int%10]);
		Displaychar(2,0,'.');
		Displaychar(3,0,Data[temp_point/10]);
        Displaychar(4,0,Data[temp_point%10]); 
		Displaychar(6,0,'5'); 
		*/
    

  温度芯片,单总线写法···在公司的时候被领导问  单总线一共可以挂多少个18B20,惭愧了,答不上来
/***********************************
 函数功能:DB18B20
 函数说明:
          
 使用环境:    硬件: MCU: ATmega32L 
                      F_CPU = 16000000
                      外部:16000000 
 接线方法:DQ-PD7
				  
 编译环境:WinAVR-20080610	
 显示说明:        
包含文件 :#include<MyBit.h>	
           #include<util/delay.h>
作者:杨琦			  
日期:2009年7月17日
修改者: 			   	(日期:  )
************************************************/
//端口预定义

#define DDR_DQ RD7
#define PIN_DQ ID7
#define POR_DQ OD7

unsigned long temp_l,temp_h;
/*********************************************************
* 名称nit_DS18B20(void)
* 说明:18B20初始化
* 功能:18B20初始化
* 调用_delay_us();   //此处1us约为1.24us
* 输入:K----16位
* 全局变量:NULL
* 返回值:无
********************************************************/
void Init_DS18B20(void)
{
   while(1)
    {
    unsigned char flag=0;
    
    DDR_DQ=1;    //设置为输出
    POR_DQ=0;    //低电平
    _delay_us(564);  //主机发出复位低电平480us~960us

    POR_DQ=1;          //释放总线
    _delay_us(4);      //等待回应-大概60us-240us后
    DDR_DQ=0;            //设置为输入
	_delay_us(200);
	DDR_DQ=1;
	POR_DQ=1;
	_delay_us(260);
	break;      

    }
}
/*********************************************************
* 名称:read_18b20(void)
* 说明:读一个字节
* 功能:读一个字节
* 调用_delay_us();   //此处1us约为1.24us
* 输入:无
* 全局变量:NULL
* 返回值:unsigned char      8位
********************************************************/
unsigned char read_18b20(void)
{
  unsigned char i=8,dat=0;
    DDR_DQ=0;            //设置为输出
    POR_DQ=1;                //拉高
    for(;i>0;i--)
      {
        dat>>=1;
        POR_DQ=0;             //低电平开始
        _delay_us(3);     //延时1us~15us  5us
        POR_DQ=1;            //释放总线
        
        DDR_DQ=0;         //设置为输入
		_delay_us(4);      //等待温度芯片发送数据    
        if(PIN_DQ)dat|=0x80; //读取数据
        _delay_us(81);      //延迟一段时间60~120us   
        DDR_DQ=1;         //设置为输入
        POR_DQ=1;    
       }
    return(dat);
	
}
/*********************************************************
* 名称:write_data(unsigned char dat)
* 说明:写一个字节
* 功能:写一个字节
* 调用_delay_us();   //此处1us约为1.24us
* 输入:unsigned char dat  //8位
* 全局变量:NULL
* 返回值:无
********************************************************/
void write_data(unsigned char dat)
{
   unsigned char i=8;
   DDR_DQ=1;             //设置为输出
   POR_DQ=1;
   for(;i>0;i--)
     {
      POR_DQ=0;             //低电平开始
      _delay_us(3);   //延时1us~15us  8us
      POR_DQ=dat&1;        
      _delay_us(81);     //延迟一段时间给温度芯片采样数据60-120us
      POR_DQ=1;             //释放总线
      dat>>=1;             //下一位
      }
}
/*********************************************************
* 名称:Read_18b20(void)
* 说明:读温度子程序
* 功能:写一个字节
* 调用write_data();  
* 输入:无
* 全局变量:无
* 返回值:int
********************************************************/
int  Read_18b20(void)
{  //TEMP_H=4;
   int t = 0;
   Init_DS18B20();      //复位
   write_data(0xCC);  //跳过ROM匹配
   write_data(0x44);  //启动温度转换

  //_delay_us(300);  //延迟一段时间

   Init_DS18B20();      //复位
   write_data(0xCC);  //跳过ROM匹配
   write_data(0xBE);  //发出读温度指令

    temp_l=read_18b20();  //温度值低位字节(其中低4位为二进制的“小数”部分)
    temp_h=read_18b20();  // 高位值高位字节(其中高5位为符号位)
   // temp_int=((temp_h<<4)&0xF0)|((temp_l>>4)&0x0f);//整数部分
   // temp_point=(temp_l&0x0f)*6.25*100;//小数部分 
   t =temp_h;
   t <<= 8;
   t  |= temp_l;
   return(t);
   
}

显示和定义文件在上一篇文章中有,就不在列出来了·

转载请注明出处。作者:四极管。广西师范大学 电子工程学院大学生科技创新基地 邮箱: yangxingbo-0311@163.com


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值