2011年12月5日 星期一

Key pad detect program

//=========================
//Project:    Key pad detect program
//Author:      Leo.Tseng
//Date:        2011/12/06
//MCU:         W78E054D
//CRYSTAL:     25Mhz
//Description: Use uart for output, set boudrate=9600,
//             Decode KEY pad, this program could support
//             4X4 KEY pad array.
//=========================
#include  <reg52.h>
#include  <stdio.h>
// 變數宣告

sbit UP = P1^2;                    // LED1
sbit DOWN = P1^3;                  // LED2
sbit LEFT = P1^4;                  // LED3
sbit RIGHT = P1^5;                 // LED4
sbit SCAN0 = P2^7;                 // KEY SCAN0
sbit SCAN1 = P2^6;                 // KEY SCAN1
sbit SCAN2 = P2^5;                 // KEY SCAN2
sbit SCAN3 = P2^4;                 // KEY SCAN3
sbit DET3 = P2^3;                  // KEY DETECT0, always high
sbit DET2 = P2^2;                  // KEY DETECT2
sbit DET1 = P2^1;                  // KEY DETECT1
sbit DET0 = P2^0;                  // KEY DETECT0
int n_SCAN;
int TIMES=65536;                   // 16位元計數器 
int PWMHIGH=1;                 
int PWMLOW=50000;                 
int PERIOD;
unsigned KEY,temp;
//副程式宣告
void TIMER1_initinal(int baudrate);
void delay (int j);
//主程式開始
void main(void)
{

 TIMER1_initinal(9600);               // 使用UART,baudrate=9600
 PERIOD=TIMES-PWMHIGH;
 TMOD = (TMOD & 0xF0) | 0x01;         // Set T/C0 Mode
 TH0= PERIOD/256;                     // load sampleing rate
 TL0= PERIOD%256;                     //
 ET0 = 1;                             // Enable Timer 0 Interrupts
 TR0 = 1;                             // Start Timer 0 Running
 EA = 1;                              // Global Interrupt Enable

 printf("\n7 KEY Detect Program\n\n");

 SCAN0=SCAN1=SCAN2=SCAN3=1;
 DET3=DET2=DET1=DET0=1;               // 設定成輸入pin
 n_SCAN=0;
 UP=DOWN=LEFT=RIGHT=0;

 while(1)
  {
   if((P2&0xFF)!=0xFF)                // 偵測是否按按鍵?
    {
     temp=P2&0xFF;                   // 第一次儲存按鍵值
      delay(6000);                    // 延遲 0.48us*6000=2.88ms,防彈跳調整
      KEY=P2&0xFF;                    // 第二次讀取按鍵值
      if (KEY&&temp)                  // 判別是否為同一鍵
      { 
       delay(6000);
       switch(KEY)                         
         {
          case 0x7B: printf("SW20 pressed, code=0x7B\n");
                    UP^=1;
                    break;
                    
          case 0x7D: printf("SW15 pressed, code=0x7D\n");
                    LEFT^=1;
                    break;
             
          case 0x7E: printf("SW19 pressed, code=0x7E\n");
                    UP=DOWN=LEFT=RIGHT=1;
                    break;
                                          
          case 0xBD: printf("SW14 pressed, code=0xBD\n");
                    DOWN^=1;
                    break;
                    
          case 0xBE: printf("SW18 pressed, code=0xBE\n");
                    DOWN^=!(UP^=1);                      // binary down counter
                     LEFT^=(!DOWN && !UP);
                     RIGHT^=(!LEFT && !DOWN && !UP);
                    break;
                               
          case 0xDE: printf("SW17 pressed, code=0xDE\n");
                    DOWN^=UP^=1;                         // binary up counter
                     LEFT^=(DOWN && UP);
                     RIGHT^=(LEFT && DOWN && UP);
                    break;
                    
          case 0xEE: printf("SW16 pressed, code=0xEE\n");
                    RIGHT^=1;
                    break;
                               
          default: break;                
         }
       //delay(6000); 
      }
    }
  }
}

//UART副程式
void TIMER1_initinal(int baudrate)      
{
 PCON  = 0x00;                          // PCON: if SMOD=1(b7), Baud Rate is Doubled                         
 SCON  = 0x52;                          // SCON: mode 1, 8-bit UART, enable rcvr                   
 TMOD  = 0x20;                            // TMOD: timer 1, mode 2, 8-bit reload          
 //TH1   = 256-(11059200/384/baudrate);   // TH1:  reload value for baud @ 11.0592MHz, TH=0xFD
 TH1   = 256-(25000000/372/baudrate);     // TH1:  reload value for baud @ 25MHz, TH=0xF9   
 TR1   = 1;                               // TR1:  timer 1 run                             
 //TI    = 1;                             // TI:   set TI to send first char of UART   
}
//計時器0中斷1
void timer0_ISR(void) interrupt 1
{
 if((SCAN0==1)&&(SCAN1&&SCAN2&&SCAN3)&&(n_SCAN==0))
  {
   TR0=0;
   SCAN0=0;
   PERIOD = TIMES-PWMLOW;
   TH0= PERIOD/256;
   TL0= PERIOD%256;
   TR0=1;
  }
 else if((SCAN0==0)&&(SCAN1&&SCAN2&&SCAN3)&&(n_SCAN==0))
   {
     TR0=0;
     n_SCAN=1;                                         // 準備掃瞄列1
     SCAN0=1;
     PERIOD = TIMES-PWMHIGH;                    
     TH0= PERIOD/256;
     TL0= PERIOD%256;
     TR0=1;
    }

  if((SCAN1==1)&&(SCAN0&&SCAN2&&SCAN3)&&(n_SCAN==1))
  {
   TR0=0;
   SCAN1=0;
   PERIOD = TIMES-PWMLOW;
   TH0= PERIOD/256;
   TL0= PERIOD%256;
   TR0=1;
  }
 else if((SCAN1==0)&&(SCAN0&&SCAN2&&SCAN3)&&(n_SCAN==1))
   {
     TR0=0;
     n_SCAN=2;                                         // 準備掃瞄列2
     SCAN1=1;
     PERIOD = TIMES-PWMHIGH;                    
     TH0= PERIOD/256;
     TL0= PERIOD%256;
     TR0=1;
    }
 if((SCAN2==1)&&(SCAN0&&SCAN1&&SCAN3)&&(n_SCAN==2))
  {
   TR0=0;
   SCAN2=0;
   PERIOD = TIMES-PWMLOW;
   TH0= PERIOD/256;
   TL0= PERIOD%256;
   TR0=1;
  }
 else if((SCAN2==0)&&(SCAN0&&SCAN1&&SCAN3)&&(n_SCAN==2))
   {
     TR0=0;
     n_SCAN=3;                                         // 準備掃瞄列3
     SCAN2=1;
     PERIOD = TIMES-PWMHIGH;                    
     TH0= PERIOD/256;
     TL0= PERIOD%256;
     TR0=1;
    }
if((SCAN3==1)&&(SCAN0&&SCAN1&&SCAN2)&&(n_SCAN==3))
  {
   TR0=0;
   SCAN3=0;
   PERIOD = TIMES-PWMLOW;
   TH0= PERIOD/256;
   TL0= PERIOD%256;
   TR0=1;
  }
 else if((SCAN3==0)&&(SCAN0&&SCAN1&&SCAN2)&&(n_SCAN==3))
   {
     TR0=0;
     n_SCAN=0;                                          // 準備掃瞄列0
     SCAN3=1;
     PERIOD = TIMES-PWMHIGH;                    
     TH0= PERIOD/256;
     TL0= PERIOD%256;
     TR0=1;
    }
                     
}

void delay (int j)
{
 while(j)
  j--;
}


//======== CONSOLE MESSAGE =======
7 KEY Detect Program
SW20 pressed, code=0x7B
SW14 pressed, code=0xBD
SW15 pressed, code=0x7D
SW16 pressed, code=0xEE
SW17 pressed, code=0xDE
SW18 pressed, code=0xBE
SW19 pressed, code=0x7E

沒有留言:

張貼留言