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

📄 simple_sequencer.c

📁 非常简单的低端单片机内核, 有定时器,任务调度,状态机例子
💻 C
字号:
/*******************************************************************************
File: Simple_Sequencer.c

Description:
This file implements the key debouncer example

Creation
Date:	November 3, 2004
Author:	Benjamin M. Kacenas
*******************************************************************************/
#include "uK_Kernel.h"

void KeyDebounce(unsigned char *ucp_KeyCode);

typedef enum KeyState
{
    KeyDownWaiting = 0,    // Initial state, waiting for a key press
    KeyDown,               // The key was really pressed
    KeyUpWaiting,          // The key is pressed waiting for release
    KeyUp                  // The key was really released
};

// Some number of pointers to switch input ports
unsigned char *KeyInputPort[];

void KeyDebounce(unsigned char *ucp_KeyCode)
{
    // State variable
    static KeyState e_KeyState = KeyDownWaiting;

    switch(e_KeyState)   // What state are we in
    {
        case KeyDownWaiting:
            // The key was pressed go to the next state
            e_KeyState = KeyDown;
            sl_RequestDelay(DELAY_10MSEC, (FuncPoint)KeyDebounce,
                ucp_KeyCode);
            break;
        case KeyDown:
            // See if the key is still down
            if (!*KeyInputPort[*ucp_KeyCode])
            {
                // The key is still down, tell the application
                KeyPressed(*ucp_KeyCode);
                // Go to the next state to look for key released
                e_KeyState = KeyUpWaiting;
                sl_RequestDelay(DELAY_100MSEC, (FuncPoint)KeyDebounce,
                    ucp_KeyCode);
            }
            else
            {
                // The key is not pressed, must have been a false alarm
                e_KeyState = KeyDownWaiting;
            }
            break;
        case KeyUpWaiting:
            // See if the key has been released
            if (*KeyInputPort[*ucp_KeyCode])
            {
                // We think the key has been released, next state
                e_KeyState = KeyUp;
                sl_RequestDelay(DELAY_10MSEC, (FuncPoint)KeyDebounce,
                    ucp_KeyCode);
            }
            else
            {
                // The key is still down, come back here later
                sl_RequestDelay(DELAY_100MSEC, (FuncPoint)KeyDebounce,
                    ucp_KeyCode);
            }
            break;
        case KeyUp:
            // See if the key is still released
            if (*KeyInputPort[*ucp_KeyCode])
            {
                // The key has been released, tell the application
                KeyReleased(*ucp_KeyCode);
                // We are done now, get out and don't come back
                e_KeyState = KeyDownWaiting;
            }
            else
            {
                // The key is still down, false alarm
                // go back to wait for the release
                e_KeyState = KeyUpWaiting;
                sl_RequestDelay(DELAY_100MSEC, (FuncPoint)KeyDebounce,
                    ucp_KeyCode);
            }
            break;
        default:
            break;
    }
}

⌨️ 快捷键说明

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