Пример #1
0
void
ps2_init(void) 
{
  bitcount = 11;
  parity = 1;
  key.num = 0;

  /* Enable the interrupt for the clock pin, because the keyboard is clocking
   * us, not vice versa 
   */
  PCICR |= _BV(PS2_PCIE);
  PS2_PCMSK |= PIN_BV(PS2_CLOCK);

  DDR_CONFIG_IN(PS2_DATA);
  DDR_CONFIG_IN(PS2_CLOCK);
  PIN_CLEAR(PS2_DATA);
  PIN_CLEAR(PS2_CLOCK);
}
Пример #2
0
static void
dht_read(void)
{
  PIN_SET(DHT);
  _delay_us(30);
  DDR_CONFIG_IN(DHT);

  /* Read in timingss, which takes approx. 4,5 milliseconds.
   * We do not disable interrupts, because a failed read is outweighed
   * by a non-serviced interrupt. Please never enclose the for-loop
   * with an ATOMIC_BLOCK! */

  uint8_t last_state = PIN_BV(DHT);
  uint8_t j = 0;
  uint8_t data[5];
  for (uint8_t i = 0; i < MAXTIMINGS; i++)
  {
    uint8_t counter = 0;
    uint8_t current_state;
    while (last_state == (current_state = PIN_HIGH(DHT)))
    {
      _delay_us(5);
      if (++counter == 20)
      {
        DHT_DEBUG("read timeout, edge=%u", i);
        return;                 /* timeout in conversation */
      }
    }
    last_state = current_state;

    /* ignore first three transitions */
    if ((i >= 4) && (i % 2 == 0))
    {
      /* shift each bit into the storage bytes */
      data[j / 8] <<= 1;
      if (counter > 7)          /* 7*5=35us */
        data[j / 8] |= 1;
      j++;
    }
  }

  /* check we read 40 bits and that the checksum matches */
  if ((j < 40) ||
      (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)))
  {
    DHT_DEBUG("read failed, bits=%u, %02X %02X %02X %02X %02X",
              j, data[0], data[1], data[2], data[3], data[4]);
    return;
  }

  int16_t t;
#if DHT_TYPE == DHT_TYPE_11
  t = data[2];
  t *= 10;
  dht_global.temp = t;
  t = data[0];
  t *= 10;
  dht_global.humid = t;
#elif DHT_TYPE == DHT_TYPE_22
  t = data[2] << 8 | data[3];
  if (t & 0x8000)
  {
    t &= ~0x8000;
    t = -t;
  }
  dht_global.temp = t;
  t = data[0] << 8 | data[1];
  dht_global.humid = t;
#endif
  DHT_DEBUG("t=%d, h=%d%%", dht_global.temp, dht_global.humid);
}