立即注册 登录
返回首页

uid:67624的个人空间

日志

ds18b20数码管显示温度

已有 861 次阅读2014-10-19 11:00 |个人分类:单片机程序

#include<reg51.h>
 #include<intrins.h> 
#define uchar unsigned char
 #define uint unsigned int 
sbit w1=P2^4; 
sbit w2=P2^5;
 sbit w3=P2^6; 
sbit w4=P2^7;
 sbit DQ=P3^7; 
uint temp; 
float f_temp;                           //温度值 variable of temperature 
bit flag; 
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f      
                                   ,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef }; 
 void delay(uint z)
 {
  uint x,y; 
 for(x=z;x>0;x--) 
  for(y=110;y>0;y--);
 } 
void Init_Ds18b20(void)     //DS18B20初始化send reset and initialization command
 {
  uint i;  
 DQ = 0;                    //单片机拉低总线 
 i=70;  while(i>0)i--;  
DQ = 1;                    //释放总线,即拉高了总线  i=4;  while(i>0)i--; } 
bit Read_One_bit()       //读取一个字节的数据read a byte date 
                            //读数据时,数据以字节的最低有效位先从总线移出 
{
  uint i; 
 bit dat;  
 DQ=0;i++;  
 DQ=1;i++;i++; 
 dat=DQ; 
 i=3;while(i>0)i--;  
return (dat); 
uchar Read_One_Byte(void) 
{  
uchar i,j,dat;  
dat=0;  
for(i=1;i<=8;i++)
  {  
 j=Read_One_bit();  
 dat=(j<<7)|(dat>>1); 
 }  
return (dat);
 }  
void Write_One_Byte(uchar dat) 
{
  uint i;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++) 
 {   
testb=dat&0x01;  
 dat=dat>>1; 
  if(testb)  
 {  
  DQ=0;i++;i++;  
  DQ=1;   
 i=5;while(i>0)i--; 
  }   
else  
 {   
 DQ=0;   
 i=5;while(i>0)i--;   
 DQ=1;   
 i++;i++;     
  }
  }
 } 
void tmpchange(void)
{
  uchar f;  
Init_Ds18b20(); 
 f=70;while(f>0)f--;  
delay(1);  
Write_One_Byte(0xcc);          //忽略ROM指令  
Write_One_Byte(0x44);          //温度转换指令 
 } 
uint Get_Tmp()                   //获取温度get the temperature 
{
  uchar a,b,f; 
 Init_Ds18b20();                //初始化 
 f=70;while(f>0)f--;  
Write_One_Byte(0xcc);          //忽略ROM指令  
Write_One_Byte(0xbe);          //温度转换指令
  a = Read_One_Byte();           //读取到的第一个字节为温度LSB 
 b = Read_One_Byte();           //读取到的第一个字节为温度MSB 
 temp = b;                      //先把高八位有效数据赋于temp  
temp <<= 8;                    //把以上8位数据从temp低八位移到高八位 
 temp = temp|a;                //两字节合成一个整型变量 
 f_temp = temp*0.0625;             
 temp = f_temp*10+0.5;               //放大十倍  
f_temp=f_temp+0.5;                       //同时进行一个四舍五入操作。
  return temp;
 }  
/****************数码码动态显示函数**************/  
void Display(uint temp)   //显示程序
 { 
 uint A1,A2,A3;
  A1 = temp/100;    //百位
  A2 = temp%100/10;   //十位
  A3 = temp%10;    //个位 
  if(flag)  
{
   w1=0;
   P0 =0x40;   //用来显示负号 
  delay(1); 
  w1=1;
   P0=0x00;
   flag=0; 
  } 
 w2=0; 
 P0 = table[A1];    //显示百位
  delay(10); 
 w2=1;  
P0=0x00;  
  w3=0; 
 P0 = table[A2+10];   //显示十位,使用的是有小数点的数组(因为temp值扩大了10倍,虽然是十位,实际为个位)
  delay(10); 
 w3=1;
  P0=0x00;
    w4=0;
  P0 = table[A3];    //显示个位 
 delay(10); 
 w4=1;
  P0=0x00;
 }       
void main() 
{      
      while(1)
         {
              tmpchange();  
             Display(Get_Tmp()); 
       }
 }

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

返回顶部