51单片机连接温度传感器

#include "reg51.h"
#include "intrins.h"
/*
unsigned char code yang_table[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//共阳极*/
unsigned char code ying_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00,0xff};//共阴极
unsigned char showdata[4]={1,1,0,3};
sbit SG1=P2^0;
sbit SG2=P2^1;
sbit SG3=P2^2;
sbit SG4=P2^3;
sfr AUXR=0x8e;
unsigned int tt=0;
sbit DQ=P3^7;
void DelayXus(unsigned char n);
void DS18B20_Reset();
void DS18B20_WriteByte(unsigned char dat);
unsigned char DS18B20_ReadByte();
//float f_temp; //浮点型温度值
//unsigned int tvalue;//温度值
//unsigned chaD          r tflag;
void DelayXus(unsigned char n)
{
 while(n--)
 {
  _nop_();
  _nop_();
 }
}
void DS18B20_Reset()
{
 CY=1;
 while(CY)
 {
  DQ=0;    //送出低电平复位信号
  DelayXus(240);    //演示至少480us
  DelayXus(240); 
  DQ=1;    //释放数据线
  DelayXus(60);    //等待60us
  CY=DQ;    //检测存在脉冲
  DelayXus(240);  //等待设备释放数据线
  DelayXus(180);
 }
}
unsigned char DS18B20_ReadByte()
{
 unsigned char i;
 unsigned char dat=0;
 for(i=0;i<8;i++)
 {
  dat>>=1;
  DQ=0;
  DelayXus(1);
  DQ=1;
  DelayXus(1);
  if (DQ) dat |= 0x80;
  DelayXus(60);
 }
 return dat;
}
void DS18B20_WriteByte(unsigned char dat)
{
 char i;
 for (i=0;i<8;i++)
 {
  DQ=0;    //开始时间片
  DelayXus(1);    //延时等待
  dat>>=1;    //送出数据
  DQ=CY;    
  DelayXus(60);    //等待时间片结束
  DQ=1;    //恢复数据线
  DelayXus(1);    //恢复延时
 }
}
unsigned int ReadTemp()
{
 unsigned char tempL=0;
 unsigned char tempH=0;
 bit tflag=0;
 unsigned int tvalue=0;
 DS18B20_Reset();    //设备复位
 DS18B20_WriteByte(0xcc);    //跳过ROM命令
 DS18B20_WriteByte(0x44);    //开始转换命令
 while(!DQ);    //等待转换完成
 DS18B20_Reset();    //设备复位
 DS18B20_WriteByte(0xcc);    //跳过ROM命令
 DS18B20_WriteByte(0xBE);    //读暂存存储器命令
 tempL=DS18B20_ReadByte(); //读温度低字节
 tempH=DS18B20_ReadByte(); //读温度高字节
 tvalue=(tempH<<8)|tempL;
 return (tvalue);
}
void TempPreprocessShow(unsigned int x)
{
 bit tflag=0;
 if(x<0x0fff)
 {
  tflag=0;
 }
 else
 {
  tflag=1;
  x=(~x)+1;
 }
 x=(unsigned int)(x*0.0625*10+0.5);
 if(tflag==1)
  showdata[0]=16;    //‘-’负号
 else
  showdata[0]=17;    //不显示
 showdata[1]=x/100;
 showdata[2]=x%100/10;
 showdata[3]=x%10;
}
void seg4show(unsigned char i)
{
 switch(i)
 {
  case 0:
   SG1=0;  SG2=1;
   SG3=1;    SG4=1; break;
  case 1:
   SG1=1;  SG2=0;
   SG3=1;    SG4=1; break;
  case 2:
   SG1=1;  SG2=1;
   SG3=0;    SG4=1; break;
  case 3:
   SG1=1;  SG2=1;
   SG3=1;    SG4=0; break;
  default:
   SG1=1;  SG2=1;
   SG3=1;    SG4=1; break;
 }
 if(i==2)
  P1=ying_table[showdata[i]]|0x80;
 else
  P1=ying_table[showdata[i]];
}
void Timer0Init(void)    //100微秒@11.0592MHz
{
 AUXR &= 0x7F;    //定时器时钟12T模式
 TMOD &= 0xF0;    //设置定时器模式
 TMOD |= 0x02;    //设置定时器模式
 TL0 = 0xA4;    //设置定时初始值
 TH0 = 0xA4;    //设置定时重载值
 TF0 = 0;    //清除TF0标志
 TR0 = 1;    //定时器0开始计时
}
void main()
{
 unsigned int Temp=0;
 Timer0Init();
 ET0=1;
 EA=1;
 while(1)
 {
  if(tt>=10000)
  {
   tt=0;
   Temp=ReadTemp();
   TempPreprocessShow(Temp);
  }
 }
}
void T0_ISR() interrupt 1 using 1
{
 tt++;
 if(tt%20==1)
  seg4show(tt%80/20);
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值