1. Button and round-robin method to process buttons (based on S5PV210)
1. What is a button?
1. Physical characteristics of buttons
(1) When no one is pressing the button, the spring will pop it open. At this time, the internal circuit is disconnected.
(2) When someone presses the button, the force of the hand overcomes the elastic force of the spring and pushes the button down. At this time, the internal part remains in the connected (closed) state; if the hand is removed, the button pops open again under the action of the spring and the internal part is disconnected again.
(3) Generally, a button has 4 pins, which are divided into 2 pairs: one pair is a normally open contact (open when not pressed and closed when pressed as described above); the other pair is a normally closed contact (closed when not pressed and open when pressed).
2. Electrical principle of buttons (combined with schematic diagram analysis)
(1) Hardware connection: SW5: GPH0_2, SW6: GPH0_3, SW7 8 9 10: GPH2_0 1 2 3
(2)**Circuit connection analysis of the button: When the button is not pressed, the button is disconnected internally and the voltage at the GPIO pin is high. When someone presses the button, the button is turned on internally, and the external VDD is connected to the ground through the resistor and the button to form a loop. At this time, the voltage at the GPIO pin becomes low. **At this time, the VDD voltage is completely divided on the resistor (this resistor is called a voltage divider resistor, and this resistor cannot be too small because the power of the resistor is U*U/R)
(3) Summary: The working method of the button is actually the pressing and releasing of the button, which correspond to the two level states of GPIO (GPIO is low level when pressed and high level when released). At this time, the SoC can determine whether the button is pressed by detecting the level of the GPIO, and this judgment result can be used as the input signal of the SoC.
3. Buttons are input devices
(1) Buttons are generally used as input devices (devices that send information from people to SoC are called input devices), and people send key signals to SoC (there are two types of key signals: press signals and pop-up signals).
(2) Some devices are pure input devices, such as buttons and touch screens; some devices are pure output devices, such as LCDs; and some devices can do both input and output, called input and output devices (IO), such as serial ports.
4. Two response methods for buttons
(1) There are two ways to process keystrokes on SoC: polling and interrupt.
(2) Polling method: The SoC actively reads the GPIO voltage level (corresponding to the key) at regular intervals to obtain key information. The disadvantage is that the CPU has to pay attention to the key event all the time, which will affect the CPU from doing other things.
(3) Interrupt mode: The SoC sets the interrupt handler ISR corresponding to the interrupt triggered by the GPIO in advance. When the external button is pressed or released, the external interrupt corresponding to the GPIO is automatically triggered, causing the ISR to be executed, thereby automatically processing the button information.
2. Polling method to process buttons
1. How to connect the buttons of X210 development board
(1) Check the schematic diagram and find the GPIO corresponding to the button: SW5: GPH0_2 SW6: GPH0_3 SW7 8 9 10: GPH2_0 1 2 3
(2) It can be seen from the schematic diagram that when pressed, it is a low level, and when it pops up, it is a high level (the schematic diagram is shown in the figure above)
2. GPIO mode settings corresponding to buttons
(1) The button is connected to the GPIO. Whether the button is pressed or released determines whether the external circuit is connected, thereby determining whether the voltage of the GPIO pin is high or low. This voltage can be used as the input signal of the GPIO pin. At this time, the GPIO is configured in input mode, and the level of the pin can be read from the SoC to be 1 or 0 (1 corresponds to high level, 0 corresponds to low level).
(2) GPH0CON (0xE0200C00) GPH0DAT (0xE0200C04) GPH2CON (0xE0200C40) GPH2DAT (0xE0200C44) These are the registers and register addresses corresponding to the keys.
(3) Set the GPIO to input mode in the CON register, and then read the DAT register (a value of 1 in the corresponding bit indicates that the external state is high (the corresponding key is up), and a value of 0 indicates that the external state is low (the key is pressed))
3. Program flow of processing buttons in polling mode
(1) The first step is to initialize the GPIO mode to input;
(2) The second step is to loop through the GPIO level values and determine whether a key is pressed.
4. Code writing and debugging
Add key.c file
// Define macros for operating registers
#define GPH0CON 0xE0200C00
#define GPH0DAT 0xE0200C04
#define GPH2CON 0xE0200C40
#define GPH2DAT 0xE0200C44
#define rGPH0CON (*(volatile unsigned int *)GPH0CON)
#define rGPH0DAT (*(volatile unsigned int *)GPH0DAT)
#define rGPH2CON (*(volatile unsigned int *)GPH2CON)
#define rGPH2DAT (*(volatile unsigned int *)GPH2DAT)
// Initialize the buttons
void key_init(void)
{
// Set the GPHxCON register to input mode
// Set all bits 8 to 15 of GPH0CON to 0.
rGPH0CON &= ~(0xFF<<8);
// Set all bits 0 to 15 of GPH2CON to 0.
rGPH2CON &= ~(0xFFFF<<0);
}
//Round-robin mode to process button functions
void key_polling(void)
{
// Read the value of each GPIO one by one to determine whether the value is 1 or 0. If it is 1, the button is pressed, and if it is 0, it pops up
// Polling means repeatedly checking if a key is pressed, with a short interval
while (1)
{
// Corresponds to the button marked LEFT on the development board
if (rGPH0DAT & (1<<2))
{
// is 1, indicating no key is pressed
led_off();
}
else
{
// is 0, indicating that there is a key
led1(); //Call the led() function to debug the program
}
// Corresponds to the button marked DOWN on the development board
if (rGPH0DAT & (1<<3))
{
// is 1, indicating no key is pressed
led_off();
}
else
{
// is 0, indicating that there is a key
led2();
}
// Corresponds to the button marked UP on the development board
if (rGPH2DAT & (1<<0))
{
// is 1, indicating no key is pressed
led_off();
}
else
{
// is 0, indicating that there is a key
led3();
}
}
}
3.Serial port output and key debounce
1. Key debugging based on serial port standard output
(1) Based on the previous serial port stdio project, we transplanted and added polling mode key processing.
2. What is key debounce?
(1) Physical devices such as buttons have jitter signals. Jitter signals refer to the process in which the level changes from high to low (that is, when the button is pressed) or from low to high (that is, when the button is released). Instead, the level changes after a period of instability. During this unstable period, the level may change repeatedly from high to low. This unstable period is called jitter (it is unreliable to obtain button information during the jitter period, and methods must be found to eliminate jitter).
(2) What is debounce? Debounce is to use hardware or software methods to minimize the impact of the jitter period on key acquisition.
There are two common ways to eliminate jitter:
The first is hardware de-jittering. The idea of de-jittering is to reduce the jitter time as much as possible. The method is to reduce the jitter by adding components such as capacitors through hardware.
The second is software debouncing. The idea of debouncing is that after a key press/pop-up event is discovered, the key is not processed immediately, but the key value is obtained again after a delay of a period of time (usually 10 to 20ms, which is the debouncing time). If the key value is the same as the last press/pop-up, it is considered to be really pressed/popped up.
(3) When more precise measurement is required, both hardware and software de-jittering should be used together.
3. Programming Practice
Add standard input and output debugging buttons and add button debounce
#include "stdio.h"
// Define macros for operating registers
#define GPH0CON 0xE0200C00
#define GPH0DAT 0xE0200C04
#define GPH2CON 0xE0200C40
#define GPH2DAT 0xE0200C44
#define rGPH0CON (*(volatile unsigned int *)GPH0CON)
#define rGPH0DAT (*(volatile unsigned int *)GPH0DAT)
#define rGPH2CON (*(volatile unsigned int *)GPH2CON)
#define rGPH2DAT (*(volatile unsigned int *)GPH2DAT)
// Initialize the buttons
void key_init(void)
{
// Set the GPHxCON register to input mode
// Set all bits 8 to 15 of GPH0CON to 0.
rGPH0CON &= ~(0xFF<<8);
// Set all bits 0 to 15 of GPH2CON to 0.
rGPH2CON &= ~(0xFFFF<<0);
}
void delay20ms(void)
{
// This function delays 20ms
// Because this is a bare metal program, and the point is not really to eliminate the jitter, but to teach
// So this program here is just symbolic and has no entity
// If it is R&D, you need to spend time to debug a program with a delay of 20ms
int i, j;
for (i=0; i<100; i++)
{
for (j=0; j<1000; j++)
{
i * j;
}
}
}
void key_polling(void)
{
// Read the value of each GPIO one by one to determine whether the value is 1 or 0. If it is 1, the button is pressed, and if it is 0, it pops up
// Polling means repeatedly checking if a key is pressed, with a short interval
while (1)
{
// Corresponds to the button marked LEFT on the development board
if (rGPH0DAT & (1<<2))
{
// is 1, indicating no key is pressed
led_off();
}
else
{
// Add debounce
// First step, delay
delay20ms();
// Step 2: Check the button status again
// Delay debounce before checking the button status, and then judge the button status after debounce. Delay debounce is required before judging whether it is popped up or pressed. We just give an example here
if (!(rGPH0DAT & (1<<2)))
{
// is 0, indicating that there is a key
led1();
printf("key left.n"); //Add the previous standard input and output. We can use printf to debug the program here.
}
}
// Corresponds to the button marked DOWN on the development board
Previous article:S3C2440 bare metal lights up the LED (directly modify the machine code)
Next article:S3C2440 (4.3-inch) LCD driver level analysis (16)
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- ADC Research of msp430F5438A
- Please help analyze how the following negative voltage is generated and the direction of current flow, thank you
- 15 yuan free shipping: high quality, Sensirion digital temperature and humidity sensor SHT31 (limited quantity, hurry up)
- How to detect motor speed?
- What are the maskable interrupts of MSP430_The priority of MSP430 interrupts
- Is this type of mask = medical surgical mask?
- Some Summary of Differential Mode and Common Mode
- TCP network communication problem
- Ask for help from all the senior experts!!
- Using Ginkgo USB-ADC and heart rate sensor to implement a heart rate tester with Android APP source code