搜索
bottom↓
回复: 1

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

[复制链接]

出0入0汤圆

发表于 2012-5-2 15:12:35 | 显示全部楼层 |阅读模式
本帖最后由 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型,仿真正常

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2012-5-2 15:37:24 | 显示全部楼层
这是为什么呢
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 02:15

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表