Mega16 keypad and digital display program

Publisher:HeavenlyJoy444Latest update time:2015-07-21 Source: 51heiKeywords:Mega16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include  
#include  
#define No_key 255 
#define K1_11 
#define K1_22 
#define K1_33 
#define K1_4    4 
#define K2_15 
#define K2_26 
#define K2_37 
#define K2_4    8 
#define K3_19
#define K3_20
#define K3_310
#define K3_4 11
#define K4_112
#define K4_213
#define K4_314
#define K4_4 15
#define Key_mask0b00001111   
#define data PORTB.0 
#define clk PORTB.1  
flash unsigned char led[16]={0x28,0xeb,0x19,0x89,0xca,0x8c,0x0c,0xe9,0x08,0x88, 
                             0xdf,0x4a,0x1c,0x3e,0x7e,0x2a}; 
unsigned charkey_stime_counter; 
unsigned char   key_temp; 
bitkey_stime_ok;  
//D0 is connected to the keyboard, the lower 4 bits are column line input, and the upper 4 bits are row line output
unsigned char read_keyboard() 
{ 
 static unsigned char key_state = 0, key_value, key_line; 
 unsigned char key_return = No_key,i; 
 switch (key_state) 
 { 
  case 0: 
  key_line = 0b00010000; 
  for (i=1; i<=4; i++) // Scan the keyboard
  { 
         PORTA = ~key_line; // Output line level
 PORTA = ~key_line; // Must be sent twice!!!
 key_value = Key_mask & PINA; // Read column level
 if (key_value == Key_mask) 
 key_line <<= 1; // No key pressed, continue scanning
 else 
 {
  key_state++; // Stop scanning when a key is pressed
  break; // Switch to debounce confirmation state
 } 
} 
break; 
  case 1: 
        if (key_value == (Key_mask & PINA)) // Read the column level again,
{
 switch (key_line | key_value) // Same as state 0, confirm key
 {//Keyboard encoding, return encoding value  
  case 0b00011110: 
       key_return = K1_1; 
       break; 
  case 0b00011101: 
       key_return = K1_2; 
       break;
          case 0b00011011: 
       key_return = K1_3; 
       break;
  case 0b00010111: 
       key_return = K1_4; 
       break;      
  case 0b00101110: 
       key_return = K2_1; 
       break;
   case 0b00101101: 
       key_return = K2_2; 
       break;
  case 0b00101011: 
       key_return = K2_3; 
       break;
  case 0b00100111: 
       key_return = K2_4; 
       break;
   case 0b01001110: 
       key_return = K3_1; 
       break;
   case 0b01001101: 
       key_return = K3_2; 
       break;
  case 0b01001011: 
       key_return = K3_3; 
       break;  
  case 0b01000111: 
       key_return = K3_4; 
       break;
   case 0b10001110: 
       key_return = K4_1; 
       break;
   case 0b10001101: 
       key_return = K4_2; 
       break;
  case 0b10001011: 
       key_return = K4_3; 
       break;  
  case 0b10000111: 
       key_return = K4_4; 
       break;
 }
key_state++; // Enter the state of waiting for key release
       } 
       else 
 {
  key_state--; 
  delay_ms(5); 
 }// The two column levels are different and return to status 0 (debounce processing)
 break; 
  Case 2: Waiting for the button to be released
      PORTA = 0b00001111; // All lines output low level
      PORTA = 0b00001111; // repeat once
      if ( (Key_mask & PINA) == Key_mask) 
      key_state=0; // All column lines are high level and return to state 0
      break; 
 }
 return key_return; 
}
//Send data to the digital tube
void sendbyte(unsigned char byte) 
{     
 unsigned char num,c;  
 num=led[byte]; 
 for(c=0;c<8;c++) 
 {
  clk=0; 
  data=num&0x01; 
  clk=1; 
  num>>=1; 
 }
}
void display(void) 
{
 if (key_stime_ok) 
  {
   key_stime_ok = 0; // 10ms to
   key_temp = read_keyboard(); // Call the keyboard interface function to read the keyboard
   if (key_temp != No_key) 
   {// A key is pressed
    sendbyte(key_temp); 
    delay_ms(10); 
   } 
  } 
}
void main(void) 
{
 DDRB = 0xFF; 
 //PORTC = 0xFF; //Keyboard interface initialization
 DDRA = 0xF0; // PD2, PD1, PD0 column lines, input mode, pull-up valid
 //T/C0 initialization  
 TCCR0=0x0B; // Internal clock, 64 division (4M/64=62.5KHz), CTC mode
 TCNT0=0x00; 
 OCR0=0x7C;// OCR0 = 0x7C(124),(124+1)/62.5=2ms 
 TIMSK=0x02; // Enable T/C0 compare match interrupt
 #asm("sei")// Open global interrupt
 while (1) 
 { 
  display(); 
 } 
}      
// Timer 0 compare match interrupt service, 2ms timing
interrupt [TIM0_COMP] void timer0_comp_isr(void) 
{ 
 //display();// Call LED scan display
 if (++key_stime_counter >=5) 
 {
  key_stime_counter = 0; 
  key_stime_ok = 1; // 10ms to
 }
}
Keywords:Mega16 Reference address:Mega16 keypad and digital display program

Previous article:AVR microcontroller drives 12864 LCD program
Next article:Mega8515 matrix keyboard and LED display program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号