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

📄 hal_key.c

📁 一些基于IRA环境开发的zigbee实例程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  uint8 ksave0 = 0;
  uint8 ksave1;
  uint8 adc;
#endif

#if defined (HAL_KEY_SW_6_ENABLE)
  if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */
  {
    keys |= HAL_KEY_SW_6;
  }
#endif
#if defined (HAL_KEY_SW_5_ENABLE)
  if (HAL_KEY_SW_5_PORT & HAL_KEY_SW_5_BIT)       /* Key is active high */
  {
    keys |= HAL_KEY_SW_5;
  }
#endif

#if defined (HAL_KEY_JOYSTICK_ENABLE)
/*
*  The joystick control is encoded as an analog voltage.  Keep on reading
*  the ADC until two consecutive key decisions are the same.
*/

  do
  {
    ksave1 = ksave0;    /* save previouse key reading */

    adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);

    if  (CHVER == 0x01)
    {
      /* Rev B */
      if ((adc >= 90) && (adc <= 100))
      {
         ksave0 |= HAL_KEY_UP;
      }
      else if ((adc >= 75) && (adc <= 85))
      {
        ksave0 |= HAL_KEY_RIGHT;
      }
      else if ((adc >= 45) && (adc <= 55))
      {
        ksave0 |= HAL_KEY_LEFT;
      }
      else if (adc <= 10)
      {
        ksave0 |= HAL_KEY_DOWN;
      }
      else if ((adc >= 101) && (adc <= 115))
      {
      }
    }
    else
    {
       /* Rev C */
      if ((adc >= 90)  && (adc <= 104))
      {
         ksave0 |= HAL_KEY_UP;
      }
      else if ((adc >= 75)  && (adc <= 89))
      {
        ksave0 |= HAL_KEY_RIGHT;
      }
      else if ((adc >= 45)  && (adc <= 56))
      {
        ksave0 |= HAL_KEY_LEFT;
      }
      else if (adc <= 10)
      {
        ksave0 |= HAL_KEY_DOWN;
      }
      else if ((adc >= 105) && (adc <= 121))
      {
      }
    }

  } while (ksave0 != ksave1);

  keys |= ksave0;

#endif

#endif /* HAL_KEY */

  return keys;

}


/**************************************************************************************************
 * @fn      HalKeyPoll
 *
 * @brief   Called by hal_driver to poll the keys
 *
 * @param   None
 *
 * @return  None
 **************************************************************************************************/
void HalKeyPoll (void)
{
#if (HAL_KEY == TRUE)

  uint8 keys = 0;

#if defined (HAL_KEY_JOYSTICK_ENABLE)
  uint8 ksave0 = 0;
  uint8 ksave1;
  uint8 adc;
#endif

  /*
  *  If interrupts are enabled, get the status of the interrupt-driven keys from 'halSaveIntKey'
  *  which is updated by the key ISR.  If Polling, read these keys directly.
  */
#if defined (HAL_KEY_SW_6_ENABLE)
  if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */
  {
    keys |= HAL_KEY_SW_6;
  }
#endif
#if defined (HAL_KEY_SW_5_ENABLE)
  if (HAL_KEY_SW_5_PORT & HAL_KEY_SW_5_BIT)       /* Key is active high */
  {
    keys |= HAL_KEY_SW_5;
  }
#endif

#if defined (HAL_KEY_JOYSTICK_ENABLE)
/*
*  The joystick control is encoded as an analog voltage.  Keep on reading
*  the ADC until two consecutive key decisions are the same.
*/

   do
  {
    ksave1 = ksave0;    /* save previouse key reading */

    adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);

      if ((adc >= 0x55) && (adc <= 0x70))
      {
         ksave0 |= HAL_KEY_UP;   //zuo
      }
      else if ((adc >= 0x40) && (adc <= 0x50))
      {
        ksave0 |= HAL_KEY_DOWN;
      }
      else if ((adc >= 0x18) && (adc <= 0x30))
      {
        ksave0 |= HAL_KEY_LEFT;
      }
      else if (adc <= 10)
      {
        ksave0 |= HAL_KEY_RIGHT;
      }
      else
      {
      }

  } while (ksave0 != ksave1);

  keys |= ksave0;

#endif

  /* Exit if polling and no keys have changed */
  if (!Hal_KeyIntEnable)
  {
    if (keys == halKeySavedKeys)
    {
      return;
    }
    halKeySavedKeys = keys;     /* Store the current keys for comparation next time */
  }

  /* Invoke Callback if new keys were depressed */
  if (keys && (pHalKeyProcessFunction))
  {
    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
  }

#endif /* HAL_KEY */

}


/**************************************************************************************************
 * @fn      halProcessKeyInterrupt
 *
 * @brief   Checks to see if it's a valid key interrupt, saves interrupt driven key states for
 *          processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later.
 *
 * @param
 *
 * @return
 **************************************************************************************************/
void halProcessKeyInterrupt (void)
{

#if (HAL_KEY == TRUE)

  bool    valid=FALSE;

#if defined (HAL_KEY_SW_6_ENABLE)
  if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)      /* Interrupt Flag has been set */
  {
    HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);    /* Clear Interrupt Flag */
    valid = TRUE;
  }
#endif

#if defined (HAL_KEY_SW_5_ENABLE)
  if (HAL_KEY_SW_5_PXIFG & HAL_KEY_SW_5_BIT)      /* Interrupt Flag has been set */
  {
    HAL_KEY_SW_5_PXIFG = ~(HAL_KEY_SW_5_BIT);    /* Clear Interrupt Flag */
    valid = TRUE;
  }
#endif

  if (valid)
  {
    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
  }
#endif /* HAL_KEY */
}

/**************************************************************************************************
 * @fn      HalKeyEnterSleep
 *
 * @brief  - Get called to enter sleep mode
 *
 * @param
 *
 * @return
 **************************************************************************************************/
void HalKeyEnterSleep ( void )
{
  /* Sleep!!! Note that HAL_KEY_SW_5 is shared with CTS pin of RS-232.
   * It was set to tri-state during active state. It needs to be pulled-up.
   */
#if defined (HAL_KEY_SW_5_ENABLE)
  HAL_KEY_SW_5_INP &= ~HAL_KEY_SW_5_BIT;       /* Set pin input mode to pull-up */
#endif
}

/**************************************************************************************************
 * @fn      HalKeyExitSleep
 *
 * @brief   - Get called when sleep is over
 *
 * @param
 *
 * @return  - return saved keys
 **************************************************************************************************/
uint8 HalKeyExitSleep ( void )
{
  /* Wakeup!!! Note that HAL_KEY_SW_5 is shared with CTS pin of RS-232.
   * It was pulled up during sleep. It needs to be set to tri-state during active state.
   */
#if defined (HAL_KEY_SW_5_ENABLE)
  HAL_KEY_SW_5_INP |= HAL_KEY_SW_5_BIT;       /* Set pin input mode to tri-state */
#endif

  /* Wake up and read keys */
  return ( HalKeyRead () );
}

/***************************************************************************************************
 *                                    INTERRUPT SERVICE ROUTINE
 ***************************************************************************************************/

/**************************************************************************************************
 * @fn      halKeyPort0Isr
 *
 * @brief   Port0 ISR
 *
 * @param
 *
 * @return
 **************************************************************************************************/
HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
{
  /* P0IF is cleared by HW for CHVER < REV_E */

  halProcessKeyInterrupt();

  if( CHVER >= REV_E )
  {
    /* Make sure that we clear all enabled, but unused P0IFG bits.
     * For P0 we can only enable or disable high or low nibble, not bit by
     * bit. For P1 and P2 enabling of single bits are possible, therefore
     * will not any unused pins generate interrupts on P1 or P2.
     * We could have checked for low and high nibble in P0, but this
     * isn't necessary as long as we only clear unused pin interrupts.
     */
    P0IFG = (HAL_KEY_P0INT_LOW_USED | HAL_KEY_POINT_HIGH_USED);
    P0IF = 0;
    CLEAR_SLEEP_MODE();
  }
}

/**************************************************************************************************
 * @fn      halKeyPort1Isr
 *
 * @brief   Port1 ISR
 *
 * @param
 *
 * @return
 **************************************************************************************************/
HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
{
  if( CHVER <= REV_D )
  {
    P1IF = 0;
  }

  halProcessKeyInterrupt();

  if( CHVER >= REV_E )
  {
    P1IF = 0;
    CLEAR_SLEEP_MODE();
  }
}

/**************************************************************************************************
 * @fn      halKeyPort2Isr
 *
 * @brief   Port2 ISR
 *
 * @param
 *
 * @return
 **************************************************************************************************/
HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
{
  if( CHVER <= REV_D )
  {
    P2IF = 0;
  }

  halProcessKeyInterrupt();

  if( CHVER >= REV_E )
  {
    P2IF = 0;
    CLEAR_SLEEP_MODE();
  }
}

/**************************************************************************************************
**************************************************************************************************/



⌨️ 快捷键说明

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