那位老师帮看看,使用串口监控读出来的总是显示<793,程序如下;
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
uint temper;
uchar a1,a2,a3,a4;
#define DQ RA1
#define DQ_HIGH() TRISA1=1
#define DQ_LOW() TRISA1=0;DQ=0
#define nop asm("nop")
__CONFIG(0X3B31);
void init();
void reset();
void write_byte(uchar date);
uchar read_byte();
void get_tem();
void delayus(uint x,uchar y);
void delay(uint x);
void send();
void main()
{
init();
while(1)
{
get_tem();
send();
RA5=!RA5;//指示灯
}
}
void reset()
{
uchar st=1;
while(st)
{
DQ_LOW();
delayus(70,30);//740
DQ_HIGH();
delayus(4,4);//70
if(DQ==1)
st=1;
else
st=0;
delayus(40,15);//500
}
}
void write_byte(uchar date)
{
uchar i,temp;
for(i=8;i>0;i--)
{
temp=date&0x01;//01010101
DQ_LOW();
delayus(0,0);//20
if(temp==1)
DQ_HIGH();
delayus(2,2);//44
DQ_HIGH();
NOP();NOP();//
date=date>>1;//00101010
}
}
uchar read_byte()
{
uchar i,date=0;
static bit j;
for(i=8;i>0;i--)
{
date=date>>1;
DQ_LOW();
nop;nop;nop;nop;nop;nop;
DQ_HIGH();
nop;nop;nop;nop;
j=DQ;
if(j==1)
date=date|0x80;
delayus(1,1);
}
return (date);
}
void get_tem()
{
uchar tem1,tem2;
float aaa;
reset(); //复位
write_byte(0xCC);//跳过ROM
write_byte(0x44);//温度转换
delay(1000);
reset();
write_byte(0xCC);
write_byte(0xBE);
tem1=read_byte();
tem2=read_byte();
nop;nop;
DQ_HIGH(); //释放总线
aaa=(tem2*256+tem1)*6.25;
temper=(int)aaa;
a1=temper/1000;
a2=temper%1000/100;
a3=temper%100/10;
a4=temper%10;
}
void delayus(uint x,uchar y)
{
uint i;
uchar j;
for(i=x;i>0;i--);
for(j=y;j>0;j--);
}
void init()//相关寄存器初始化
{
TXSTA=0X24;
RCSTA=0X90;//连续接收
SPBRG=25; //9600波特率
TRISA5=0;
GIE=1;
RCIE=1;
ADCON1=0X07;
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void send()
{
TXREG=a1|0x30;
delay(1);
TXREG=a2|0x30;
delay(1);
TXREG=a3|0x30;
delay(1);
TXREG=a4|0x30;
delay(1);
TXREG=0X0D;
delay(1);
TXREG=0X0A;
delay(1);
while(!TRMT);
} |