I use arm system to write for a few days for ds18b20 program code. And finally during the simulation the temperature doesn't display. After testing program, I find a problem. I think the reading and writing program of ds18b20 is wrong. I hope someone can help me to solve it.
void write_ds18b20 uint8 (dat) / / write data
{
uint8 i;
IO0DIR|=DQ;
for(i=0;i<8;i++)
{
IO0CLR|=DQ;
delay_us(2);
if(dat&0x01)
{
IO0SET|=DQ;
delay_us(10);
} else
{
IO0CLR|=DQ;
delay_us(10);
}
delay_us(3);
dat=dat>>1;
}
IO0SET|=DQ;
}
uint8 read_ds18b20() //read data
{
uint i=0,readat=0;
for(i=0;i<8;i++)
{
IO0DIR|=DQ;
IO0CLR|=DQ;
delay2us; //1us
readat>>=1;
IO0SET|=DQ;
IO0DIR&=(~DQ); //Set port input, let the ds18b20 transmit data to the bus
if(IO0PIN&DQ) readat|=0x80; // The mainframe start sampling
delay_us(12);
}
return readat;
}