DS18B20与51单片机多路温度传感器_第1页
DS18B20与51单片机多路温度传感器_第2页
DS18B20与51单片机多路温度传感器_第3页
DS18B20与51单片机多路温度传感器_第4页
DS18B20与51单片机多路温度传感器_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、51单片机C程序#include#include#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_rom

2、C8=0x65,0x00,0x00,0x00,0xb8,0xc5,0x35,0x28;uchar ds18b20_romD8=0x3c,0x00,0x00,0x00,0xb8,0xc5,0x36,0x28;uchar code LCDData =; uchar code dot_tab =78899; 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

3、i; for(i=0;iN;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_();_

4、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();/*/初始化LCD1602/*void init_lcd1602() write_command(0x01);/清除屏幕 write_command(0x38)

5、;/功能设定(8位,2行,5*7点阵式) write_command(0x0c);/显示器ON,光标OFF,闪烁OFF write_command(0x06);/地址加1/*/初始化ds18b20/*bit 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();/否则再发

6、复位信号/*/读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;i8;i+) if(read_bit() receive_data=receive_data+(mi); delay(7); return(receive_data);/*/向ds18b20写一位/*void write_bit(uc

7、har bitval) DQ=0; if(bitval=1) DQ=1; delay(5); DQ=1;/*/向DS18B20写一个字节命令/*void write_byte(uchar val) uchar i,temp; for(i=0;ii; 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;i0;i-)write_byte(*(rom+i-1);return 1;/*/从d

8、s18b20中读出64位序列号/*/*uchar * read_rom(void)uchar rom8,i;ds18b20_init();write_byte(0x33);for(i=8;i0;i-)romi-1=read_byte();return &rom0;*/*/将64位序列号显示在LCD上显示出来/*/*void print_char(uchar a)if(a=0&a=0x0a &a4)|(temp_data_h&0x0f)4)|(temp_data_h&0x0f)4); LCD_3=LCDDatatemp/100;/查表得温度百位 LCD_2=LCDData(temp%100)/1

9、0;/查表得温度十位 LCD_1=LCDData(temp%100)%10;/查表得温度个位 void display(void) uchar i; write_command(0x80); for(i=0;i16;i+) write_data(first_linei); write_command(0xc0); for(i=0;i16;i+) write_data(second_linei);void main(void) uchar temp_data_l,temp_data_h;/*uchar *ds18b20_rom;*/init_lcd1602();/*while(1)uchar i

10、;ds18b20_rom=read_rom(); /读序列号 write_command(0x80);for(i=0;i8;i+) ds18b20_romAi=*ds18b20_rom;ds18b20_rom+;write_command(0x80);for(i=0;i8;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(ds18b

11、20_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

12、_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=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论