Hardware Hookup
From the circuit connection diagram, we can see that one end of the 8 independent buttons is grounded and the other end is connected to the socket JP5.
On the development board, JP5 is connected to the P1 port through an external cable. The microcontroller reads the status of the P1 port (0 or 1) to determine which button is pressed.
software design
Writing a key scanning function
/****************************************************** ***********
* Function name: duli_key_scan
* Function: Identify which key is pressed
* Input: None
* Output: key value 1~8
*************************************************** **********/
unsigned char duli_key_scan(void)
{
static unsigned char key_num;
if (KEY != 0xff)
{ //Indicates that a key is pressed
delay10ms(); //delay to eliminate jitter
if ((KEY != 0xff))
{ //A key is pressed
switch (KEY)
{
case 0xfe :
key_num = 1;
break;
case 0xfd :
key_num = 2;
break;
case 0xfb :
key_num = 3;
break;
case 0xf7 :
key_num = 4;
break;
case 0xef :
key_num = 5;
break;
case 0xdf :
key_num = 6;
break;
case 0xbf :
key_num = 7;
break;
case 0x7f :
key_num = 8;
break;
}
}
}
return key_num;
}
According to the key value returned by the key scanning function, write the LED display program to light up different LEDs.
LED display program
void led(unsigned char num)
{
//Code value when 8 LEDs are lit individually
unsigned char led[9] = {0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
LED = led[num];
}
Burn to the development board
Note: There are two key modes (the only difference between the two codes is the keyword static)
The first type: press the light on, release the light off (no static)
The second type: the light turns on when the button is pressed, and remains unchanged when the button is released until the next press, when the light turns on. (With static).
Extensions
Buttons move the LED light left and right (button 1 moves right, button 2 moves left)
Press the button to select the pattern display (each button is like a mode switch, press it to display different modes)
Previous article:Microcontroller learning record - buzzer
Next article:Microcontroller learning record - playing with LED
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- What IC is this???
- MM32F103 Development Board Initial Impressions
- With the configuration parameter table, can AT commands be customized?
- Sincerely recruiting embedded software and hardware engineers
- Characteristics, functions and precautions of chopper
- Copper plating under the DC conversion power inductor
- Regarding FPGA development, compared with several major companies' EDA design tools, Xilinx's Vivado is still the best
- How to simulate I2C communication using GPIO on C2000
- Never forget the teacher's kindness, teachers, happy holidays~~
- ★★ If you don’t read this before you start to learn embedded systems, you will definitely regret it.