问题描述
我已经定义了以下这些功能,当我询问Device_ID功能,例如的家族代码时,我只得到FF(应该是28),实际上我得到了家庭代码,48位序列号,和crc 8位都是一。它似乎像检测从设备功能工作,因为它应该....如果我连接奴隶,他说我在这里,如果我把它拿走...没有设备存在。我也有一个5kohm上拉在1wire。
我没有一个线索为什么我的Device_ID不工作,所以我的问题是为什么不工作!你可以在这段代码中看到任何错误(Im使用PIC 18F87J50和一个DS18B20):
我在Main中的陈述:
OSCTUNEbits.PLLEN = 1;
Init_Registers();
Init_Settings();
Init_Ports();
Init_Glcd();
Detect_Slave_Device();
Device_ID(0x33);
已使用的功能:
void Detect_Slave_Device(void){
uint8_t value;
OW_reset_pulse();
if(OW_reset_pulse())
{
Display_Cls();
Display_StringAt(No Device Present!,5,6);
Delay_1sec();
Delay_1sec();
} else {
Display_Cls();
Display_StringAt(Device Present!,5,6);
Delay_1sec();
Delay_1sec();
$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > uint8_t OW_reset_pulse(void){
uint8_t presence_detect;
drive_OW_low(); //开车低点
Delay_us(200); //延迟至少480微秒(我们)
Delay_us(200);
Delay_us(85);
drive_OW_high(); //释放总线
//这里应该是客户端驱动器低
Delay_us(50); //延迟50微秒(我们)
presence_detect = read_OW(); //来自slave的存在脉冲样本
Delay_us(200); //延迟435微秒(我们)
Delay_us(200);
Delay_us(35);
drive_OW_high(); //发布公共汽车
return presence_detect;}
void drive_OW_low(void){
LATHbits.LATH0 = 0;
TRISHbits.TRISH0 = 0; } //输出
void drive_OW_high(void){
LATHbits.LATH0 = 0;
TRISHbits.TRISH0 = 1;} //输入
uint8_t read_OW(void){
unsigned char read_data = 0;
TRISHbits.TRISH0 = 1;
if(1 == PORTHbits.RH0)
read_data = 1;
else
read_data = 0;
return read_data;}
现在有一些重要的device_id: p>
void Device_ID(uint8_t command){
uint8_t loop,family,checksum,ROM_CODE [8]; // 1字节CRC,6字节SERIAL,1字节系列代码
static char container [1];
OW_reset_pulse();
OW_write_byte(0x33); // Read ROM COMMAND DS18B20
for(loop = 0; loop< 9; loop ++)//每个时间1个字节= 64位
{
ROM_CODE [loop ] = OW_read_byte();
}
family = ROM_CODE [1];
checksum = ROM_CODE [8];
等等=打印我的价值 - >
void OW_write_byte(uint8_t write_data){
uint8_t loop; (循环= 0;循环< 8;循环++)的
{
OW_write_bit(write_data& 0x01); //先发送LS位
write_data>> = 1; //将下一个位的数据字节移到
}}
void OW_write_bit(uint8_t write_bit){
if(write_bit)//写1个时间段
{
Brakepoint() ; // 1 us
//写一个'1'
drive_OW_low(); //开车低点
Delay_us(6); //延迟6微秒(我们)
drive_OW_high(); //释放总线
Delay_us(64);; //延迟64微秒(我们)
}
else //写0时间段
{
Brakepoint(); // 1 us
//写一个'0'
drive_OW_low(); //开车低点
Delay_us(65); //延迟65微秒(us)
drive_OW_high(); //释放总线
Delay_us(10); //延迟10微秒恢复(我们)
}}
uint8_t OW_read_byte(void)
uint8_t loop,result = 0;
for(loop = 0; loop< 8; loop ++)
{
result>> = 1; //移动结果以使其准备好接收
if(OW_read_bit())
result | = 0x80; //如果结果为1,则设置MS-bit
}
返回结果;
uint8_t OW_read_bit(void){
uint8_t read_data; //读一点
Brakepoint(); // 1 us
drive_OW_low(); //开车低点
Delay_us(6); // delay 6微秒(us)
drive_OW_high(); //释放总线
Delay_us(9); //延迟9微秒(我们)
read_data = read_OW(); //读取OW_PIN的状态
Delay_us(55); // delay 55 microsecond(us)
return read_data;}
故障不是自己的代码,我试图将其连接到具有RC滤波器的数字端口,10nf的小导体和1k欧姆的电阻器。
我以后不知道。感谢您的回复,这很有帮助。
I have defined these functions below, and when I ask Device_ID function for example ofthe family code, I only get FF (should be 28), acctually I get both the family code, 48-bit serial, and the crc 8 bit to be all "ones". It seems like the detect slave device function works as it should.... If i connect the slave he say's I am here, and if i take it away... no device present. I also have a 5kohm pull-up at the 1wire.
And I dont have a clue why my Device_ID dont work, so my question is why doesnt it work!? can u see anything wrong within this code (Im using a PIC 18F87J50 and one DS18B20):
My statement in Main:
OSCTUNEbits.PLLEN = 1;
Init_Registers();
Init_Settings();
Init_Ports();
Init_Glcd();
Detect_Slave_Device();
Device_ID( 0x33 );
Used functions:
void Detect_Slave_Device(void){
uint8_t value;
OW_reset_pulse();
if (OW_reset_pulse())
{
Display_Cls();
Display_StringAt("No Device Present!",5,6);
Delay_1sec();
Delay_1sec();
} else {
Display_Cls();
Display_StringAt("Device Present!",5,6);
Delay_1sec();
Delay_1sec(); }}
uint8_t OW_reset_pulse(void){
uint8_t presence_detect;
drive_OW_low(); // Drive the bus low
Delay_us(200); // Delay at least 480 microsecond (us)
Delay_us(200);
Delay_us(85);
drive_OW_high (); // Release the bus
// Here should the client drive low
Delay_us(50); // Delay 50 microsecond (us)
presence_detect = read_OW(); // Sample for presence pulse from slave
Delay_us(200); // Delay 435 microsecond (us)
Delay_us(200);
Delay_us(35);
drive_OW_high (); // Release the bus
return presence_detect;}
void drive_OW_low (void){
LATHbits.LATH0 = 0;
TRISHbits.TRISH0 = 0; }// Output
void drive_OW_high (void){
LATHbits.LATH0 = 0;
TRISHbits.TRISH0 = 1;} // Input
uint8_t read_OW (void){
unsigned char read_data=0;
TRISHbits.TRISH0 = 1;
if (1 == PORTHbits.RH0)
read_data = 1;
else
read_data = 0;
return read_data;}
And now some ofthe important device_id:
void Device_ID( uint8_t command ){
uint8_t loop, family, checksum, ROM_CODE[8]; // 1 byte CRC, 6 bytes SERIAL, 1 byte Family code
static char container[1];
OW_reset_pulse();
OW_write_byte( 0x33 ); // READ ROM COMMAND DS18B20
for(loop = 0; loop < 9; loop++) // 1 byte in per time = 64-bits
{
ROM_CODE[loop] = OW_read_byte();
}
family = ROM_CODE[1];
checksum = ROM_CODE[8];
And so on = Print MY VALUES ->
void OW_write_byte (uint8_t write_data){
uint8_t loop;
for (loop = 0; loop < 8; loop++)
{
OW_write_bit(write_data & 0x01); //Sending LS-bit first
write_data >>= 1; // shift the data byte for the next bit to send
} }
void OW_write_bit (uint8_t write_bit){
if (write_bit) // Write 1 time slot
{
Brakepoint(); // 1 us
//writing a bit '1'
drive_OW_low(); // Drive the bus low
Delay_us(6); // Delay 6 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(64);; // Delay 64 microsecond (us)
}
else // Write 0 time slot
{
Brakepoint(); // 1 us
//writing a bit '0'
drive_OW_low(); // Drive the bus low
Delay_us(65); // Delay 65 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(10); // Delay 10 microsecond for recovery (us)
}}
uint8_t OW_read_byte (void)
uint8_t loop, result=0;
for (loop = 0; loop < 8; loop++)
{
result >>= 1; // shift the result to get it ready for the next bit to receive
if (OW_read_bit())
result |= 0x80; // if result is one, then set MS-bit
}
return result; }
uint8_t OW_read_bit (void){
uint8_t read_data; // Reading a bit
Brakepoint(); // 1 us
drive_OW_low(); // Drive the bus low
Delay_us(6); // delay 6 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(9); // delay 9 microsecond (us)
read_data = read_OW(); //Read the status of OW_PIN
Delay_us(55); // delay 55 microsecond (us)
return read_data;}
The fault was not the code it self, I tried to connect it to a digital port with a RC filter, a small conductor of 10nf and a resistor of 1k ohm.
I did not know this until later. Thanks for the response, it was helpful.
这篇关于1wire问题详细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!