找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2744|回復: 0
收起左側

求一個矩陣鍵盤輸入,LCD顯示多位數(shù)的函數(shù), 主函數(shù)該如何處理?

[復制鏈接]
ID:252935 發(fā)表于 2018-3-18 14:33 | 顯示全部樓層 |閱讀模式
主函數(shù)該如何處理?

#include<reg51.h>       
#include"lcd.h"

#define GPIO_KEY P1

unsigned char KeyValue;
//用來存放讀取到的鍵值
unsigned char KeyState;
//用來存放按鍵狀態(tài)
unsigned char PuZh[]=" Pechin Science ";
unsigned char dat[]="0123456789ABCDEF";
void Delay10ms();   //延時50us
void KeyDown();                 //檢測按鍵函數(shù)

/*******************************************************************************
* 函 數(shù) 名         : main
* 函數(shù)功能                   : 主函數(shù)
* 輸    入         : 無
* 輸    出         : 無
*******************************************************************************/
void main(void)
{
        unsigned char i;
        LcdInit();
        KeyState=0;
        for(i=0;i<16;i++)
        {
//                LcdWriteCom(0x80);
                LcdWriteData(PuZh[i]);       
        }
        while(1)
        {
                KeyDown();
                if(KeyState)
                {
                        KeyState=0;
                        LcdWriteCom(0x80+0x40);
                        LcdWriteData(dat[KeyValue]);
                }
        }                               
}
/*******************************************************************************
* 函 數(shù) 名         : KeyDown
* 函數(shù)功能                   : 檢測有按鍵按下并讀取鍵值
* 輸    入         : 無
* 輸    出         : 無
*******************************************************************************/
void KeyDown(void)
{
        char a;
        GPIO_KEY=0x0f;
        if(GPIO_KEY!=0x0f)
        {
                Delay10ms();
                if(GPIO_KEY!=0x0f)
                {
                        KeyState=1;
                        //測試列
                        GPIO_KEY=0X0F;
//                         Delay10ms();
                        switch(GPIO_KEY)
                        {
                                case(0X07):        KeyValue=0;break;
                                case(0X0b):        KeyValue=1;break;
                                case(0X0d): KeyValue=2;break;
                                case(0X0e):        KeyValue=3;break;
//                                default:        KeyValue=17;        //檢測出錯回復17意思是把數(shù)碼管全滅掉。
                        }
                        //測試行
                        GPIO_KEY=0XF0;
                        Delay10ms();
                        switch(GPIO_KEY)
                        {
                                case(0X70):        KeyValue=KeyValue;break;
                                case(0Xb0):        KeyValue=KeyValue+4;break;
                                case(0Xd0): KeyValue=KeyValue+8;break;
                                case(0Xe0):        KeyValue=KeyValue+12;break;
//                                default:        KeyValue=17;
                        }
                        while((a<50)&&(GPIO_KEY!=0xf0))         //檢測按鍵松手檢測
                        {
                                Delay10ms();
                                a++;
                        }
                        a=0;
                }
        }
}
/*******************************************************************************
* 函 數(shù) 名         : Delay10ms
* 函數(shù)功能                   : 延時函數(shù),延時10ms
* 輸    入         : 無
* 輸    出         : 無
*******************************************************************************/
void Delay10ms(void)   //誤差 0us
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}



回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表