jzjjzj 发表于 2012-5-2 15:12:35

proteus 仿真ds18b20,数据类型不同出问题了,跪求指教

本帖最后由 jzjjzj 于 2012-5-2 15:15 编辑

#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar num;
sbit ds=P2^3;
sbit lcden=P2^7;
sbit lcdrs=P2^5;
sbit lcdrw=P2^6;
void delayus(int z)        //Calling the routine takes about 24 μs, and then each count takes another 16μs       微秒级
        {
                int x;
                for(x=0;x<z;x++);
        }
void delayms(uint i) //毫秒级
        {
                uint x,y;
                for(x=0;x<i;x++)
                        for(y=0;y<148;y++);
        }
void ds_init()          //ds18b20初始化
        {
                uchar status ;
                ds=1;
                _nop_();
                ds=0;
                delayus(29);
                ds=1;
                delayus(5);
                status=ds;
                delayus(20);
                ds=1;
        }
void ds18b20_writebyte(uchar date)//向ds18b20写一个字节
        {
                uchar i,j;
                for(i=0;i<8;i++)
                        {
                                if(date&0x01)
                                        {
                                                ds=0;
                                                for(j=0;j<2;j++); //延时15us
                                                ds=1;//写1
                                        }
                                        else
                                                {
                                                        ds=0;//写0
                                                        for(j=0;j<2;j++);//延时15us

                                                }
                                        delayus(2);//延时55us,等待ds18b20进行采样
                                        ds=1;//释放总线
                                        _nop_();
                                        date>>=1;//数据右移一位,发下一位

                                }
                               
        }
uchar ds18b20_readbyte()//向ds18b20读一个字节
        {
                uchar i,tmp,j;
                for(i=0;i<8;i++)
                        {
                                ds=1;
                                _nop_();
                                _nop_();
                                tmp>>=1;
                                ds=0;
                                _nop_();
                                _nop_();
                                _nop_();
                                _nop_();
                                ds=1;
                                for(j=0;j<2;j++);//延时15us
                                if(ds)//开始读位
                                        tmp|=0x80;
                                        delayus(2);//延时55us

                        }
                        return tmp;       
        }
uint get_temp()//读取寄存器中存储的温度数据
        {
        //        uint temp,f_temp;
                uint temp;
                uint wl,wh;
                        ds_init();
       
                ds18b20_writebyte(0xcc);
                ds18b20_writebyte(0x44);
                delayms(5);
                ds_init();
                _nop_();
                ds18b20_writebyte(0xcc);
                ds18b20_writebyte(0xbe);
                wl=ds18b20_readbyte();
                wh=ds18b20_readbyte();
        //        temp=wh;
        //        temp<<=8;
        //        temp=temp|wl;
        //        temp=temp*0.0625;
        //        temp=temp*10;
        //                temp=temp+0.05;
        //        temp = wl / 16 + wh * 16;//实际温度值=(TH*256+TL)/16,即:TH*16+TL/16
                     //这样得出的是温度的整数部分,小数部分被丢弃了
        temp=((wh*256)+wl)*0.0625;


        return temp;}
void write_com(uchar com)        //写指令
        {
                lcdrs=0;
                lcdrw=0;
                P0=com;
                delayms(1);
                lcden=1;
                delayms(1);
                lcden=0;
                               
        }
void write_date(uchar date)       //        写数据
        {
                lcdrs=1;
                lcdrw=0;
                P0=date;
                delayms(1);
                lcden=1;
                delayms(1);
                lcden=0;
        }
void lcd_init()
        {
       
                write_com(0x38);       
                delayms(1);
                write_com(0x38);
                write_com(0x38);
                write_com(0x08);
                write_com(0x01);
                write_com(0x0c);
                write_com(0x06);               
        }
void lcd_display()
        {
                uint t;
                t=get_temp();
                write_com(0x80+0x40+10);
                write_date(t/10+0x30);
                write_date(t%10+0x30);
          write_date(0xdf);
      write_date(0x43);
        }
void main()
        {        lcd_init();
           while(1)
           {
                lcd_display();

                }
                       
        }


上面这个延时函数void delayus(int z)        //Calling the routine takes about 24 μs, and then each count takes another 16μs       微秒级
        {
                int x;
                for(x=0;x<z;x++);
        }
把int z改成uint z,int x 改成 uint x仿真出来是这样的,如果是int型,仿真正常

raxb 发表于 2012-5-2 15:37:24

这是为什么呢
页: [1]
查看完整版本: proteus 仿真ds18b20,数据类型不同出问题了,跪求指教