DS18B20与51单片机多路温度传感器.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流DS18B20与51单片机多路温度传感器.精品文档.51单片机C程序#include<reg51.h>#include<intrins.h>#define uchar unsigned charsbit RS=P35;/定义LCD端口sbit RW=P36;sbit E=P37;sbit DQ=P34;/定义单总线端口uchar LCD_3,LCD_2,LCD_1,LCD_0;uchar ds18b20_romA8=0x8e,0x00,0x00,0x00,0xb8,0xc5,0x30,0x28;/四个DS18B20的64位ROM地址uchar ds18b20_romB8=0x52,0x00,0x00,0x00,0xb8,0xc5,0x34,0x28;uchar ds18b20_romC8=0x65,0x00,0x00,0x00,0xb8,0xc5,0x35,0x28;uchar ds18b20_romD8=0x3c,0x00,0x00,0x00,0xb8,0xc5,0x36,0x28;uchar code LCDData ="0123456789" uchar code dot_tab ="0112334456678899" uchar first_line16="A:000.0 B:000.0" /LCD第一行显示缓存数组uchar second_line16="C:000.0 D:000.0" /LCD第二行显示缓存数组/延时程序void delay(unsigned int N) unsigned int i; for(i=0;i<N;i+);/LCD1602测忙void check_busy(void)while(1)P1=0xff;E=0;_nop_();RS=0;_nop_();_nop_();RW=1;_nop_();_nop_();E=1;_nop_();_nop_();_nop_();_nop_();if(P1&0x80)=0)break;E=0;/LCD1602写命令void write_command(uchar tempdata) E=0;_nop_();_nop_();RS=0;_nop_();_nop_();RW=0;P1=tempdata;_nop_();_nop_();E=1;_nop_();_nop_(); E=0;_nop_();check_busy();/LCD1602写数据void write_data(uchar tempdata) E=0;_nop_();_nop_();RS=1;_nop_();_nop_();RW=0;P1=tempdata;_nop_();_nop_();E=1;_nop_();_nop_(); E=0;_nop_();check_busy();/初始化LCD1602void init_lcd1602() write_command(0x01);/清除屏幕 write_command(0x38);/功能设定(8位,2行,5*7点阵式) write_command(0x0c);/显示器ON,光标OFF,闪烁OFF write_command(0x06);/地址加1/初始化ds18b20bit resetpulse(void)DQ=0;delay(40);/延时500usDQ=1;delay(4);/延时60usreturn(DQ); /读取P1.0的状态void ds18b20_init(void)while(1)if(!resetpulse() /收到ds18b20的应答信号DQ=1;delay(40);/延时240usbreak;elseresetpulse();/否则再发复位信号/读ds18b20一位uchar read_bit(void) DQ=0; _nop_(); _nop_(); DQ=1; delay(2); return(DQ);/读ds18b20一个字节uchar read_byte(void) uchar i,m,receive_data; m=1; receive_data=0; for(i=0;i<8;i+) if(read_bit() receive_data=receive_data+(m<<i); delay(7); return(receive_data);/向ds18b20写一位void write_bit(uchar bitval) DQ=0; if(bitval=1) DQ=1; delay(5); DQ=1;/向DS18B20写一个字节命令void write_byte(uchar val) uchar i,temp; for(i=0;i<8;i+) temp=val>>i; temp=temp&0x01; write_bit(temp); delay(5);/64位的ROM序列号匹配bit match_rom(uchar *rom)uchar i;ds18b20_init();write_byte(0x55);for(i=8;i>0;i-)write_byte(*(rom+i-1);return 1;/从ds18b20中读出64位序列号/*uchar * read_rom(void)uchar rom8,i;ds18b20_init();write_byte(0x33);for(i=8;i>0;i-)romi-1=read_byte();return &rom0;/将64位序列号显示在LCD上显示出来/*void print_char(uchar a)if(a>=0&&a<=9) write_data(a+0x30);else if(a>=0x0a &&a<= 0x0f) write_data(a+0x37);/温度转换,将从ds18b20中读出来的温度值/转化成十进制的温度值,并存放显示缓存void convert_T(uchar temp_data_h,uchar temp_data_l) uchar temp; if(temp_data_h&0xf0)=0xf0)/如果温度寄存器里的高位为1, /则温度为负 temp_data_l=temp_data_l;/负温度将补码转成二进制, if(temp_data_l=0xff)/取反再加1 temp_data_l=temp_data_l+0x01; temp_data_h=temp_data_h; temp_data_h=temp_data_h+0x01; else temp_data_l=temp_data_l+0x01; temp_data_h=temp_data_h; LCD_0=dot_tabtemp_data_l&0x0f;/查表得小数位的值 temp=(temp_data_l&0xf0)>>4)|(temp_data_h&0x0f)<<4); LCD_3='-'/显示"-"号 LCD_2=LCDData(temp%100)/10;/查表得负温度温度十位 LCD_1=LCDData(temp%100)%10;/查表得负温度个位 else /温度为正 LCD_0=dot_tabtemp_data_l&0x0f;/查表得小数位的值 temp=(temp_data_l&0xf0)>>4)|(temp_data_h&0x0f)<<4); LCD_3=LCDDatatemp/100;/查表得温度百位 LCD_2=LCDData(temp%100)/10;/查表得温度十位 LCD_1=LCDData(temp%100)%10;/查表得温度个位void display(void) uchar i; write_command(0x80); for(i=0;i<16;i+) write_data(first_linei); write_command(0xc0); for(i=0;i<16;i+) write_data(second_linei);void main(void) uchar temp_data_l,temp_data_h;/*uchar *ds18b20_rom;*/init_lcd1602();/*while(1)uchar i;ds18b20_rom=read_rom(); /读序列号 write_command(0x80);for(i=0;i<8;i+) ds18b20_romAi=*ds18b20_rom;ds18b20_rom+;write_command(0x80);for(i=0;i<8;i+) /显示序列号print_char(ds18b20_romAi/16);print_char(ds18b20_romAi%16);while(1)ds18b20_init(); write_byte(0xcc); write_byte(0x44); /启动温度转换delay(700);match_rom(ds18b20_romA); /匹配第一路 write_byte(0xbe); /读温度 temp_data_l= read_byte(); /温度低8位 temp_data_h = read_byte(); /温度高8位 convert_T(temp_data_h,temp_data_l);first_line2=LCD_3; /温度转换后的各位first_line3=LCD_2;/分别存入显示缓存数组中first_line4=LCD_1;first_line6=LCD_0;match_rom(ds18b20_romB); /匹配第二路 write_byte(0xbe); /读温度 temp_data_l= read_byte(); /温度低8位 temp_data_h = read_byte(); /温度高8位 convert_T(temp_data_h,temp_data_l);first_line10=LCD_3; /温度转换后的各位first_line11=LCD_2;/分别存入显示缓存数组中first_line12=LCD_1;first_line14=LCD_0;display();match_rom(ds18b20_romC); /匹配第三路 write_byte(0xbe); /读温度 temp_data_l= read_byte(); /温度低8位 temp_data_h = read_byte(); /温度高8位 convert_T(temp_data_h,temp_data_l);second_line2=LCD_3; /温度转换后的各位second_line3=LCD_2;/分别存入显示缓存数组中second_line4=LCD_1;second_line6=LCD_0;match_rom(ds18b20_romD); /匹配第四路 write_byte(0xbe); /读温度 temp_data_l= read_byte(); /温度低8位 temp_data_h = read_byte(); /温度高8位 convert_T(temp_data_h,temp_data_l);second_line10=LCD_3;/温度转换后的各位second_line11=LCD_2; /分别存入显示缓存数组中second_line12=LCD_1;second_line14=LCD_0;display();