这是我用430编写的18b20测温的程序,读出的错温度数据是恒为E01F 为什么啊?
#include "msp430X42X.h"
#define CPU_F ((double)1000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define DQ_H P2DIR&=~BIT2
#define DQ_L P2DIR|=BIT2
#define DQ_0 P2OUT&=~BIT2
unsigned char temp1,temp2;
void writebit(unsigned char bit)
{
DQ_H;
DQ_L;
DQ_0;
delay_us(3);
if(bit) DQ_H;
else
{
DQ_L;
DQ_0;
}
delay_us(60);
}
unsigned char readbit()
{
char bit;
DQ_H;
DQ_L;
DQ_0;
delay_us(3);
DQ_H;
delay_us(3);
if(P2IN&BIT2) bit=1;
else bit=0;
delay_us(60);
return(bit);
}
void write_byte(unsigned char byte)
{
int i;
for(i=0;i<8;i++)
{
writebit(byte&0x01);
byte=byte>>1;
_NOP();
}
}
unsigned char read_byte()
{
int t;
unsigned char ans=0;
for(t=0;t<8;t++)
{
ans=ans<<1;
ans|=readbit();
_NOP();
}
return(ans);
}
int reset()
{
int t;
DQ_H;
_NOP();
_NOP();
DQ_L;
DQ_0;
delay_us(500);
DQ_H;
delay_us(100);
if((P2IN&BIT2)==0) t=1;
else t=0;
delay_us(480);
return(t);
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
FLL_CTL0|=XCAP18PF;
while(1)
{
while(!reset())
{
}
write_byte(0xCC);
write_byte(0x44);
delay_us(5000);
while(!reset())
{
}
write_byte(0xCC);
write_byte(0xBE);
delay_us(500000);
temp1=read_byte();
temp2=read_byte();
}
} |