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

📄 05070.c

📁 被动红外传感器开关
💻 C
📖 第 1 页 / 共 2 页
字号:
    }
    else if (uiMajorButtonTimer >= BUTTON_DEBOUNCING_TIME)  //0.1s
    {
      ucMsgButton = MSG_MAJOR_BUTTON_SHORT_PRESSED;
    }
    uiMajorButtonTimer = 0;
    ucButtonTimer2 = 0;
  }
}

void updateCDS(void)
{
  if (__adc__(ADMUX_LIGHTLEVEL) >= LIGHT_LEVEL_THRESHHOLD)
  {
    if ((++ucTimerDark) == 0)
    {
      ucTimerDark--;
    }
  }
  else
  {
    ucTimerDark = 0;
  }
}

void updateTimer(void)
{
  if (ucStatus.bf_var.OCCUPIED == 0)
  {
    if ((ucTimerBase == 0) && (uiTimerWarmup == 0))
    {
      if (uiTimerDelay > 0)
      {
        uiTimerDelay--;
      }
      else
      {
        if ((++uiTimerAfterDelay) == 0)
        {
          uiTimerAfterDelay--;
        }
      }
    }
  }
  else
  {
    __figureoutdelay__();
    uiTimerAfterDelay = 0;
  }
}

void inoutTestMode(void)
{
  if (uiTimerTestMode == 0)
  {
    uiTimerTestMode = TIME_TESTMODE_DURATION;
  }
  else
  {
    uiTimerTestMode = 0;
  }
}

void updatePIR(void)
{
  static unsigned char ucLastPIRSample = 0;
  static unsigned char ucCurrentPIRSample = 0;
  static unsigned char ucThreshHold = MIN_THRESH;
  static unsigned int ucLastLastHighLength = 0;
  static unsigned int ucLastHighLength = 0;
  static unsigned int ucCurHighLength = 0;
  static unsigned int ucLastLowLength = 0;
  static unsigned int ucCurLowLength = 0;
  ucStatus.bf_var.SPACEOCCUPIED = 0;
  ucLastPIRSample = ucCurrentPIRSample;
  ucCurrentPIRSample = __adc__(ADMUX_OCCUPATION);
  if (ucCurrentPIRSample > MAX_PIR)
    ucCurrentPIRSample = MAX_PIR;
  if (ucCurrentPIRSample >= PIR_CENTER)
    ucCurrentPIRSample -= PIR_CENTER;
  else
    ucCurrentPIRSample = PIR_CENTER - ucCurrentPIRSample;
  if ((0 == uiTimerWarmup) && (0 == ucTimerIgnore))
  {
    if (ucCurrentPIRSample >= ucThreshHold)
    {
      if (ucLastPIRSample < ucThreshHold)
      {
        ucLastLastHighLength = ucLastHighLength;
        ucLastHighLength = ucCurHighLength;
        ucCurHighLength = 1;
      }
      else if (ucCurHighLength > 0)
      {
        ucCurHighLength++;
        if (ucCurHighLength >= MID_WIDTH)
        {
          ucStatus.bf_var.SPACEOCCUPIED = 1;
          ucCurHighLength = 0;
        }
      }
    }
    else
    {
      if (ucLastPIRSample >= ucThreshHold)
      {
        if (((ucLastHighLength >= MIN_WIDTH) && (ucCurHighLength >= MIN_WIDTH) && ((ucLastHighLength >= MIN_SPACE) || (ucCurHighLength >= MIN_SPACE)) && (ucLastHighLength + ucCurLowLength + ucCurHighLength <= MAX_WIDTH)) || ((ucLastLastHighLength >= MIN_WIDTH) && (ucLastHighLength >= MIN_WIDTH) && (ucCurHighLength >= MIN_WIDTH) && (ucLastLastHighLength + ucLastLowLength + ucLastHighLength + ucCurLowLength + ucCurHighLength <= MAX_WIDTH)))
        {
          ucStatus.bf_var.SPACEOCCUPIED = 1;
          ucCurLowLength = 0;
          ucCurHighLength = 0;
        }
        else
        {
          ucLastLowLength = ucCurLowLength;
          ucCurLowLength = 1;
        }
      }
      else if (ucCurLowLength > 0)
        ucCurLowLength += (ucCurLowLength >= MAX_WIDTH) ? 0 : 1;
    }
  }
  else if (ucTimerBase == 0)
  {
    if (uiTimerWarmup > 0)
    {
      uiTimerWarmup--;
    }
    if (ucTimerIgnore > 0)
    {
      ucTimerIgnore--;
    }
  }
}

void updateStatus(void)
{
  switch (ucStatus.uc_var & STATUS_MASK)
  {
    case STATUS_MANUALOFF:
      if (ucMsgButton == MSG_MAJOR_BUTTON_SHORT_PRESSED)
      {
        goLoadOn();
      }
      else
      {
        if ((uiTimerTestMode > 0) || ((ucMode == MODE_U) && (uiTimerAfterDelay >= TIME_RESET_TO_AUTO)))
        {
          goAutoOff();
        }
      }
      break;
    case STATUS_AUTOOFF:
      if ((uiTimerTestMode == 0) && (ucMsgButton == MSG_MAJOR_BUTTON_SHORT_PRESSED))
      {
        goLoadOn();
      }
      else if (ucStatus.bf_var.SPACEOCCUPIED == 1) //&& (ucMsgButton != MSG_MAJOR_BUTTON_SHORT_PRESSED))
      {
      	if (ucTimerDark == 0xff)
      	{
      	  goLoadOn();
      	}
      	else
      	{
          goOnIfDark();
        }
      }
      else
      {
        if ((ucMode == MODE_BA) && (uiTimerAfterDelay >= TIME_RESET_TO_MANUAL) && (uiTimerTestMode == 0))
        {
          goManualOff();
        }
      }
      break;
    case STATUS_LOADON:
      if ((uiTimerTestMode == 0) && (ucMsgButton == MSG_MAJOR_BUTTON_SHORT_PRESSED))
      {
        goManualOff();
      }
      else
      {
        if (uiTimerDelay == 0)
        {
          goAutoOff();
        }
      }
      break;
    case STATUS_ONIFDARK:
      if (((uiTimerTestMode == 0) && (ucMsgButton == MSG_MAJOR_BUTTON_SHORT_PRESSED)) || (ucTimerDark == 0xff))
      {
        goLoadOn();
      }
      else
      {
        if (uiTimerDelay == 0)
        {
          goAutoOff();
        }
      }
      break;
    default:
      break;
  }
}

void goLoadOn(void)
{
  ucStatus.bf_var.RELAYBUFFER = 1;
  ucStatus.uc_var &= ~STATUS_MASK;
  ucStatus.uc_var |= STATUS_LOADON;
}

void goManualOff(void)
{
  ucStatus.bf_var.RELAYBUFFER = 0;
  ucStatus.uc_var &= ~STATUS_MASK;
  ucStatus.uc_var |= STATUS_MANUALOFF;
}

void goAutoOff(void)
{
  ucStatus.bf_var.RELAYBUFFER = 0;
  ucStatus.uc_var &= ~STATUS_MASK;
  ucStatus.uc_var |= STATUS_AUTOOFF;
}

void goOnIfDark(void)
{
  ucStatus.uc_var &= ~STATUS_MASK;
  ucStatus.uc_var |= STATUS_ONIFDARK;
}

void updateLED(void)
{
  static unsigned char ucTCCR2A = TCCR2A_OFF;
  static unsigned char ucOCR2B = OCR2B_DIM;
  static unsigned char ucLEDTimer = 0;
  switch (ucMsgButton)
  {
    default:
      if (ucMsgButton == MSG_MAJOR_BUTTON_INOUTTESTMODE_HOLD)
      {
        ucOCR2B = OCR2B_ON;
        if ((ucTimerBase == 0) && ((ucTimerSystem & 0x01) == 0))
        {
          ucTCCR2A ^= (1<<5); //toggle LED on/off
        }
      }
      else if (ucMsgButton == MSG_MAJOR_BUTTON_LONG_HOLD)
      {
        ucOCR2B = OCR2B_ON;
        ucTCCR2A = TCCR2A_ON;
        if (uiTimerTestMode == 0)
        {
          ucTCCR2A = TCCR2A_OFF;
        }
      }
      else
      {
        if ((ucLEDTimer == 0) && (uiTimerTestMode > 0) && (ucStatus.bf_var.SPACEOCCUPIED == 1))
        {
          ucLEDTimer = TIME_LED_FLASH_DURATION;
        }
        if (ucLEDTimer > (TIME_LED_FLASH_DURATION - TIME_LED_ON_DURATION))
        {
          ucOCR2B = OCR2B_ON;
          ucTCCR2A = TCCR2A_ON;
        }
        else if (ucLEDTimer > 0)
        {
          ucTCCR2A = TCCR2A_OFF;
        }
        else  //ucLEDTimer == 0
        {
          ucOCR2B = OCR2B_DIM;
          ucTCCR2A = TCCR2A_OFF;
          if ((ucStatus.bf_var.RELAYBUFFER == 0) && (OUTPUT_RELAY_CONTROL == 0))
          {
            ucTCCR2A = TCCR2A_ON;  //turn on the LED when load is Off
          }
        }
        if (ucLEDTimer > 0)
        {
          if (ucTimerBase == 0)
          {
            ucLEDTimer--;
          }
        }
      }
      break;
  }
  TCCR2A = ucTCCR2A;
  OCR2B = ucOCR2B;
}

void lineSync(void)
{
  unsigned char ucLineSense = 0;
  __watchdog_reset();
  while (ucLineSense != 0xff)
  {
    ucLineSense = (ucLineSense << 1) + INPUT_LINE;
  }
  while (ucLineSense != 0xf0)
  {
    ucLineSense = (ucLineSense << 1) + INPUT_LINE;
  }
  if (ucTimerUnit == 0)
  {
    ucTimerUnit = 0xff;
    TCNT1 = 0;
    GTCCR |= (1 << PSR10);
    TCCR1B = TCCR1B_INIT;
  }
  else if (ucTimerUnit == 0xff)
  {
    uiFullCycle = TCNT1;
    TCCR1B = TCCR1B_OFF;
    if ((uiFullCycle < FULL_CYC_60 + 24) && (uiFullCycle > FULL_CYC_60 - 32))
    {
      ucTimerUnit = 6;
    }
    else if ((uiFullCycle < FULL_CYC_50 + 24) && (uiFullCycle > FULL_CYC_50 - 32))
    {
      ucTimerUnit = 5;
    }
    else
    {
      ucTimerUnit = 0;
    }
  }
  else
  {
    ucStatus.bf_var.LINEFREQ = 1;
    if ((++ucTimerBase) == ucTimerUnit)
    {
      ucTimerBase = 0;
      if ((++ucTimerSystem) == 10)
      {
        ucTimerSystem = 0;
      }
    }
  }
}

unsigned char __adc__(unsigned char ucTmpADCMUX)
{
  ADMUX = ucTmpADCMUX;
  ADCSRA = ADCSRA_INIT;
  ADCSRA = ADCSRA_START;
  while (ADCSRA_Bit6 == 1)
    ;
  ADCSRA = ADCSRA_OFF;
  return ADCH;
}

void __figureoutdelay__(void)
{
  uiTimerDelay = 0;
  if ((STATUS_LOADON == (ucStatus.uc_var & STATUS_MASK)) || (STATUS_ONIFDARK == (ucStatus.uc_var & STATUS_MASK)))
  {
    if (uiTimerTestMode > 0)
      uiTimerDelay = TIME_TESTMODE_DELAY;
    else
    {
      uiTimerDelay = uiDelayData[ucMode];
    }
  }
  return;
}

void delayandswitch(void)
{
  static unsigned int uiOnCalibration = 0xffff;
  static unsigned int uiOffCalibration = 0xffff;
  TCNT1 = 0;
  GTCCR |= (1 << PSR10);
  TCCR1B = TCCR1B_INIT;
  if (uiOffDelay >= uiFullCycle - 15)
  {
    uiOffDelay = DFT_OFF_DELAY;
  }
  if (uiOnDelay >= uiFullCycle - 15)
  {
    uiOnDelay = DFT_ON_DELAY;
  }
  uiTemp = uiOffDelay;
  if (ucStatus.bf_var.RELAYBUFFER)
  {
    uiTemp = uiOnDelay;
  }
  while (TCNT1 < uiTemp)
    ;
  OUTPUT_RELAY_CONTROL = ucStatus.bf_var.RELAYBUFFER;
  if (ucStatus.bf_var.RELAYBUFFER)  //turning On
  {
    uiTemp = uiOnCalibration;
    uiOnCalibration--;
    while (TCNT1 < uiFullCycle - 1)
    {
      if (INPUT_LOAD == 1)
      {
        uiTemp = uiFullCycle - TCNT1;
        uiOnCalibration = 0xffff;
        break;
      }
    }
    uiOnDelay += uiTemp;
  }
  else  //turning Off
  {
    while (INPUT_LINE == 0)
      ;
    while (INPUT_LOAD == 0)
    {
      if (TCNT1 >= (uiFullCycle >> 1) + 8)
      {
        break;
      }
    }
    while (INPUT_LOAD == 1)
      ;
    uiTemp = uiFullCycle - TCNT1;
    __delay_cycles(200);  //delay 100us
    if (INPUT_LINE == 0)
    {
      uiTemp = uiOffCalibration;
      uiOffCalibration--;
    }
    else
    {
      uiOffCalibration = 0xffff;
    }
    uiOffDelay += uiTemp;
  }
  ucTimerIgnore = TIME_IGNORE;
  TCCR1B = TCCR1B_OFF;
}

⌨️ 快捷键说明

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