1 配置系统滴答定时器
/**************************************************************************
* º¯ÊýÃû : SystickInitialisation
* º¯ÊýÃèÊö : ÉèÖÃSystick¶¨Ê±Æ÷,ÖØ×°ÔØʱ¼äΪ20ms
* ÊäÈë²ÎÊý : ÎÞ
* Êä³ö½á¹û : ÎÞ
* ·µ»ØÖµ : ÎÞ
**************************************************************************/
void SystickInitialisation(void)
{
if (SysTick_Config(72000 * 20)) // ST3.5.0¿â°æ±¾ 20ms¶¨Ê±
{
/* Capture error */
while (1);
}
}
配置的时候系统函数已经配置好中断
2 设置中断标志
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval : None
*/
void SysTick_Handler(void)
{
sign_20ms = 1;
}
3 按钮检测程序有一个标志代表按钮时间也就是自己可以设置长按和短按
<pre name="code" class="cpp">int main(void)
{
vu16 KeyPortStatus = 0;
/* ¶¨Òå°´¼üɨÃè״̬ö¾Ù±äÁ¿ */
KeyScanState_Typedef KeyScanState;
RCC_Configuration(); //ϵͳʱÖÓ³õʼ»¯
GPIO_Configuration();//¶Ë¿Ú³õʼ»¯
SystickInitialisation();
GPIO_SetBits(GPIOB,GPIO_Pin_5);
while(1)
{
#if 1
if(sign_20ms == Bit_SET)
{
sign_20ms = 0;
/* ¶ÁÈ¡IOµçƽ */
KeyPortStatus = GPIO_ReadInputDataBit(KEYPORT,KEY0PIN) & 0x0001;
/* ½øÈë״̬»úÁ÷³Ì */
switch(KeyScanState)
{
/* ״̬1£ºÅжÏÓзñ°´¼ü°´Ï */
case KeyScanState_0:
{
if(KeyPortStatus != 0x0001)
{
/* Óа´¼ü°´Ï£¬¸üÐÂ״̬±êÖ¾ */
KeyScanState = KeyScanState_1;
}
break;
}
/* ״̬2£ºÅжÏÊÇ·ñ¶¶¶¯ */
case KeyScanState_1:
{
if(!KeyPortStatus)
{
/* ·Ç¶¶¶¯£¬È·ÈÏ°´¼ü°´Ï£¬Ö´ÐÐÏàÓ¦²Ù×÷ */
if(GPIO_ReadInputDataBit(KEYPORT, KEY0PIN) == 0)
{
// GPIO_WriteBit(LEDPORT,
// LED1PIN,
// (BitAction)(1 - GPIO_ReadOutputDataBit(LEDPORT, LED1PIN)));
sign_keypress ++;
}
/* ¸üÐÂ״̬±êÖ¾ */
KeyScanState = KeyScanState_2;
}
else
{
/* ¶¶¶¯£¬È·ÈÏ°´¼üδ°´Ï£¬¸üÐÂ״̬±êÖ¾*/
KeyScanState = KeyScanState_0;
}
break;
}
/* ״̬3£ºËÉÊÖ¼ì²â */
case KeyScanState_2:
{
if(KeyPortStatus == 0x0001)
{
/* ËÉÊÖ£¬¸üÐÂ״̬±êÖ¾ */
KeyScanState = KeyScanState_0;
}
else
{
KeyScanState = KeyScanState_1;
}
break;
}
}
}
if(sign_keypress == 150)
{
sign_keypress = 0;
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
else
{
}
#endif
#if 0
if(sign_20ms == Bit_SET)
{
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET)
{
//LED ·¢¹â
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
else
{
//LED ϨÃð
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
}
#endif
}
}