Because the course has a single-chip microcomputer, today I took out the TX-B development board I bought last year to play with it. I wrote a single-chip microcomputer program about row and column scanning.
Reference: Matrix keyboard detection
Connection circuit diagram of keyboard and single-chip microcomputer Connection
circuit diagram of 6-bit digital tube and single-chip microcomputer
Common cathode digital tube digital coding
The four rows of the matrix keyboard are connected to P3.0-P3.3 respectively, and the four columns are connected to P3.4-P3.7 respectively.
The code is written using "KeilC51v612", and the code is as follows:
Phone Keypad
1#include
2//Simulate the telephone keypad, press a key, and the corresponding value will be displayed. The rows and columns are arranged in the 3 x 4 way of ordinary telephones.
3unsigned char t, key, a, count;
4
5sbit light8 = P1^7;
6//Latch end of the latch that controls the digital tube segment selection. Latch after the falling edge, that is, first set dula = 1, then set dula = 0 to complete the latch
7sbit dula=P2^6;
8//Latch end of the latch that controls the digital tube bit selection process is similar to dula
9sbit wela=P2^7;
10
11unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
12 0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-F digital tube coding
13void display(unsigned char num) //digital tube display function
14{
15 P0=table[num]; //Send the numth display code to the P0 port and then to the bus
16 dula=1; //Generate a falling edge to latch the data
17 dula=0;
18 P0 = 0xc0;
19 wela=1; //same as dula
20 wela=0;
twenty one}
twenty two
23//Get the key value by checking the columns. base is the value of the first column of each row. The rows and columns here are arranged in the 3 x 4 format of ordinary phones.
24unsigned char getKey(unsigned char da, unsigned char base)
25{
26 switch(da)
27 {
28 case 0xe0: //Detect the first column
29 return base;
30 case 0xd0: //Detect the second column
31 return base + 1;
32 case 0xb0: //Detect the third column
33 return base + 2;
34 default : //Others return the value of the last press
35 return key;
36 }
37}
38
39//Wait for the key to be released. If the key is still pressed, light up the LED 8.
40void wait()
41{
42 while(t != 0xf0)
43 {
44 t = P3;
45 t = t & 0xf0;
46 light8 = 0;
47 }
48 light8 = 1;
49}
50
51//Delay function, for example, if i=10, the delay is about 10ms.
52void delay(unsigned char i)
53{
54 unsigned char j, k;
55 for(j = i; j > 0; j--)
56 {
57 for(k = 125; k > 0; k--);
58 }
59}
60
61void main()
62{
63 dula = 0; //Initialize dula and wela
64 wela = 0;
65 while(1)
66 {
67 a = 0xfe; //Check the first line first
68 for(count = 0; count < 4; count ++)
69 {
70 P3 = a;
71 t = P3 & 0xf0; //Read P3 port
72 if(t != 0xf0) //Judge whether a key is pressed
73 {
74 delay(10); //delay 10ms to de-bounce
75 t = P3 & 0xf0; // Check again whether a key is pressed
76 if(t != 0xf0)
77 {
78 switch(count) //Determine which row has a key pressed
79 {
80 case 0:
81 key = getKey(t, 1); //Get the key value of the first row
82 break;
83 case 1:
84 key = getKey(t, 4);
85 break;
86 case 2:
87 key = getKey(t, 7);
88 break;
89 case 3:
90 if(t == 0xd0) //Check if a key is pressed in row 4, column 2
91 key = 0;
92 break;
93 }
94 wait();
95 display(key);
96 P1 = a; //Prompt which row has a key pressed
97 }
98 }
99 a = (a << 1) | (a >> 7); //Continue to detect the next line, circularly shift left one bit to achieve
100 }
101 }
102}
Haha, my major is very strange, I have to master both software and hardware. I often can't grasp the direction, so I have to learn both. But I still think software is more suitable for me.
Thanks to Brother Guo's experimental board, it is also with this board that I can quickly develop a great interest in single-chip microcomputers.
Functional diagram of TX-1B experimental board (currently replaced by TX-1C)
Previous article:Principle and example program of 12864 dot matrix LCD display module (HJ12864M-1)
Next article:MCU Exercise - AD Conversion
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Start with a Routine
- Share a reference book on servo motor drive
- Is FPGA a big deal in the field of artificial intelligence?
- 【GD32F310G-START】Voltage meter
- C language algorithm to calculate the age of beautiful women
- LOTO virtual oscilloscope about trigger sensitivity function
- [ST60 short-distance test] Part 2: Communication rate test
- PT4115 cannot be completely shut down
- STM32 Linux development board recommendation | PHYTEC development board: Help you reduce development risks and improve product stability
- FAQ|Beineng International's new glass breakage detection solution