DS18B20数码管显示温度不对,求解,以下源码

发布于:2022-12-21 ⋅ 阅读:(440) ⋅ 点赞:(0)

#include<intrins.h>
void Delay500us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    i = 227;
    while (--i);
}
void Delay100us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    i = 43;
    while (--i);
}
void Delay400us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    i = 181;
    while (--i);
}
void Delay55us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    i = 22;
    while (--i);
}
void Delay58us()        //@11.0592MHz
{
    unsigned char i;

    i = 24;
    while (--i);
}
void Delay5us()        //@11.0592MHz
{
}
void Delay750ms()        //@11.0592MHz
{
    unsigned char i, j, k;

    _nop_();
    i = 6;
    j = 65;
    k = 181;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}
void Delay1000ms()        //@11.0592MHz
{
    unsigned char i, j, k;

    _nop_();
    i = 8;
    j = 1;
    k = 243;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}
 

#ifndef __DELAY_H__
#define __DELAY_H__
void Delay500us();
void Delay100us();
void Delay400us();
void Delay55us();
void Delay58us();
void Delay5us();
void Delay750ms();
void Delay1000ms();
#endif
#include <REGX52.H>
#include<intrins.h>
#include"delay.h"
sbit DS18B20_IO=P3^7;
unsigned char DS18B20_init()
{
    unsigned char ack=1;
    DS18B20_IO=1;
    DS18B20_IO=0;
    Delay500us();
    DS18B20_IO=1;    
    Delay100us();
    ack=DS18B20_IO;
    Delay400us();
    DS18B20_IO=1;
return ack;
}
void DS18B20_write_bit(unsigned char sendbit)
{
    DS18B20_IO=0;
    Delay5us();
    DS18B20_IO=sendbit;
    Delay55us();
    DS18B20_IO=1;
    _nop_();     //delay 2us
}
unsigned char DS18B20_read_bit()
{
    unsigned char readbit;
    DS18B20_IO=0;
    _nop_();     //delay 2us
    DS18B20_IO=1;
    _nop_();     //delay 2us
    readbit=DS18B20_IO;    
    Delay58us();
    DS18B20_IO=1;
    _nop_();     //delay 2us
return readbit;
}
void DS18B20_write_byte(unsigned char sendbyte)
{
void DS18B20_write_bit(unsigned char sendbit);  
    unsigned char i;
    for(i=0;i<8;i++)
    {
    DS18B20_write_bit(sendbyte&(0x01<<i));
    }    
}
unsigned char DS18B20_read_byte()
{
unsigned char DS18B20_read_bit();
    unsigned char i,rb,readbyte=0x00;
    for(i=0;i<8;i++)
    {
    if(rb=(DS18B20_read_bit())) readbyte|=0x01<<i;
    }
return readbyte;
}
 
#ifndef __DS18B20_BASICSETUP_H__
#define __DS18B20_BASICSETUP_H__
unsigned char DS18B20_init();
void DS18B20_write_byte(unsigned char sendbyte);
unsigned char DS18B20_read_byte();
#endif
#include"DS18B20_basicsetup.h"
#include<intrins.h>
#include"delay.h"
#define DS18B20_SKIPROM 0XCC
#define DS18B20_CONVERT_TEMPERATURE 0X44
#define DS18B20_WRITE_SCRATCHPAD 0X4E
#define DS18B20_READ_SCRATCHPAD 0XBE
#define DS18B20_COPY_SCRATCHPAD 0X48
void DS18B20_convert_t()
{
    unsigned char n=1;
    DS18B20_init();
    DS18B20_write_byte(DS18B20_SKIPROM);
    DS18B20_write_byte(DS18B20_CONVERT_TEMPERATURE);
    Delay1000ms();
}
float DS18B20_read_t()
{
    unsigned char n=1,T_LSB,T_MSB;
    int t;
    float T;
    DS18B20_init();
    DS18B20_write_byte(DS18B20_SKIPROM);
    DS18B20_write_byte(DS18B20_READ_SCRATCHPAD);
    T_LSB=DS18B20_read_byte();
    T_MSB=DS18B20_read_byte();
    t=(T_MSB<<8)|T_LSB;
    T=t/16.0;
return T; 
}
#ifndef __DS18B20_ORDER_H__
#define __DS18B20_ORDER_H__
void DS18B20_convert_t();
float DS18B20_read_t();
#endif
#include <REGX52.H>
unsigned char timer_0[8]={1,2,3,4,5,6,7,8};
void time_init()
{
    TMOD=TMOD&0xf0;
    TMOD=TMOD|0X01;
    EA=1;
    ET0=1;
    TR0=1;
    TF0=0;
    TH0=64536/256;
    TL0=64536%256;
}
void lednumscan()
{
void displaynum(unsigned char loc,unsigned char n);
    static unsigned char i;
    displaynum(i,timer_0[i]);
    i++;
    if(i>=8) i=0;
}
void displaynum(unsigned char loc,unsigned char n)
{
    unsigned char numtens[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; //p0    0~9    and off and -
    unsigned char numindiv[10]={0xbf,0x86,0xdb,0xcf,0xe6,0x6d,0xfd,0x87,0xff,0xef};//p0 0.~9.
    unsigned char location[8]={0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c};     //p2    0~7     from right to left
    P0=0x00;
    P2=location[loc];
    P0=numtens[n];
    if(timer_0[7]==1&&P2==location[7]) P0=numtens[11];
    if(timer_0[7]==0&&P2==location[7]) P0=numtens[10];
    if(P2==location[4]) P0=numindiv[n];
}
void display_T(float T)
{
    long num;
    if(T<0)  {T=-T;timer_0[7]=1;}
    else timer_0[7]=0;
    num=(long)(T*10000);
    timer_0[6]=(unsigned char)(num/1000000);
    timer_0[5]=(unsigned char)(num%1000000/100000);
    timer_0[4]=(unsigned char)(num%100000/10000);
    timer_0[3]=(unsigned char)(num%10000/1000);
    timer_0[2]=(unsigned char)(num%1000/100);
    timer_0[1]=(unsigned char)(num%100/10);
    timer_0[0]=(unsigned char)(num%10);    
}
    
#ifndef __TEMPERATURE_DISPLAY_H__
#define __TEMPERATURE_DISPLAY_H__
void time_init();
void lednumscan();
void display_T(float T);
#endif

#include <REGX52.H>
#include"DS18B20_order.h"
#include"temperature_display.h"

void main()
{
    float temperature;

    time_init();

    while(1)
    {
          DS18B20_convert_t();    
        temperature=DS18B20_read_t();
        display_T(temperature);
    }
}

void timer0() interrupt 1
{
    static unsigned char n;
    TH0=64536/256;
    TL0=64536%256;
    n++;
    if(n==2) {n=0;lednumscan();}    
}

主函数


 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到