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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
uint i = 0;
bit b = 0;
DO = 0; //产生读时隙
i++;//维持低电平至少1us
DO = 1; //1us以上后拉高
Delayus(2); //延时8us,DO下降沿15内ds18b20输出的数据才有效
b = DO; //读取数据
Delayus(40);//每个读时隙至少持续60us
Writebyte(0x80);
Writebyte(0x1f);
EA = 1;
}
/*启动温度转换*/
void StartConvert()
{
Resetpaulse();//发出复位脉冲,每次操作都从复位开始
Delay(1);
EA = 0;
Writebyte(0xcc); //skip room命令
Writebyte(0x44); //启动温度转换命令
Delay(100);
ReadTempreture();
}
while(1);
}
}
/*产生复位脉冲,等待应答信号*/
void Resetpaulse()
{
DO = 0; //拉低约600us
Delayus(150);
Delayus(150);
DO = 1;//产生上升沿,延时约15~60us
Delayus(30);
while(~DO); //等待应答信号
}
/*读取数据一位*/
bit Readbit()
EA = 1;
}
/*读取温度值*/
void ReadTempreture()
{
EA = 0;
Resetpaulse();//发出复位脉冲,每次操作都从复位开始
Delay(1);
Writebyte(0xcc); //skip room命令
Writebyte(0xbe); //读取暂存器命令
temp_l = Readbyte(); //存储温度低字节值(整数部分低四位和小数部分)
temp_h= Readbyte(); //存储温度高字节值(其中高五位为符号位)
EA = 1;
}
/*数据转换*/
void DigitalConvert()
{
uchar total = 0;
tempsign = (temp_h >> 7) & 0x01; //得出符号位
if(tempsign == 0)//正数的处理方法
return(b);
}
/*读取一个字节*/
uchar Readbyte()
{
uchar byte_read = 0;
uchar i, j;
for(i=0; i<8; i++)
{
j = Readbit();
byte_read = (j<<i) | byte_read; //低位读起
}
return(byte_read);
}
/*写一个字节*/
void Writebyte(uchar byte_to_write)
{
uchar i = 0;
uchar j = 0;
bit write_bit = 0;
for(j=0; j<8; j++)
{
write_bit =(byte_to_write & 0x01);
if(write_bit == 1) //写1
uchar code comm[4] = {0x01,0x02,0x04,0x08};
uchar code seg[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code seg_add_dicimal[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
tens = (total%100)/10;
ones = (total%100)%10;
}
else //负数处理求负数补码规则是,按位取反,得到反码,加1即得补码(符号位不变)
{
tempsign = 1;
total = ((temp_l >> 4) & 0x0f); //取整数部分低4位
total |= ((temp_h << 4) & 0xf0); //整数部分高三位和符号位
SEG = seg[tens];
}
}
else if(k == 2)//显示个位,因为各位有小数点,所以分开来显示
{
P1 = comm[k];
SEG = seg_add_dicimal[ones]; //要加上小数点
}
else //显示小数,0或5
{
P1 = comm[k];
SEG = seg_dicimal[low_four];
#include <at89x52.h>
#define DO P3_7
#define SEG P0 //数码管自左至右依次为1234位
#define MS2L 0x18//1ms的延时参数
#define MS2H 0xfc
#define uchar unsigned char
#define uint unsigned int
{
total = ((temp_h << 4)&0xf0) | ((temp_l >> 4)&0x0f); //取整数位
low_four = (temp_l>>3) & 0x01; //取小数位,9位分辨率,低字节第3位为小数位,只有0、1两种取值
hundreds = total / 100; //计算百、十、个位
uchar low_four = 0;
/*延时以ms为单位的t时间*/
void Delay(uint t)
{
uint i;
while(t--)
{
for(i=0; i<125; i++)
{ }
}
}
/*us级延时,延时时间约4+2*i*/
void Delayus(uchar i)
{
while(--i);
low_four = (temp_l >> 3) & 0x01;//取小数位
if(low_four == 0)//这里total位uchar型变量,所以根据最低位来确定整数取补规则
total = ~total + 1;//最低位为0的时候,取反加1后有进位,所以这里total要取反加1
else
total = ~total;//最低位为1的时候,取反加1没有进位,所以total直接取反即可
}
}
/*配置ds18b20,9位分辨率*/
void Configurate()
{
EA = 0;
Resetpaulse();//发出复位脉冲,每次操作都从复位开始
Delay(1);
Writebyte(0xcc); //skip room命令
Writebyte(0x4e);
Writebyte(0x7f);
{
DO = 0; //产生写时隙
Delayus(3);//延时15us
DO = 1; //写1
Delayus(40); //延时,写时隙不得低于60us
}
else
{
DO = 0; //产生写时隙
Delayus(50); //延时,保持低约60us~120us
DO = 1;
i++;
}
byte_to_write = byte_to_write >> 1;
SEG = seg_dicimal[low_four];
}
}
else if(tempsign == 1)//负数
{
if(k == 0)//显示符号位
{
P1 = comm[k];
SEG = 0xbf;
}
else if(k == 1)
{
if(tens == 0)
{P1 = 0;}
else
{
P1 = comm[k];
{
if(k == 0)
{
if(hundreds == 0)//高位0消隐
{P1 = 0;}
else
{
P1 = comm[k];
SEG = seg[hundreds];
}
}
else if(k == 1)
{
if(tens == 0 && hundreds == 0) //高位0消隐
{P1 = 0;}
else
uchar code seg_dicimal[2] ={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;
tens = (total%100)/10;
ones = (total%100)%10;
}
}
/*中断处理*/
void Display()interrupt 1
{
EA = 0;
TL0 = MS2L;
TH0 = MS2H;
DigitalConvert(); //数据转换
if(tempsign == 0)//正数
{
P1 = comm[k];
SEG = seg[tens];
}
}
else if(k == 2)//显示个位,因为个位有小数点,所以又定义了一个数组分开来显示
{
P1 = comm[k];
SEG = seg_add_dicimal[ones]; //要加上小数点
}
else//显示小数,0或5
{
P1 = comm[k];Βιβλιοθήκη Baidu
}
}
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();
相关文档
最新文档