Книга: Advanced PIC Microcontroller Projects in C

Program Using a Built-in Keypad Function

Program Using a Built-in Keypad Function

In the program listing in Figure 6.48, a function called getkeypad has been developed to read a key from the keyboard. The mikroC language has built-in functions called Keypad_Read and Keypad_Released to read a key from a keypad when a key is pressed and when a key is released respectively. Figure 6.49 shows a modified program (KEYPAD2.C) listing using the Keypad_Released function to implement the preceding calculator project. The circuit diagram is the same as in Figure 6.46.

/**************************************************************
               CALCULATOR WITH KEYPAD AND LCD
               ==============================
In this project a 4 x 4 keypad is connected to PORTB of a PIC18F452
microcontroller. Also an LCD is connected to PORTC. The project is a simple
calculator which can perform integer arithmetic.
The keys are labeled as follows:
1 2 3 4
5 6 7 8
9 0   Enter
+ ? * /
In this program mikroC built-in functions are used.
Author: Dogan Ibrahim
Date:   July 2007
File:   KEYPAD2.C
**************************************************************/
#define Enter 12
#define Plus 13
#define Minus 14
#define Multiply 15
#define Divide 16
//
// Start of MAIN program
//
void main() {
 unsigned char MyKey, i,j,lcd[5],op[12];
 unsigned long Calc, Op1, Op2;
 TRISC = 0; // PORTC are outputs (LCD)
 //
 // Configure LCD
 //
 Lcd_Init(&PORTC);           // LCD is connected to PORTC
 Lcd_Cmd(LCD_CLEAR);
 Lcd_Out(1,1, "CALCULATOR"); // Display CALCULATOR
 Delay_ms(2000);
 Lcd_Cmd(LCD_CLEAR);
 //
 // Configure KEYPAD
 //
 Keypad_Init(&PORTB); // Keypad on PORTB
 //
 // Program loop
 //
 for(;;)                       // Endless loop
 {
  MyKey = 0;
  Op1 = 0;
  Op2 = 0;
  Lcd_Out(1,1, "No1: ");       // Display No1:
  while(1) {
   do                          // Get first number
    MyKey = Keypad_Released();
   while(!MyKey);
   if (MyKey == Enter) break;  // If ENTER pressed
   if (MyKey == 10) MyKey = 0; // If 0 key pressed
   Lcd_Chr_Cp(MyKey + '0');
   Op1 = 10*Op1 + MyKey;
  }
  Lcd_Out(2,1, "No2: ");       // Display No2:
  while(1)                     // Get second no
  {
   do
    MyKey = Keypad_Released(); // Get second number
   while(!MyKey);
   if (MyKey == Enter) break;  // If ENTER pressed
   if (MyKey == 10) MyKey = 0; // If 0 key pressed
   Lcd_Chr_Cp(MyKey + '0');
   Op2 = 10*Op2 + MyKey;
  }
  Lcd_Cmd(LCD_CLEAR);
  Lcd_Out(1,1, "Op: ");        // Display Op:
  do
   MyKey = Keypad_Released();  // Get operation
  while(!MyKey);
  Lcd_Cmd(LCD_CLEAR);
  Lcd_Out(1,1, "Res=");        // Display Res=
  switch(MyKey)                // Perform the operation
  {
  case Plus:
   Calc = Op1 + Op2;  // If ADD
   break;
  case Minus:
   Calc = Op1 - Op2;  // If Subtract
   break;
  case Multiply:
   Calc = Op1 * Op2;  // If Multiply
   break;
  case Divide:
   Calc = Op1 / Op2;  // If Divide
   break;
  }
  LongToStr(Calc, op); // Convert to string
  //
  // Remove leading blanks
  //
  j=0;
  for(i=0;i<=11;i++) {
   if (op[i] != ' ') // If a blank
   {
    lcd[j]=op[i];
    j++;
   }
  }
  Lcd_Out_Cp(lcd); // Display result
  Delay_ms(5000);  // Wait 5 seconds
  Lcd_Cmd(LCD_CLEAR);
 }
}


Figure 6.49: Modified program listing

Before using the Keypad_Released function we have to call the Keypad_Init function to tell the microcontroller what the keypad is connected to. Keypad_Released detects when a key is pressed and then released. When released, the function returns a number between 1 and 16 corresponding to the key pressed. The remaining parts of the program are the same as in Figure 6.48.

Оглавление книги


Генерация: 1.025. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз