单片机控制的温度传感器C语言程序

上传人:pu****.1 文档编号:564398419 上传时间:2022-08-29 格式:DOC 页数:8 大小:42KB
返回 下载 相关 举报
单片机控制的温度传感器C语言程序_第1页
第1页 / 共8页
单片机控制的温度传感器C语言程序_第2页
第2页 / 共8页
单片机控制的温度传感器C语言程序_第3页
第3页 / 共8页
单片机控制的温度传感器C语言程序_第4页
第4页 / 共8页
单片机控制的温度传感器C语言程序_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《单片机控制的温度传感器C语言程序》由会员分享,可在线阅读,更多相关《单片机控制的温度传感器C语言程序(8页珍藏版)》请在金锄头文库上搜索。

1、#include #define DO P3_7 #define SEG P0 /数码管自左至右依次为1234位#define MS2L 0x18 /1ms的延时参数#define MS2H 0xfc#define uchar unsigned char#define uint unsigned intuchar code comm4 = 0x01,0x02,0x04,0x08;uchar code seg10 = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;uchar code seg_add_dicimal10 = 0x40,0x79

2、,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10; uchar code seg_dicimal2 =0xc0, 0x92;uchar k = 0;uchar temp_l = 0;uchar temp_h = 0;uchar tempsign = 0;uchar hundreds = 0;uchar tens = 0;uchar ones = 0;uchar low_four = 0;/*延时以ms为单位的t时间*/void Delay(uint t) uint i; while(t-) for(i=0; i125; i+) /*us级延时,延时时间约42*i

3、*/void Delayus(uchar i) while(-i); /*产生复位脉冲,等待应答信号*/void Resetpaulse() DO = 0; /拉低约600us Delayus(150); Delayus(150); DO = 1;/产生上升沿,延时约1560us Delayus(30); while(DO); /等待应答信号 /*读取数据一位*/bit Readbit() uint i = 0; bit b = 0; DO = 0; /产生读时隙 i+; /维持低电平至少1us DO = 1; /1us以上后拉高 Delayus(2); /延时8us,DO下降沿15内ds18

4、b20输出的数据才有效 b = DO; /读取数据 Delayus(40); /每个读时隙至少持续60us return(b); /*读取一个字节*/uchar Readbyte() uchar byte_read = 0; uchar i, j; for(i=0; i8; i+) j = Readbit(); byte_read = (ji) | byte_read; /低位读起 return(byte_read); /*写一个字节*/void Writebyte(uchar byte_to_write) uchar i = 0; uchar j = 0; bit write_bit = 0

5、; for(j=0; j 1; /*配置ds18b20,9位分辨率 */void Configurate() EA = 0; Resetpaulse(); /发出复位脉冲,每次操作都从复位开始 Delay(1); Writebyte(0xcc); /skip room命令 Writebyte(0x4e); Writebyte(0x7f); Writebyte(0x80); Writebyte(0x1f); EA = 1;/*启动温度转换*/void StartConvert() Resetpaulse(); / 发出复位脉冲,每次操作都从复位开始 Delay(1); EA = 0; Write

6、byte(0xcc); /skip room命令 Writebyte(0x44); /启动温度转换命令 EA = 1;/*读取温度值*/void ReadTempreture() EA = 0; Resetpaulse(); / 发出复位脉冲,每次操作都从复位开始 Delay(1); Writebyte(0xcc); /skip room命令 Writebyte(0xbe); /读取暂存器命令 temp_l = Readbyte(); /存储温度低字节值 (整数部分低四位和小数部分) temp_h= Readbyte(); /存储温度高字节值 (其中高五位为符号位) EA = 1;/*数据转换

7、*/void DigitalConvert() uchar total = 0; tempsign = (temp_h 7) & 0x01; /得出符号位 if(tempsign = 0) /正数的处理方法 total = (temp_h 4)&0x0f); /取整数位 low_four = (temp_l3) & 0x01; /取小数位,9位分辨率,低字节第3位为小数位,只有0、1两种取值 hundreds = total / 100; /计算百、十、个位 tens = (total%100)/10; ones = (total%100)%10; else /负数处理 求负数补码规则是,按位

8、取反,得到反码,加1即得补码(符号位不变) tempsign = 1; total = (temp_l 4) & 0x0f); /取整数部分低4位 total |= (temp_h 3) & 0x01; /取小数位 if(low_four = 0) /这里total位uchar型变量,所以根据最低位来确定整数取补规则 total = total + 1; /最低位为0的时候,取反加1后有进位,所以这里total要取反加1 else total = total;/最低位为1的时候,取反加1没有进位,所以total直接取反即可 tens = (total%100)/10; ones = (tota

9、l%100)%10; /*中断处理*/void Display()interrupt 1 EA = 0; TL0 = MS2L; TH0 = MS2H; DigitalConvert(); /数据转换 if(tempsign = 0)/正数 if(k = 0) if(hundreds = 0) /高位0消隐 P1 = 0; else P1 = commk; SEG = seghundreds; else if(k = 1) if(tens = 0 & hundreds = 0) /高位0消隐 P1 = 0; else P1 = commk; SEG = segtens; else if(k =

10、 2)/显示个位,因为个位有小数点,所以又定义了一个数组分开来显示 P1 = commk; SEG = seg_add_dicimalones; /要加上小数点else /显示小数,0或5 P1 = commk; SEG = seg_dicimallow_four; else if(tempsign = 1) /负数 if(k = 0)/显示符号位 P1 = commk; SEG = 0xbf; else if(k = 1) if(tens = 0) P1 = 0; else P1 = commk; SEG = segtens; else if(k = 2)/显示个位,因为各位有小数点,所以分

11、开来显示 P1 = commk; SEG = seg_add_dicimalones; /要加上小数点 else /显示小数,0或5 P1 = commk; SEG = seg_dicimallow_four; k+; if(k = 4) k = 0; EA = 1; /*主函数部分*/void main() TMOD = 0x01; TL0 = MS2L; TH0 = MS2H; TF0 = 0; EA = 1; ET0 = 1; TR0 = 1; Configurate(); do Delay(1); StartConvert(); Delay(100); ReadTempreture(); while(1);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号