|
现在我的问题是,复位没问题,已经通过示波器查看波形。
写字节的波形也对,但是就是看不到DS18B20传过来的温度波形,输出信息一直是104或10c .
从网上找了很多资料,修改了很多,但是就是没有波形,各位高手麻烦看一下我的程序,帮帮忙~ 在此非常感谢。
以下是我的程序:
void Ds18b20_Delay_Unit(BYTE wUSec) //delay
{
WORD wCount; // loop counter
while (wUSec--)
{
wCount = 100; // reset loop counter
while (--wCount) ;
}
}
//--------------DS18B20 driver------------------
//write one byte
void DS18B20_WriteOneByte(UCHAR ucWriteData)
{
//int i;
UCHAR uci;
for(uci=0; uci<8; uci++)
{
P1_7=0;
P1_7=ucWriteData&0x01;
Ds18b20_Delay_Unit(3);//delay 66us
P1_7=1;
//i=10;
//while(i--);//delay 2us
ucWriteData>>=1;
}
}
//read one byte
UCHAR DS18B20_ReadOneByte(void)
{
int i;
UCHAR uci;
UCHAR ucReadData=0;
for(uci=0; uci<8; uci++)
{
P1_7=0;
ucReadData >>=1;
P1_7=1;
i=10;
while(i--);//delay 2us
ucReadData |= P1_7;
Ds18b20_Delay_Unit(3);
}
return ucReadData;
}
//initial DS18B20
void InitDS18B20(void)
{
int i;
P1_7=1;
i=5;
while(i--);
P1_7=0;
Ds18b20_Delay_Unit(30);//delay 860us
P1_7=1;
}
//read temprature
float DS18B20_GetTemperature(void)
{
UCHAR bTemperature;
UCHAR ucTemperatureLowByte;
UCHAR ucTemperatureHighByte;
//EA=0; //disable MCU interrupt
InitDS18B20();
Delay1ms(2);
DS18B20_WriteOneByte(0xCC); //skip rom
DS18B20_WriteOneByte(0x44);//convert temperature
Delay1ms(1000); //long time delay
InitDS18B20();
Delay1ms(2);
DS18B20_WriteOneByte(0xCC); //skip rom
DS18B20_WriteOneByte(0xBE);//read temperature
ucTemperatureLowByte = DS18B20_ReadOneByte();
ucTemperatureHighByte = DS18B20_ReadOneByte();
//ucTemperatureHighByte = DS18B20_ReadOneByte()<<8;
//bTemperature = (ucTemperatureHighByte | ucTemperatureLowByte) *0.0625;
bTemperature = (ucTemperatureHighByte>>4 | ucTemperatureLowByte<<4)&0x3F ;
printk("\ntemp===>%x",ucTemperatureLowByte);
printk("\ntemp===>%x",ucTemperatureHighByte);
printk("\ntemp===>%x\n",bTemperature);
//EA=1;//enable MCU interrupt
return bTemperature;
}
|
|