当前位置:文档之家› ATmega16的DS18B20测温程序

ATmega16的DS18B20测温程序

//DS18B20初始化程序 uchar ds18b20_reset() {
unsigned char errTime=0;//用于循环计数
DQ_OUT;//先设置成输出 DQ_CLR;//总线拉低
Delay_Us(500);//保持500us(最小为480us,最大为960us) DQ_IN;//1 _NOP(); while(DQ_R)//探测 IO 引上是上升沿 {
uchar i; for(i=0;i<8;i++)//1个字节有8位,1位1位的传输
{ DQ_OUT;//先设置成输出 DQ_CLR;//总线拉低 Delay_Us(10);//按照写1时序,在15us 中完成所以延时10us if(value&0x01)//判断此时写入的值是1还是0 { DQ_SET;//如果是1,总线拉高,使得18B20能采样 } else DQ_CLR; Delay_Us(100);//如果是0(低电平)就不用管,继续延时(15+15+30=60us,100us 足够) DQ_SET;//释放总线 value=value>>1;//每次传输完后移位 } } uint ds18b20_read_byte(void)//18B20读一个字节的程序 { uint i,value;
for(i=0;i<8;i++) {
value=value>>1;//移位,最后一次读正好是最高位 DQ_OUT;//先设置成输出 DQ_CLR;//总线拉低 Delay_Us(10);//>1us,<15us 控制器读1时序 DQ_SET;//总线释放准备采样 DQ_IN;//采样,设置成输入 if(DQ_R)//如果读到的值是1 { value|=0x80;//从低位开始读取 } Delay_Us(50);//>45us } return value; } //读取温度值先读取暂存器的值在进行温度转换否则会意外出错 unsigned int readTempDS18B20(void) {
void write_dat(uchar dat) {
PORTD|=BIT(4); PORTD&=~BIT(5); PORTB=dat; PORTD|=BIT(6); delay(1); PORTD&=~BIT(6); } void LCD_init() {
DDRB=0XFF; DDRD|=BIT(4)|BIT(5)|BIT(6); PORTD&=~BIT(6);
ds18b20_reset();//18B20初始化 ds18b20_write_byte(0xcc);//对 ROM 进行操作,因为只接了1个器件所以写跳过指令 ds18b20_write_byte(0x44);//启动温度转换 Delay_ms(1);//给硬件一点时间让其进行转换 return(temp); }
/************************************************************** **函数功能:10us 级延时 **输入参数:x10us:延时 x10us 数 **返回值:无 **在本函数外定义变量:无 **调用的函数:无 **************************************************************/ void Delay10us(int x10us);
基于 ATMEGA16的 DS18B20测温程序
main 函数:
/* 程序功能:18B20测温结果在1602液晶上显示 说明:PA5为输入端口 18B20参数:测温范围:-55~+125℃,在-10~+85℃精确度为±5 通信方式:单总线
*/ #include <iom16v.h> #include <macros.h> #include "delay.h" #include "display.h" #include "18B20.h" #define uchar unsigned char #define uint unsigned int
uint temperture; void main() {
LCD_init();//1602液晶初始化 while(1) { temperture=readTempDS18B20(); display();//调用显示函数 } } 18B20.c:
#include <iom16v.h> #include <macros.h> #include"18B20.h" #define uchar unsigned char
--------------------------------------------------------
display.c: #include <iom16v.h> #include <macros.h> #include"display.h" #define uchar unsigned char #define uint unsigned int
#define display_h extern void write_com(uchar com); extern void write_dat(uchar dat); extern void LCD_init(); extern void display(); extern uchar table[]; #endif delay.h:
Delay_Us(6);//5.15us errTime++; if(errTime>20) return(0x00); //如果等待大于约 5.15us*20就返回0x00,报告复位失败(实际上只要等 待15-60us)
} errTime=0; while(!(DQ_R))//注意(DQ_R)与 DQ_R 不同 {
/************************************************************** **函数功能:延时1ms **输入参数:无 **返回值:无
**在本函数外定义变量:无 **调用的函数:NOP() **************************************************************/ void Delay_1ms(void);
delay(5); write_com(0X80+15); delay(5); write_dat(table1[temperture%10]); delay(5);
write_com(0X80+0X40); delay(5);
for(i=0;i<12;i++) {
write_dat(table2[i]); delay(5);
Delay_Us(6);//5.15us errTime++; if(errTime>50) return(0x00); //如果等待大于约 5.15us*50就返回0x00,报告复位失败(实际上只要等 待60-240us) } return(0xff); }
void ds18b20_write_byte(uchar value)//18B20写一个字节的程序 {
uchar table1[]="0123456789"; uchar table2[]="design:zhubo"; uchar table3[]="temperature:"; uchar table4[]=".";
extern int temperture; void delay(unsigned int ms) {
/************************************************************** **函数功能:ms 级延时 **输入参数:xms:延时 ms 数 **返回值:无 **在本函数外定义变量:无 **调用的函数:Delay_1ms() **************************************************************/ void Delay_ms(unsigned int xms);
} }
-------------------------------------------------------接口函数: 18B20.h: #define uchar unsigned char #define uint unsigned int
extern uchar ds18b20_reset(); extern void ds18b20_write_byte(uchar value); extern uint ds18b20_read_byte(void); extern uint readTempDS18B20(void); display.h: #define uchar unsigned char #define uint unsigned int #ifndef display_h
unsigned char tempL,tempH; unsigned int temp; //开始读取温度
ds18b20_reset();//18B20复位 ds18b20_write_byte(0xcc);//跳过 ROM ds18b20_write_byte(0xbe);//命令读取暂存器 tempL=ds18b20_read_byte();//从暂存器中读取数据 tempH=ds18b20_read_byte();//从暂存器中读取数据 temp=(tempH<<8)|tempL;//总值为高位*256+低位 temp=temp*0.625;//为了保留1位小数,最小单位为0.0625
相关主题