我这个程序不知道怎么啦,编译没错但仿真不了

2019-07-15 20:38发布

#include<reg51.h>
#include"temp.h"
#define uint unsigned int
#define uchar unsigned char
sbit    K=P1^7;
sbit   LED=P1^0;
uchar code DIG_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示0~9的值
void display(int temp );
void Delay(uint z);
void  elec(int  temp);

void main()
{
while(1)
  {          
     elec(Ds18b20ReadTemp());
     display( Ds18b20ReadTemp());
  }


}

//显示函数
void display(int  temp )
{
  if(temp<0)
      {
                temp=temp-1;
                temp=~temp;

                P0=0x00;
        P0=0x40;
        P2=0x7f;
        Delay(1);

           P0=0x00;
       P0=DIG_CODE[temp/100];
       P2=0xbf;
       Delay(1);

           P0=0x00;
       P0=DIG_CODE[temp%100/10];
       P2=0xdf;
       Delay(1);


      P0=0x00;
          P0=DIG_CODE[temp%100%10];
      P2=0xef;
      Delay(1);
          }
         else
         {       
            P0=0x00;
                 P0=0x00;
        P2=0x7f;
        Delay(1);

           P0=0x00;
       P0=DIG_CODE[temp/100];
       P2=0xbf;
       Delay(1);

           P0=0x00;
       P0=DIG_CODE[temp%100/10];
       P2=0xdf;
       Delay(1);

          P0=0x00;
      P0=DIG_CODE[temp%100%10];
      P2=0xef;
      Delay(1);
         
          }

}

//延时函数
void Delay(uint z)
{
unsigned  int x,y;
for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

void  elec(int  temp)
{

    if(temp>-15)
        {
           LED=1;
           Delay(100);
           LED=0;
           Delay(100);
        }
   if(temp<-30)
         K=1;
         while(temp>-25)
         {
         K=0;
         
         }
        return;
}


/*******************************************************************************/
#include"temp.h"

/*******************************************************************************
* 函数名         : Delay1ms
* 函数功能                   : 延时函数
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

void Delay1ms(unsigned int y)
{
        unsigned int x;
        for(y;y>0;y--)
                for(x=110;x>0;x--);
}
/*******************************************************************************
* 函数名         : Ds18b20Init
* 函数功能                   : 初始化
* 输入           : 无
* 输出                  : 初始化成功返回1,失败返回0
*******************************************************************************/

unsigned char Ds18b20Init()
{
        unsigned int i;
        DQ=0;                         //将总线拉低480us~960us
        i=70;       
        while(i--);//延时642us
        DQ=1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
        i=0;
        while(DQ)        //等待DS18B20拉低总线
        {
                i++;
                if(i>5000)//等待>5MS
                        return 0;//初始化失败       
        }
        return 1;//初始化成功
}

/*******************************************************************************
* 函数名         : Ds18b20WriteByte
* 函数功能                   : 向18B20写入一个字节
* 输入           : com
* 输出                  : 无
*******************************************************************************/

void Ds18b20WriteByte(unsigned char dat)
{
        unsigned int i,j;
        for(j=0;j<8;j++)
        {
                DQ=0;                        //每写入一位数据之前先把总线拉低1us
                i++;
                DQ=dat&0x01; //然后写入一个数据,从最低位开始
                i=6;
                while(i--); //延时68us,持续时间最少60us
                DQ=1;        //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
                dat>>=1;
        }
}
/*******************************************************************************
* 函数名         : Ds18b20ReadByte
* 函数功能                   : 读取一个字节
* 输入           : com
* 输出                  : 无
*******************************************************************************/


unsigned char Ds18b20ReadByte()
{
        unsigned char byte,bi;
        unsigned int i,j;       
        for(j=8;j>0;j--)
        {
                DQ=0;//先将总线拉低1us
                i++;
                DQ=1;//然后释放总线
                i++;
                i++;//延时6us等待数据稳定
                bi=DQ;         //读取数据,从最低位开始读取
                             /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
                byte=(byte>>1)|(bi<<7);                                                  
                i=4;                //读取完之后等待48us再接着读取下一个数
                while(i--);
        }                               
        return byte;
}
/*******************************************************************************
* 函数名         : Ds18b20ChangTemp
* 函数功能                   : 让18b20开始转换温度
* 输入           : com
* 输出                  : 无
*******************************************************************************/

void  Ds18b20ChangTemp()
{
        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);                //跳过ROM操作命令                 
        Ds18b20WriteByte(0x44);            //温度转换命令
//        Delay1ms(100);        //等待转换成功,而如果你是一直刷着的话,就不用这个延时了

}
/*******************************************************************************
* 函数名         : Ds18b20ReadTempCom
* 函数功能                   : 发送读取温度命令
* 输入           : com
* 输出                  : 无
*******************************************************************************/

void  Ds18b20ReadTempCom()
{       

        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);         //跳过ROM操作命令
        Ds18b20WriteByte(0xbe);         //发送读取温度命令
}
/*******************************************************************************
* 函数名         : Ds18b20ReadTemp
* 函数功能                   : 读取温度
* 输入           : com
* 输出                  : 无
*******************************************************************************/

int Ds18b20ReadTemp()
{
        int temp=0;
        unsigned char tmh,tml;
        Ds18b20ChangTemp();                                 //先写入转换命令
        Ds18b20ReadTempCom();                        //然后等待转换完后发送读取温度命令
        tml=Ds18b20ReadByte();                //读取温度值共16位,先读低字节
        tmh=Ds18b20ReadByte();                //再读高字节
        temp=tmh;
        temp<<=8;
        temp|=tml;
        return temp;
}

#ifndef __TEMP_H_#define __TEMP_H_
#include<reg51.h>
sbit DQ=P3^7;
void Delay1ms(unsigned int );unsigned char Ds18b20Init();void Ds18b20WriteCom(unsigned char com);unsigned char Ds18b20ReadByte();void  Ds18b20ChangTemp();void  Ds18b20ReadTempCom();int Ds18b20ReadTemp();
#endif



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。