📄 电脑显示的温度计.txt
字号:
//MCU: AT89S51
//晶振:11.0592M (一定要用这个晶振)
#include"AT89X51.H"
#include "absacc.h"
#include "stdio.h"
#include "stdlib.h"
#include "intrins.h"
unsigned char staus=0;
unsigned int send_count; /*定时器计数变量*/
unsigned char send_data[]="现在温度是:125.0 摄氏度";
//ds18b20 part
#define SkipRom 0xcc
#define ConvertTemperature 0x44
#define ReadScratchpad 0xbe
/*******************************************************************/
sbit One_Wire_Bus=P2^4;
/********************************************************************/
unsigned char GetScratchpad[2];
unsigned char code decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char code decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
unsigned char ResultTemperatureH;
unsigned char ResultTemperatureLH,ResultTemperatureLL;
unsigned char ResultSignal;
/********1ms延时子程序***********/
delay_nms(unsigned int n)
{
unsigned int i;
unsigned char j;
for(i=0;i<n;i++)
for(j=0;j<120;j++)
; //空操作
}
/********************************************************************/
void One_Wire_Delay(unsigned char delay_time)
{
while(delay_time)delay_time--;//延时时间:=(8+delay_time*6)us;
}
//*****初始化DS18B20******/
void Initize_One_Wire_Bus(void)
{
One_Wire_Bus=0;
One_Wire_Delay(80);//总线拉低 488us
One_Wire_Bus=1;
//One_Wire_Delay(25);//总线拉高 158us;
One_Wire_Delay(24);//总线拉高 152us;
}
/***********************************************************************/
/*******************读一个字节ds18b20 *********************************/
unsigned char One_Wire_Read_Byte(void)
{
bit temp_bit;
unsigned char i,result=0;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
One_Wire_Bus=1;
temp_bit=One_Wire_Bus;
//One_Wire_Delay(9);//延时 62 us
One_Wire_Delay(8);//延时 56 us
_nop_();
_nop_();
_nop_();
if(temp_bit)
result|=0x01<<i;
}
return(result); //返回ds18b20值
}
/*********向ds18b20写一个字节*********************/
void One_Wire_Write_Byte(unsigned char oww_dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
if(oww_dat&0x01)One_Wire_Bus=1;
//One_Wire_Delay(20);// 128 us
One_Wire_Delay(19);// 122 us
_nop_();
_nop_();
_nop_();
_nop_();
One_Wire_Bus=1;
oww_dat>>=1;
}
One_Wire_Delay(10);
}
/******************************************/
void Read_18B20(void)
{
unsigned char tempH,tempL;
Initize_One_Wire_Bus();
One_Wire_Write_Byte(SkipRom);
_nop_();
//There is just one DS1820 on the bus;
One_Wire_Write_Byte(ConvertTemperature);
One_Wire_Delay(5);
//Start to convert temperature;
Initize_One_Wire_Bus();
One_Wire_Write_Byte(SkipRom);
_nop_();
One_Wire_Write_Byte(ReadScratchpad);
GetScratchpad[0]=One_Wire_Read_Byte();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=One_Wire_Read_Byte();
//Master samples the MSB temperature from the scratchpad;
One_Wire_Delay(120);
tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);
tempL=(GetScratchpad[0]&0x0f);
Initize_One_Wire_Bus();
//Issue a reset to terminate left parts;
if(tempH&0x80)
{
tempH=~tempH;
tempL=~tempL+1;
ResultSignal=1;
//Negative temperature;
}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad;
/***********************************************************************/
void main(void)
{
// 以下是设置中断和定时器
PT0=1;
ET0=1;
// ES=1; //开串口接收中断
IT0=1;IT1=1;
TMOD=0X20;
// 定时器0为产生1ms的中断,
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
//定时器1为9600bit/s 的波特率发生器
TL1=0xfd; //波特率发生
TH1=0xfd;
SCON=0xd8;
PCON=0X00;//波特率不加倍
// 开定时器
TR1=1;
TR0=1;
TI=1;
EA=1;
while(1)
{
;
}
}
// 定时器0的中断服务程序,向串口发送姿态数据。
void intsvr1(void) interrupt 1
{
TR0=0;
// 重新装入定时器0的初值;
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
send_count++;
//每4秒发送一次信息到电脑上
if (send_count==4000) // 4秒判断
{
send_count=0;
Read_18B20();
send_data[11]=(ResultTemperatureH/100)|0x30; //温度百位
if(send_data[11]==0x30)
send_data[11]=0xff;
send_data[12]=((ResultTemperatureH%100)/10)|0x30; //温度十位
send_data[13]=(ResultTemperatureH%10)|0x30; //温度个位
send_data[15]=(ResultTemperatureLH/10)|0x30; //温度小数位
puts(send_data);
puts("*************************************");
puts("\0");
// putchar(0x0d); //回车
// putchar(0x0a); //换行
}
TR0=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -