A total of four buttons are used.
#define _KEYDOWN_TEST_TIME (20)
unsigned char IsKeyDown(volatile unsigned char Value, unsigned char pin)
{
unsigned long CurState = 0, i;
for(i = 0; i < _KEYDOWN_TEST_TIME; ++i)
CurState += _GET_BIT(Value, pin)? 0:1; //The keyboard is connected to a pull-up resistor, and a low level is pressed
if(CurState == _KEYDOWN_TEST_TIME)
return 1;
return 0;
}
//All the following values are based on the calling frequency of the DealWithKey function, which is determined by the timing value of timer 2.
//The count value of a short key press. A short press is considered only when the key detection counter counts greater than or equal to this value.
#define _KEY_SHORT_CNTR (2)
//The count value of a long key press. A long press is considered only when the key detection counter counts greater than or equal to this value.
#define _KEY_LONG_CNTR (_KEY_SHORT_CNTR * 3)
//Key detection interval when no key is pressed, used for key debounce.
#define _KEY_NORMAL_DEVIDER (2)
//This value is used for the execution frequency of the button detection function after a long press.
#define _ADDMIN_FAST_DEVIDER (15)
//This value is used for the execution frequency of the button detection function after a short press.
#define _ADDMIN_LONG_DEVIDER (_ADDMIN_FAST_DEVIDER * 3)
//This value is used to set the buzzer sounding time
. #define _BEEPER_DEVIDER (10)
void DealWithKey(void)
{
static unsigned int AddCntr = 0;
static unsigned int MinCntr = 0;
static unsigned int UnitCntr = 0;
static unsigned int RunCntr = 0;
static unsigned char BeepCntr = 0;
static unsigned int DevCntr = 0;
static unsigned char LastRunState = 0;
ed char temp;
if(g_State != Setting) //Running state, all keys are invalid
return;
if(BeepCntr > 0) //Process the buzzer
--BeepCntr;
else
_BEEPER_OFF;
if(DevCntr) //Key processing divider
{
--DevCntr;
return;
}
//This button can be entered repeatedly. After long pressing, it will act at a low frequency first, and then at a high frequency after several actions.
AddCntr = IsKeyDown(_PIN(_TO_KEY_ADD), _KEY_ADD)? AddCntr + 1 : 0;
if((temp = (AddCntr >= _KEY_LONG_CNTR)) || AddCntr >= _KEY_SHORT_CNTR)
{
...
}
//Same as above
MinCntr = IsKeyDown(_PIN(_TO_KEY_MIN), _KEY_MIN)? MinCntr + 1 : 0;
if((temp = (MinCntr >= _KEY_LONG_CNTR)) || MinCntr >= _KEY_SHORT_CNTR)
{
...
}
//This button cannot be entered repeatedly, that is, it will only work after it is lifted and then pressed
UnitCntr = IsKeyDown(_PIN(_TO_KEY_UNIT), _KEY_UNIT)? UnitCntr + 1 : 0;
if(UnitCntr == _KEY_SHORT_CNTR)
{
...
}
else if(UnitCntr > _KEY_SHORT_CNTR)
{
UnitCntr = _KEY_SHORT_CNTR + 1; //Repeated execution is prohibited
}
//This button will execute other functions only after the button is lifted
if(IsKeyDown(_PIN(_TO_KEY_RUN), _KEY_RUN))
{
++RunCntr;
}
else
{
RunCntr = 0;
if(LastRunState) //Indicates that the key was lifted only after it was pressed
{
LastRunState = 0;
g_State = EmptyInflatting;
}
}
if(RunCntr == _KEY_SHORT_CNTR)
{
BeepCntr = _BEEPER_DEVIDER;
_BEEPER_ON;
LastRunState = 1; //This key is effective when it is lifted.
}
else if(RunCntr > _KEY_SHORT_CNTR)
{
RunCntr = _KEY_SHORT_CNTR + 1; //Repeated execution is prohibited
}
DevCntr = _KEY_NORMAL_DEVIDER;
}
Previous article:Multiplexing approach leads to LED displays with fewer pins
Next article:Implementation of electronic photo frame based on Atmel-View
- Popular Resources
- Popular amplifiers
- 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)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- About the disassembly and welding of this type of module
- [Repost] Introduction to the application of hard-flex PCB
- What is the output/input impedance value of CC2xxx?
- 【Project source code】The PC of FPGA transmits the image to FPGA based on USB3.0 and displays it on the TFT screen
- HX711 module schematic and driver
- MAXIM baseball cap
- The last day of 2018, let’s commemorate
- Today at 10:00 AM, live broadcast with prizes: TI's new generation C2000 microcontroller integrates powerful communication capabilities and control...
- [Mini intelligent obstacle avoidance mobile robot] Unboxing
- [NXP Rapid IoT Review] + web to get data of rapid iot kit