⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds182x.c

📁 ds18b20 thermometer with lcd, ds18b20 thermometer with lcd, ds18b20 thermometer with lcd
💻 C
📖 第 1 页 / 共 2 页
字号:
        x |= TM_Read_bit();

        if (!x)
        {
            if (i > lst_dif)
                tm_rombit = 0;
            else
            if (i == lst_dif)       /* to device where conflict was last time use 1 now (it was 0 for previous device) */
                tm_rombit = 1;
            else
                tm_rombit = *ptr & mask;
                                    /* seed with bit value of previously found device */

            if (!tm_rombit)         /* last bit not set position */
                tm_lst0 = i;
        }
        else
        #if 0   /* did not worked (against spec ?) */
            tm_rombit = (x & 0x02);  /* or (x & 1) ^ 1 (inverted LSb) */
        #else
            tm_rombit = (x & 1) ^ 1; /* inverted LSb */
        #endif

        if (tm_rombit)
            *ptr |= mask;
        else
            *ptr &= ~mask;

        TM_Write_bit(tm_rombit);
    }

    TM_Exit_Critical();     /* reenable interrupts */

    #if DS_DEBUG
     hex_dump((char *)&tm_romdta, sizeof(tm_romdta));
    #endif

    return (x == 3) ? 0 : 1 + (lst_dif == tm_lst0);
}

/*
 *  TM_Crc - calculate CRC (Cyclic Redundancy Check) for
 *           Dallas Semiconductor Touch Memory.
 */
u_char TM_Crc(u_char crc, u_char inp)
{
    u_char inp_z = inp,
           i = 8,
           carry;

    do
    {
        inp ^= crc;
        crc >>= 1;

        if (inp & 1)
        {
            crc ^= (0x18 >> 1);
            crc |= 0x80;
        }

        carry = inp_z & 1;
        inp_z >>= 1;

        if (carry)
            inp_z |= 0x80;
        inp = inp_z;

    } while (--i);

    return crc;
}

/*
 *  TM_WRITE_BYTE - writes a byte to the one-wire bus.
 */
static void TM_Write_byte(u_char val)
{
    u_char cnt = 8;

    do
    {
      TM_Write_bit ((val & 1));
      val >>= 1;
    } while (--cnt);
}

/*
 *  TM_READ_BYTE - reads a byte from the one-wire bus.
 */
static u_char TM_Read_byte(void)
{
    u_char cnt = 8;
    u_char value = 0;

    do
    {
        value >>= 1;
        value |= (TM_Read_bit()) ? 0x80 : 0x00;         /* read one bit */
    } while (--cnt);

    return value;
}

/*
 *  TM_WRITE_BIT - writes a byte to the one-wire bus, passed in u_charval.
 */
static void TM_Write_bit(u_char bitval)
{
    sbi(DS_DDR, DQ);        /* set DQ pin direction - output */
    cbi(DS_PORT, DQ);       /* pull DQ low to start timeslot */

//COMPRESS_DISABLE;
//     _NOP();                /* wait 68 ns */
//    _NOP();                /* wait 68 ns */
//     _NOP();                /* wait 68 ns */
//COMPRESS_REENABLE;

	_delay_us(0.2); //delay 200ns
	 
    if( bitval ) sbi(DS_PORT, DQ); /* set DQ */
    else cbi(DS_PORT, DQ);

    //TM_Delay(3);            /* hold value for remainder of timeslot */
	_delay_us(45); //delay 3*15us

    sbi(DS_PORT, DQ);       /* DQ = 1 */
    //TM_Delay(1);            /* finish timeslot */
	_delay_us(15); //delay 15us
}

/*
 *  TM_READ_BIT - reads a byte from the one-wire bus.
 */
static u_char TM_Read_bit(void)
{
    u_char val;

    sbi(DS_DDR, DQ);        /* set DQ pin direction - output */
    cbi(DS_PORT, DQ);       /* pull DQ low to start timeslot */

//COMPRESS_DISABLE;
//     _NOP();                /* wait 68 ns */
//     _NOP();                /* wait 68 ns */
//     _NOP();                /* wait 68 ns */
//COMPRESS_REENABLE;

	_delay_us(0.2); //delay 200ns

    sbi(DS_PORT, DQ);       /* then return high DQ = 1 */

    //TM_Delay(1);            /* wait 15 us */
	_delay_us(15); //delay 15us

    cbi(DS_DDR, DQ);        /* set DQ pin direction - input */
    sbi(DS_PORT, DQ);       /* enable AVR internal pull-up resistor */

    bit_is_set(DS_PIN, DQ) ? (val = 1) : (val = 0); /* read DQ status */

    //TM_Delay(4);            /* wait for end of timeslot */
	_delay_us(60); //delay 4*15us

    return val;             /* return value of DQ line */
}

/*
 *  TM_RESET - the initialization sequence (reset pulse and presence pulse(s)).
 *
 *  Note: All transactions on the 1-Wire bus begin with an initialization sequence.
 */
static u_char TM_Reset(void)
{
    u_char presence;

    sbi(DS_DDR, DQ);        /* set DQ pin direction - output */

    cbi(DS_PORT, DQ);       /* pull DQ line low */

    //TM_Delay(32);           /* leave it low for 480us */
	_delay_us(480); //delay 480us

                            /* allow DQ line to return high */
    cbi(DS_DDR, DQ);        /* set DQ pin direction - input */
   // sbi(DS_PORT, DQ);       /* enable AVR internal pull-up resistor */

   // sbi(DS_PORT, DQ);
   // TM_Delay(4);            /* wait for presence */
   _delay_us(60); //delay 4*15us

                            /* get presence signal */

   if(bit_is_set(DS_PIN, DQ))
   {
   presence = 1;
   }
   else
   {
   presence = 0;
   }						
//    bit_is_set(DS_PIN, DQ) ? (presence = 1) : (presence = 0);

    //TM_Delay(20);           /* wait for end of timeslot */
	_delay_us(300); //delay 20*15us

    return presence;        /* presence signal returned
                             * 0 = presence, 1 = no part
                             */
}

/*
 *  TM_Delay()
 *
 *  approximately 15us delay - this is vital for DQ !!
 *                             change number of nops to adjust the delay
 *
 */



void TM_Delay(u_char cnt)
{
  /* 14,7456 MHz ... 1x nop ... 68 ns */
 
  #define Delay1us()  _NOP()

  /*#define Delay1us()  _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP();  \
                      _NOP()*/

COMPRESS_DISABLE;
  do
  { Delay1us();       /* 1us */
    Delay1us();       /* 2us */
    Delay1us();       /* 3us */
    Delay1us();       /* 4us */
    Delay1us();       /* 5us */
    Delay1us();       /* 6us */
    Delay1us();       /* 7us */
    Delay1us();       /* 8us */
    Delay1us();       /* 9us */
    Delay1us();       /* 10us */
    Delay1us();       /* 11us */
    Delay1us();       /* 12us */
    Delay1us();       /* 13us */
    Delay1us();       /* 14us */
    Delay1us();       /* 15us */
  }while (--cnt);
COMPRESS_REENABLE;
}



/*
 *  TM_Init()
 *
 *    This is here to initialize the 1-Wire bus devices.
 *
 *   Returns:
 *    > 0  ...  Number of appeared thermometeres on the 1-Wire bus.
 *      0  ...  Thermometer not present.
 */
u_char TM_Init(void)
{
  u_char tm_cnt = 0;

  #if !MULTI_DEVICE && DS_DEBUG
   TM_Read_rom(&tm_romdta[0]);
   hex_dump((char *)tm_romdta, 8);
  #endif

  /* TM_Search_rom() returns 0x80 if thermometer not connected */

  tm_cnt = 0x7F & TM_Search_rom(&tm_romdta[0]);

  if( tm_cnt )
  { /* here you can check if TM_Crc() works */

    #if 0
    { u_char  i, crc = 0;

      for (i = 0; i < 7; i++) crc = TM_Crc(crc, tm_romdta[i]);

      printf_P(PSTR("\ncrc:%X,%X\n"), crc, tm_romdta[7]);
    }
    #endif

    /* start conversion on all thermometers */
    TM_Sample_temperature(0xFF);
  }

  return tm_cnt;
}

/*
 *  TM_Scan()
 *
 *    If e.g. 1 thermometer is unplugged this can be called to refresh address table.
 *
 */
#if 0                     /* function is not currently used */
void TM_Scan(void)
{
  /* TM_Search_rom() returns 0x80 if thermometer not connected */
  return 0x7F & TM_Search_rom(&tm_romdta[0]);
}
#endif

/*
 * Display some debugging information - ascii/hex dump.
 */
#if DS_DEBUG
void hex_dump (u_char *buf, u_int length)
{ u_char abuf[19] = "[                ]\x0";	/* ascii buffer */
  u_int acnt = 1;								/* ascii counter */

  while(length--)
  {
	printf_P(PSTR("%02X "), *buf);

	if(*buf > ' ') abuf[acnt] = *buf;
	else abuf[acnt] = ' ';

	buf++;

	if(acnt++ == (sizeof(abuf)-3))
	{ printf_P(PSTR("   %s\n"), abuf);
   	  acnt = 1;
	}
  }

  if(acnt>1)
  { do
	{ printf_P(PSTR("   "));
	  abuf[acnt] = ' ';
	}while ( acnt++ < (sizeof(abuf)-3) );

    printf_P(PSTR("   %s\n"), abuf);
  }
}
#endif

#endif
/* ---------------------------- End Of File ------------------------------ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -