温度计读不出温度,求助了

2020-02-10 08:33发布

//lcd8bit

void delay(unsigned int i)
{
        while(i--);
}
#define RS1 RD0=1
#define RS0 RD0=0
#define RW1 RD1=1
#define RW0 RD1=0
#define E1 RD2=1
#define E0 RD2=0
#define PC PORTC

void write_cmd(uchar cmd)
{
delay(2);
RS0;
RW0;
PC=cmd;
E1;
delay(2);
E0;

}
void write_data(uchar dat)
{
delay(2);
RS1;
RW0;
PC=dat;
E1;
delay(2);
E0;

}
void lcd_init()
{
TRISC=0x00;
TRISD=0x00;
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x01);
delay(10);
}

//主程序
#include"pic.h"
#define PORTAIT(adr,bitt) ((unsigned)(&adr)*8+(bitt))
static bit DQ @ PORTAIT(PORTB,0);
static bit ISB0  @ PORTAIT(TRISB,0);
#define uchar unsigned char
#define uint unsigned int
#include"8bit1602.h"
unsigned char tab[16]={0,0,0};

void Init_DS18B20()
{
        uchar j,x=0;
        ISB0=0;
        DQ=1;
        delay(10);
        DQ=0;
        delay(200);
        DQ=1;
        delay(10);
        ISB0=1;
        delay(2);
        j=DQ;
        delay(10);
}
unsigned char ReadOneChar()
{
        unsigned char i=0;
        unsigned char dat=0;
        for(i=8;i>0;i--)
        {
                ISB0=0;
                DQ=0;
                dat>>=1;
                DQ=1;
                ISB0=1;
                if(DQ==1)
                        dat|=0x80;
                delay(2);
        }
        return dat;
}
void WriteOneChar(unsigned char dat)
{
        unsigned char i=0;
        for(i=8;i>0;i--)
        {
                ISB0=0;
                DQ=0;
                DQ=dat&0x01;
                delay(1);
                DQ=1;
                dat>>=1;
        }
        delay(2);
}
int ReadTemperature(void)
{
        uchar a=0;
        uchar b=0;
        int t=0;
        float tt=0;
        Init_DS18B20();
        WriteOneChar(0xCC); // 跳过读序号列号的操作
        WriteOneChar(0x44); // 启动温度转换
        Init_DS18B20();
        WriteOneChar(0xCC); //跳过读序号列号的操作
        WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
        a=ReadOneChar();//低位
        b=ReadOneChar();//高位
        t=b;
        t<<=8;
        t=t|a;
        tt=t*0.0625;
        t= tt*10+0.5;
        return(t);
}  
void main()
{
        uchar i;
        int temp;
        TRISC=0x00;
        RW0;
        lcd_init();       
        while(1)
        {
                write_cmd(0x80);
                temp=ReadTemperature();//读温度
                tab[0]=temp%1000/100+48;//显示百位
                tab[1]=temp%100/10+48;//显示十位       
                tab[2]='C';
                for(i=0;i<16;i++)
                {
                        write_data(tab);
                        delay(2);
                }
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
xjl1227
1楼-- · 2020-02-10 12:18
不需要了
millwood0
2楼-- · 2020-02-10 18:02
"不需要了"

you should consider taking a class in "structured programming".

or simply "programming 101".

一周热门 更多>