Friday, May 23, 2014

Example of Keypad 4x4 Program

Schematic Diagram for 4x4 Keypad

//Keypad Driver: flex_kbd4x4.c
#define row0 PIN_B4 
#define row1 PIN_B5 
#define row2 PIN_B6 
#define row3 PIN_B7 
#define col0 PIN_B0 
#define col1 PIN_B1 
#define col2 PIN_B2 
#define col3 PIN_B3 

// Keypad layout: 
char const KEYS[4][4] = 
{{'7','8','9','/'}, 
 {'4','5','6','X'}, 
 {'1','2','3','-'}, 
 {'*','0','=','+'}}; 


#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where 
// n is the number of times you expect 
// to call kbd_getc each second 

void kbd_init() 
//set_tris_b(0xF0); 
//output_b(0xF0); 
port_b_pullups(true);  

short int ALL_ROWS (void) 
if(input (row0) & input (row1) & input (row2) & input (row3)) 
   return (0); 
else 
   return (1); 



char kbd_getc() 
static byte kbd_call_count; 
static short int kbd_down; 
static char last_key; 
static byte col; 

byte kchar; 
byte row; 

kchar='\0'; 

if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) 
  { 
   switch (col) 
     { 
      case 0: 
        output_low(col0); 
        output_high(col1); 
        output_high(col2); 
        output_high(col3); 
        break; 
    
      case 1: 
        output_high(col0); 
        output_low(col1); 
        output_high(col2); 
        output_high(col3); 
        break; 

      case 2: 
        output_high(col0); 
        output_high(col1); 
        output_low(col2); 
        output_high(col3); 
        break; 

      case 3: 
        output_high(col0); 
        output_high(col1); 
        output_high(col2); 
        output_low(col3); 
        break; 
      } 

   if(kbd_down) 
     { 
      if(!ALL_ROWS()) 
        { 
         kbd_down=false; 
         kchar=last_key; 
         last_key='\0'; 
        } 
     } 
   else 
     { 
      if(ALL_ROWS()) 
        { 
         if(!input (row0)) 
            row=0; 
         else if(!input (row1)) 
            row=1; 
         else if(!input (row2)) 
            row=2; 
         else if(!input (row3)) 
            row=3; 

         last_key =KEYS[row][col]; 
         kbd_down = true; 
        } 
      else 
        { 
         ++col; 
         if(col==4) 
            col=0; 
        } 
     } 
   kbd_call_count=0; 
  } 
return(kchar); 

//**********************************************************
//Main Program

#include <18F4550.h> // PIC18F4550 HEADER FILE
#fuses XT,NOWDT,NOLVP,NOPROTECT // EXTERNAL CLOCK, NO WATCH DOG TIMER, NO LOW VOLTAGE 
#device adc=10 // USE 10 BIT ADC QUANTIZATION
#use delay (clock=4M) // 4 MHZ CRYSTAL

#include <flex_lcd420.c>
#include <flex_kbd4x4.c>


//Main Program

void main()
{  
   char k;
   
   lcd_init();
   kbd_init();

   lcd_putc("\fReady...\n");
   //Pin Configuration
   
   while(true)
   {
      //Program start here
      k=kbd_getc();
      if(k!=0)
        if(k=='*')
         {
          lcd_putc('\f');
          delay_ms(100);
          lcd_putc("\fReady...\n");
         }
        else
          lcd_putc(k);
   }

}


2 comments:

Unknown said...

nice... electronic engineering companies

Unknown said...

Hello everyone, I'm Patricia Sherman in Oklahoma USA right now. I would like to share with you my experience of borrowing USD $185,000.00 to clear my bank draft and start a new business. It all started when I lost my house and I took my stuff because of the bank policy and I met some bills and some personal needs. So I became very desperate and started looking for funds in every way. Fortunately for me, a friend of mine, Linda told me about a credit company firm, I was intrigued by the fraud, but I was intrigued by my situation and had no choice but to get advice from my friend about this company. contacting them really doubted me because of my past experience with online lenders, did you know that little? '' Elegantloanfirm@hotmail.com This company has been very helpful to me and my colleague and today, thanks to this credit company, the proud owner of well-organized work and responsibilities, they smiled back at me. So if you really need to grow or start your own business, or if you really need to borrow money in any financial hardship, we recommend you find a financial development opportunity in your business today. {E-mail:} Elegantloanfirm@hotmail.com / whats-app number +393511617486.... online for credit not a victim of scam Thank you.