#include
Keywords:Mega16
Reference address:Mega16 keypad and digital display program
#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 } }
Previous article:AVR microcontroller drives 12864 LCD program
Next article:Mega8515 matrix keyboard and LED display program
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
Guess you like
- MicroPython officially releases pyb D series development board
- Ask for the direction positioning principle of Bluetooth 5.1
- A few pictures help you easily understand DDR crosstalk
- cs+ cannot simulate debugging
- C language STM8 program help
- Will Wi-Fi 6 replace Wi-Fi 5 as the mainstream wireless standard?
- Last day! TI live broadcast with prizes | Application of low power technology in Wi-Fi camera and PIR infrared sensor design
- Today's live broadcast: The new Infineon MOSFET based on Source-down technology effectively improves power density, which is visible to the naked eye
- DIY a WS2812 clock
- [NUCLEO-L552ZE Review] Unboxing and Onboard Resource Analysis