實驗目的:

1. 學習鍵盤及LED 驅動原理。

2. 將鍵盤上的按鍵在超級終端上和LED上顯示出來。
 

#include <stdlib.h>

#include <string.h>

 

//Shin, On Pil

#include "def.h"

#include "option.h"

#include "2410addr.h"

#include "2410lib.h"

#include "2410slib.h"

#include "mmu.h"

void Isr_Init(void);

void HaltUndef(void);

void HaltSwi(void);

void HaltPabort(void);

void HaltDabort(void);

U16 FunctionKey=0xffff;//功能键扫描吗,0有效

U8 keyBoard_KeyMap[]={'b','9','6','3','a','8','5','2','0','7','4','1',1,13,14,15};//10-退格,11-*/.,12-,13-,14-确定,15-取消;

U16 GetScanKey()

{

    U16 key=0xffff;

    U32 i,temp=0xf;

    for(i=1;i<0x8;i<<=1){

        rGPEDAT|=0x3800;

        rGPEDAT&=~(i<<11);

        key<<=4;

        Delay(1);

        //temp=rGPGDAT;

 

        if((rGPGDAT&0x04)==0x0)

            temp&=0xe;

        else

            temp|=0x1;

       

        if((rGPGDAT&0x20)==0x0)

            temp&=0xd;

        else

            temp|=0x2;

       

        if((rGPGDAT&0x40)==0x0)

            temp&=0xb;

        else

            temp|=0x4;

       

        if((rGPGDAT&0x80)==0x0)

            temp&=0x7;

        else

            temp|=0x8;

        key|=(temp&0xf);

    }

    return key;

}

U32 GetKey()

{

    int i;

    U16 key,tempkey=1;

    static U16 oldkey=0xffff;

    static U8 keystatus=0;

    U8 keycnt=0;

    U32 temp;

    while(1){

        key=0xffff;

        while(1){

            key=GetScanKey();

            if((key&FunctionKey)!=FunctionKey)//有按键按下

                break;

            Delay(200);

            oldkey=0xffff;

        }

        //Delay(800);

        Delay(500);

        if(key!=GetScanKey())

            continue;

 

        if(oldkey!=key){

            keystatus=0;

        }

        if(keystatus==0){   //第一次按下此键

            keycnt=0;

            keystatus=1;

        }

        else if(keystatus==1){  //第二次重复此键

            keycnt++;

            if(keycnt==20)

                keystatus=2;

            else

                continue;

        }

 

        oldkey=key;

        break;

    }

     for(i=0;i<16;i++){  //查找按键,不包括功能键

        if((key&tempkey)==0 && (FunctionKey&tempkey)!=0)

            break;

        tempkey<<=1;

    }

//  temp=~(key|FunctionKey);

    return (keyBoard_KeyMap[i]);

//return i;

}

void SetFunctionKey(U16 Fnkey)  //设定功能键扫描码,1有效 比如ctrl alt

{

    FunctionKey&=~Fnkey;

}

//===================================================================

void Main(void)

{

    int i;

    int key;

    Led_Display(15);

   

    MMU_Init();

  

#if ADS10  

    __rt_lib_init();                //for ADS 1.0

#endif

   

//    ChangeClockDivider(0,0);          // 1:1:1   

//    ChangeClockDivider(1,0);          // 1:2:2   

    ChangeClockDivider(1,1);          // 1:2:4   

 

//    ChangeMPllValue(0x92,0x4,0x2);    // FCLK=50MHz   

//    ChangeMPllValue(0x96,0x5,0x1);    // FCLK=135428571Hz

//    ChangeMPllValue(0x2a,0x1,0x0);    // FCLK=200MHz  

//    ChangeMPllValue(0x5c,0x1,0x1);    // FCLK=200MHz

//    ChangeMPllValue(0x5c,0x4,0x0);    // FCLK=200MHz

//    ChangeMPllValue(0x8e,0x7,0x0);    // FCLK=200MHz       

    ChangeMPllValue(0xa1,0x3,0x1);    // FCLK=202.8MHz 

//    ChangeMPllValue(0x66,0x1,0x1);    // FCLK=220MHz  

//    ChangeMPllValue(0x69,0x1,0x1);    // FCLK=226MHz      

//    ChangeMPllValue(0x96,0x2,0x1);    // FCLK=237MHz

   

    Port_Init();

    Isr_Init();

//    Rtc_Init();

    Uart_Init(0,115200);

    Uart_Select(0);

 

    //Check whether or not the POWER_OFF wake-up.

    Delay(0);   //calibrate Delay()

 

  rGPECON=0xf57fffff;

  rGPGCON=0xffff03cf;

   for (;;){

         key=GetKey();

        //key=key+'1'-1;

        Uart_SendByte(key);

        key=key-'1'+1;

        Led_Display(key);

    }

 }

 //===================================================================

void Isr_Init(void)

{

    pISR_UNDEF  = (unsigned)HaltUndef;

    pISR_SWI    = (unsigned)HaltSwi;

    pISR_PABORT = (unsigned)HaltPabort;

    pISR_DABORT = (unsigned)HaltDabort;

   

    rINTMOD     = 0x0;                     //All=IRQ mode

//    rINTCON=0x5;                           //Non-vectored,IRQ enable,FIQ disable   

    rINTMSK     = BIT_ALLMSK;              //All interrupt is masked.

    rINTSUBMSK  = BIT_SUB_ALLMSK;          //All sub-interrupt is masked. <- April 01, 2002 SOP

 

//    rINTSUBMSK  = ~(BIT_SUB_RXD0);         //Enable Rx0 Default value=0x7ff

//    rINTMSK     = ~(BIT_UART0);            //Enable UART0 Default value=0xffffffff   

   

//    pISR_UART0=(unsigned)RxInt;            //pISR_FIQ,pISR_IRQ must be initialized

}

 

//===================================================================

void HaltUndef(void)

{

    Uart_Printf("Undefined instruction exception.\n");

    while(1);

}

//===================================================================

void HaltSwi(void)

{

    Uart_Printf("SWI exception.\n");

    while(1);

}

//===================================================================

void HaltPabort(void)

{

    Uart_Printf("Pabort exception.\n");

    while(1);

}

 

//===================================================================

void HaltDabort(void)

{

    Uart_Printf("Dabort exception.\n");

    while(1);

}

 

 

 /*

//=========================

    while(1)

    {

       Led_Display(3);

    }

//=========================   

*/

/*

//=========================

    while(1)

    {

       Led_Display(1);

       Delay(1500);  

       Led_Display(2);

       Delay(1500);            

       Led_Display(4);

       Delay(1500);  

       Led_Display(8);

       Delay(1500);               

    }

//=========================  

*/