找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1098|回复: 0
打印 上一主题 下一主题
收起左侧

PIC单片机DS18B20读取温度程序,可直接移植换个IO口就行

[复制链接]
跳转到指定楼层
楼主
ID:1004920 发表于 2023-4-1 16:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/////////////////////////////////////////////
//名 称 :DS18B20温度的读取
//开发者:zhangchunhua
//环 境 :PIC单片机
//时 序 :4.00 晶振
/////////////////////////////////////////////
#include<pic.h>
/////////////////////////////////////////////
#define uchar unsigned char
#define uint  unsigned int
/////////////////////////////////////////////
#define DQ  RC3
#define DQ_HIGH()  TRISC3=1
#define DQ_LOW()  TRISC3=0;DQ=0
/////////////////////////////////////////////
void reset(void);
void write_byte(uchar date);
uchar read_byte(void);
void yanshius(uint t);
void start_t(void);
signed int read_t(void);
/////////////////////////////////////////////
void yanshius(uint t)
{
        uint i;
        for(i=0;i<t;i++)
        {        NOP();        }
}
void reset(void)
{
        uchar st=1;
        while(st)
        {
                DQ_LOW();
                yanshius(29);//510uS
                DQ_HIGH();
                yanshius(2);//60uS
                if(DQ==1)
                        st=1;
                else
                {        st=0;
                         yanshius(25);//480uS
                }
        }
}
void write_byte(uchar date)
{
        uchar i,temp;
        for(i=8;i>0;i--)
        {
                temp=date & 0x01;//01010101
                DQ_LOW();
                if(temp == 1)
                {        NOP();NOP();
                        DQ_HIGH();
                        yanshius(2);       
                }
                else
                {
                        yanshius(2);//60uS
                        DQ_HIGH();       
                }       
                NOP();NOP();
                date = date>>1;//00101010yanshius(0);
        }
}
uchar read_byte(void)
{
        uchar i,date;
        static bit j;
        for(i=8;i>0;i--)
        {
                date = date>>1;
                DQ_LOW();
                NOP();NOP();
                DQ_HIGH();
                NOP();NOP();NOP();NOP();NOP();NOP();NOP();
                j = DQ;
                if(j == 1)
                {
                        date=date|0x80;//1000 0000                       
                }       
                yanshius(1);//47uS
        }
        return date;
}

void start_t(void)
{
        reset();  //复位
        write_byte(0xCC);//跳过ROM
        write_byte(0x44);//温度转换
}
//////读取的温度数值是放大10倍
signed int read_t(void)
{
        uchar tem1,tem2;
        uint tt,mm=0;
        double aaa;
        signed int temper;
        reset();
        write_byte(0xCC);
        write_byte(0xBE);
        tem1=read_byte();
        tem2=read_byte();
        tt = tem2;
        tt = tt<<8|tem1;
        if(tt&0x8000)
        {
                mm = ~tt + 1;
                aaa = mm * (-0.625);
        }
        else
        {
                aaa = tt*0.625;
        }
        temper=(signed int)aaa;
        return temper;       
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

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