문제

아래에서 이러한 기능을 정의했으며 Device_ID 기능을 요청하면 가족 코드의 기능을 요청하면 FF (28이어야 함) 만 가져야합니다. 모든 "ones". 슬레이브 장치 기능 감지가 필요한대로 작동하는 것 같습니다 .... 내가 슬레이브를 연결하면 그는 내가 여기 있다고 말하고, 내가 그것을 가져 가면 ... 장치가 없다고 말합니다. 또한 1 와이어에서 5kohm 풀업이 있습니다.

그리고 나는 내 device_id가 작동하지 않는 이유에 대한 단서가 없기 때문에 내 질문은 왜 작동하지 않는가!? 이 코드 내에서 잘못된 것을 볼 수 있습니까 (PIC 18F87J50 및 하나의 DS18B20을 사용하고 있습니다) :

메인의 내 진술 :

     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();     // 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;}

그리고 이제 중요한 장치 _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;}
도움이 되었습니까?

해결책 2

결함은 코드 자체가 아니 었습니다. RC 필터, 10nf의 작은 도체 및 1k 옴의 저항이있는 디지털 포트에 연결하려고했습니다.

나는 나중에까지 이것을 몰랐다. 응답에 감사드립니다. 도움이되었습니다.

다른 팁

다음은 DS18B20을 읽기 위해 PIC18에 쓴 몇 가지 코드입니다.

/****************************************************************************
* temperature.h
****************************************************************************/

#ifndef TEMP_H
#define TEMP_H

extern double read_temp(void) ;
extern void start_temp(void);

#endif

/****************************************************************************
* temperature.c
****************************************************************************/

void     reset_ow(void);
void     write_ow(uint8_t  b);
uint8_t  read_ow (void);

#define OW_TEMP_SIG LATHbits.LATH0
#define OW_TEMP_TRIS TRISHbits.TRISH0
#define OW_TEMP_SIG_IN PORTHbits.RH0
#define DIR_OUT 0
#define DIR_IN 1

void start_temp(void) 
{
   uint8_t i;

   OW_TEMP_SIG=1;
   OW_TEMP_TRIS=DIR_OUT;
   for ( i=0;i<100;i++)
   {
       Delay_us(100);
   }
   reset_ow();
   write_ow(0xcc); // skip rom
   write_ow(0x44); // start t conv
}

double read_temp(void) 
{
   double temp=0;
   S16 itemp;

   reset_ow();
   write_ow(0xcc); // skip rom
   write_ow(0xbe); // read scratch pad
   itemp=read_ow();
   itemp|=(S16)read_ow()<<8;

   temp = itemp*(0.0625);
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   return temp; 
}


void reset_ow(void)
{
   OW_TEMP_TRIS=DIR_OUT;
   OW_TEMP_SIG=0;
   Delay_us(250);
   Delay_us(250);
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   Delay_us(250);
   Delay_us(250);
}

void write_ow(uint8_t b)
{
   uint8_t i;

   OW_TEMP_SIG=1;
   OW_TEMP_TRIS=DIR_OUT;
   for ( i=0;i<8;i++)
   {
      OW_TEMP_SIG=0;
      if ( b & 0x01 )
      {
         Delay_us(10);
         OW_TEMP_SIG=1;
      }
      Delay_us(70);
      OW_TEMP_SIG=1;
      Delay_us(10);
      b >>= 1;
   }
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
}

uint8_t read_ow(void)
{
   uint8_t b=0;
   uint8_t m;
   uint8_t i;

   m=1;
   for ( i=0;i<8;i++)
   {
      OW_TEMP_SIG=1;
      OW_TEMP_TRIS=DIR_OUT;
      OW_TEMP_SIG=0;        
      Delay_us(8);
      OW_TEMP_TRIS=DIR_IN;
      OW_TEMP_SIG=1;
      Delay_us(15);

      if ( 1 == OW_TEMP_SIG_IN )
      {
         b |= m;
      }
      m <<=1;
      Delay_us(60);
   }
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   return b;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top