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

📄 keyboard.c

📁 实现了dsPIC30f6012a通过SPC3与PLC通信
💻 C
📖 第 1 页 / 共 2 页
字号:
//************************************************************************
//File Name:   Keyboard.c
//Include:    void Key_Scan(unsigned char *buffer, unsigned char *counter,
//                         unsigned char *step, unsigned char *key_value);
//                           -- Scan the keyboard and return a value.
//             void Key_Treat(unsigned char key_value);
//                    -- Treat the value which comes from Key_Scan func.
//Description: Scan the keyboard and treat with it.
//************************************************************************
#include "Headfiles.h"

//************************************************************************
//Func. Name:        Key_Scan Func.
//Input variables:   unsigned char *buffer -- Record the key value temporarily.
//                   unsigned char *counter-- Record the scan times.
//                   unsigned char *step   -- Record the scan step,
//                                            may be one of the following value:
//                                  "KEYBOARD_SCAN_START, KEYBOARD_SCAN_DELAY_1, 
//                                   KEYBOARD_SCAN_GETKEY, KEYBOARD_SCAN_DELAY_2,
//                                   KEYBOARD_SCAN_SPDUP_1, KEYBOARD_SCAN_SPDUP_2"
//                   unsigned char *key_value  -- Record the result of scan,
//                                            may be one of the following value:
//                                  "NON_KEY, LEFT_KEY, RIGHT_KEY, UP_KEY,
//                                   DOWN_KEY, ENTER_KEY, MENU_KEY"
//Output variables:  None
//Use sub-func.list: None
//Used by list:      MainFunc.c - void main(void);
//Description:       Scan the keyboard and return a reslut of the scan.If one key
//                is pressed, it will return logic 0; otherwise return logic 1.
//                   ------------------
//                  | MSP430      P1.0 |<--- key1 (UP_KEY)
//                  |  F167       P1.1 |<--- key2 (DOWN_KEY)
//                  |             P1.2 |<--- key3 (LEFT_KEY)
//                  |             P1.3 |<--- key4 (RIGHT_KEY)
//                  |             P1.4 |<--- key5 (MENU_KEY)
//                  |             P1.5 |<--- key6 (ENTER_KEY)
//********************************************************************************
void Key_Scan(unsigned char *buffer, unsigned char *counter, unsigned char *step, unsigned char *key_value)
{
    switch (*step)                   //"step" is signed with way the of the subfunc.
    {
        case KEYBOARD_SCAN_START :       //Keyboard scan func. starts from here.
            if (((PORTD >> 5) & 0x3F) != 0x3F)   //Scan the ports and make sure whether one of them is valued or not.
            {
                *buffer = (PORTD >> 5) & 0x3F;    //If valued, record it to the buffer.
                *counter = 0;             //Clear the counter.
                *step = KEYBOARD_SCAN_DELAY_1;  //Go to delay_1 step.
            }
            else                         //If not valued
            {
                *buffer = 0x00;          //Clear the buffer for next scan.
                *key_value = NON_KEY;        //Record the result of this scan, none key is pressed.
                *counter = 0;            //Clear the counter
                *step = KEYBOARD_SCAN_START; //Go to start step at the next scan.
            }
            break;
        case KEYBOARD_SCAN_DELAY_1 :     //Delay for preventing mechnical tremble in pressing the key.
            if (*counter >= 5)           //The counter is enough and go on.
            {
                *step = KEYBOARD_SCAN_GETKEY;  //Go to getkey step.
                *counter = 0;              //Clear the counter.
            }
            else                        //The counter is not enough and continue to count.
            {
                (*counter)++;            //Continue to count.
                *step = KEYBOARD_SCAN_DELAY_1;  //Back to the delay step at the next scan.
                *key_value = NON_KEY;      //Record none key is pressed.
            }
            break;
        case KEYBOARD_SCAN_GETKEY :       //Scan the keyboard and record the value is key is pressed.
            if (*buffer == ((PORTD >> 5) & 0x3F))  //Scan the keyboard and compare with the buffer
            {
                switch (*buffer)          //If the value in buffer is as same as the keyboard.
                {
                    case 0x1F : *key_value = UP_KEY;     break;
                    case 0x2F : *key_value = DOWN_KEY;   break;
                    case 0x37 : *key_value = LEFT_KEY;   break;
                    case 0x3B : *key_value = RIGHT_KEY;  break;
                    case 0x3D : *key_value = MENU_KEY;   break;
                    case 0x3E : *key_value = ENTER_KEY;  break;
                    default   : *key_value = NON_KEY;    break;  //Record the value to "value" variable.
                }
                //*step =  KEYBOARD_SCAN_DELAY_2;
                if (((PORTD >> 5 )& 0x3F) != 0x3F)
                {
                    *step = KEYBOARD_SCAN_DELAY_2;  //Go to the next step for acceleration.
                }
                else
                {
                    *step = KEYBOARD_SCAN_START;
                }
            }
            else                         //If the value in buffer is not as same as the keyboard.
            {
                *key_value = NON_KEY;
                *step = KEYBOARD_SCAN_START;
                *buffer = 0x00;
                *counter = 0;                 //Clear the value and Go to start step at the next scan.
            }
            break;
        case KEYBOARD_SCAN_DELAY_2 :       //Delay for acceleration of some keys.
            if (*counter >= 10)
            {
                if ((((PORTD >> 5) & 0x3F) == 0x3B) ||
                    (((PORTD >> 5) & 0x3F) == 0x37))
                {
                    *counter = 0;
                    *step = KEYBOARD_SCAN_SPDUP_1;   //Spdup1 for LEFT_KEY & RIGHT_KEY.
                }
                else
                {
                    *counter = 0;
                    *step = KEYBOARD_SCAN_SPDUP_2;  //Spdup2 for other keys.
                }
            }
            else                  //Delay is not over, continue counting to next scan.
            {
                (*counter)++;
                *step = KEYBOARD_SCAN_DELAY_2;
                *key_value = NON_KEY;
            }
            break;
        case KEYBOARD_SCAN_SPDUP_1 :
            if (*buffer == ((PORTD >> 5) & 0x3F))
            {
                if(*counter >= 20)
                {
                    switch (*buffer)
                    {
                        case 0x37 : *key_value = LEFT_KEY;   break;
                        case 0x3B : *key_value = RIGHT_KEY;  break;
                        default   : *key_value = NON_KEY;    break;
                    }
                    *counter -= 5;  //Control the ration of acceleration for LEFT_KEY & RIGHT_KEY.
                }
                else
                {
                    (*counter)++;
                    *key_value = NON_KEY;
                    *step = KEYBOARD_SCAN_SPDUP_1;
                }
            }
            else
            {
                *key_value = NON_KEY;
                *step = KEYBOARD_SCAN_START;
                *counter = 0;
                *buffer = 0x00;
            }
            break;
        case KEYBOARD_SCAN_SPDUP_2 :
            if (*buffer == ((PORTD >> 5) & 0x3F))
            {
                if(*counter >= 20)
                {
                     switch (*buffer)
                     {
                         case 0x1F : *key_value = UP_KEY;    break;
                         case 0x2F : *key_value = DOWN_KEY;  break;
                         default   : *key_value = NON_KEY;   break;
                     }
                     *counter -= 20;  //Control the ration of acceleration for UP_KEY & DOWN_KEY.
                }
                else
                {
                     (*counter)++;
                     *key_value = NON_KEY;
                     *step = KEYBOARD_SCAN_SPDUP_2;
                }
            }
            else
            {
                *key_value = NON_KEY;
                *step = KEYBOARD_SCAN_START;
                *counter = 0;
                *buffer = 0x00;
            }
            break;
        default : break;
    }
}
//*************************************************************************************
//Func. Name:        Key_Teart Func.
//Input variables:   unsigned char key_value  -- The result of scan,
//                                  may be one of the following value:
//                                  "NON_KEY, LEFT_KEY, RIGHT_KEY, UP_KEY,
//                                   DOWN_KEY, ENTER_KEY, MENU_KEY"
//Output variables:  None
//Use sub-func.list: Flash.c - void Flash_Write(void);  -- Write data into FLASH memory.
//                   Assistant.c - unsigned char Array_Ptr(void);
//                      -- Calculate the array subscript of data, as the following:
//                          "Control_Parameter[], Channel_Parameter[][],
//                           System_Parameter[], System_Parameter_Asic[]"
//                   Assistant.c - void Study_Self(unsigned channel);
//                      -- Record the referrence value of hydrulic pressure by some format.
//                         ref. value = P1 * 1.434 - P2;
//                   Assistant.c - void LCD_Arrow_Position(unsigned char arrow_min,
//                                                         unsigned char arrow_max);
//                      --Computing
//Used by list:      MainFunc.c - void main(void);
//Description:       Scan the keyboard and return a reslut of the scan.If one key
//                is pressed, it will return logic 0; otherwise return logic 1.
//************************************************************************************
void Key_Treat(unsigned char key_value)
{
    unsigned char data_ptr;
    
    if (key_value == ENTER_KEY)
    {
        switch (Interface)
        {
            case MAIN_VIEW : Interface = ENTER_ASK_VIEW;
                             LCD_Clear();
                             break;
            case ENTER_ASK_VIEW : EEPROM_Write();
                                  Interface = ENTER_ANS_VIEW;
                                  LCD_Clear();
                                  break;
            case ENTER_ANS_VIEW : Interface = MAIN_VIEW;
                                  LCD_Clear();
                                  break;
            case MENU_VIEW : switch (Arrow_Position)
                             {
                                 case 0 : Interface = CONTROL_SET_VIEW;
                                          Arrow_Position = 1;

⌨️ 快捷键说明

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