Getting Started with C51 MCU - Buttons

Publisher:古通闲人Latest update time:2022-05-26 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Key jitter

There is a sound when a key is pressed. Although some key press sounds are very slight, these are all vibrations caused by the collision of metal conductors; this phenomenon is called: key jitter.


If the jitter of the keys is not dealt with, the keys will cause unexpected interference to the system circuit or program. Therefore, we need to "de-bounce" the keys.

The key is effective after being pressed and lifted

Using the delay function, the key1 is pressed and then released to control the LED to flash;


#include

sbit key1 = P1^4; //key1 on the experimental board

sbit LED0 = P0^0;

unsigned char key1_down;

void delay(unsigned int delay_time)

   unsigned int j = 0;

   for(;delay_time > 0;delay_time --)

   { 

  for(j = 0;j < 125;j ++); 

   }

}

void key_pressed(void) //key scan function

{

    if(key1 == 1); //If the key is pressed

{

delay(20); //eliminate keyboard jitter

if(key1 == 1) //If the key is pressed

key1_down = 1; //Memory key1 pressed state

}

if((key1 == 0) && (key1_down == 1)) //key1 was pressed before and is now lifted

{

LED0 = !LED0; //LED flashes

key1_down = 0; //The button pressed status is cleared

}

}

void main(void) 

{

    P0 = 0x00; // Turn off all LEDs driven by P0

  while(1)

key_pressed(); //Call function    

}


The button is effective when pressed

It takes effect when the confirmation button is really pressed


/* Software debounce */                                                                                                                                  

if (0 == K1) //If a key is pressed                                                                                                                  

{                                                                                                                                             

    delay_ms(8); //delay for a while to debounce                                                                                                    

    if (0 == K1) //If a key is pressed, it is detected to be in a stable closed state                                                                                       

    {                                                                                                                               

        //Statement block; //What needs to be done after pressing the button                                                                                

    }                                                                                                                              

    while (!K1); // Release detection, if you hold down the button, the loop will continue                                                                                      

}

Keywords:C51 Reference address:Getting Started with C51 MCU - Buttons

Previous article:C51 MCU LCD1602 Driver
Next article:Buzzer (HC6800-EM3 V2.2 development board)

Recommended ReadingLatest update time:2024-11-23 04:57

C51 MCU————Bus and system expansion
1. Citation As mentioned above, the characteristics of single-chip microcomputers are small size, full functions, compact system structure, and can meet the requirements of small needs. So what about a slightly larger embedded system? Then it is possible that your data memory, program memory, and IO ports may not be
[Microcontroller]
C51 MCU————Bus and system expansion
C51 MCU LCD1602 Driver
LCD1602 Introduction LCD1602 character liquid crystal (16 characters per line, two lines in total) - a dot matrix liquid crystal module specially used to display letters, numbers and symbols. It is composed of several 5x7 or 5x10 dot matrix characters. Each dot matrix character position can be used to display a chara
[Microcontroller]
C51 MCU LCD1602 Driver
STC-C51 serial port problem
STC11L32XE      STC11L48XE    STC11L60XE Serial port issues 1.    These three chips all have two serial ports, (RXD/P1.6, TXD/P1.7) --- Port1 serial port   and   (RXD/P3.0, TXD/3.1) --- Port3 serial port. These two serial ports cannot be used at the same time, but can only be time-division multiplexed, and are selec
[Microcontroller]
51 single chip microcomputer + PWM control gradient colorful lights C51 program
1. Hardware Introduction: (Using 5050LED 2W) The RGB three-color LED control pins are P1.2, P1.1, and P1.0 of the microcontroller. The positive pole of the LED is connected to the positive pole of the main power supply (24V), and the negative pole is connected to the collector of the three transistors. The control pin
[Microcontroller]
51 single chip microcomputer + PWM control gradient colorful lights C51 program
Detailed overview of C51 program using HD44780 read and write microcontroller
Next I will introduce to you a HD44780 read-write microcontroller c51 program #include 《reg51.h》 #include 《intrins.h》 sbit GND_LCD=P1^7; sbit rs=P1^0; sbit rw=P1^1; sbit e=P1^2; unsigned char busy(void); void ctrl(unsigned char); void wd_h(unsigned char); void wd_l(unsigned char); unsigned char rd(void); void write(un
[Microcontroller]
Usage of sbit sfr in c51 programming
1 First distinguish between bit and sbit   Bit is similar to int char, except that char = 8 bits and bit = 1 bit. They are all variables, and the compiler assigns addresses during the compilation process. Unless you specify, the address is random. This address is the entire addressable space, RAM+FLASH+extension spa
[Microcontroller]
PID algorithm adjustment C51 program (1)
About PID Proportional regulation: It is to respond to the deviation of the system in proportion. Once the system has a deviation, the proportional regulation will immediately produce a regulating effect to reduce the deviation. A large proportional effect can speed up the regulation and reduce the error, but a too la
[Microcontroller]
Detailed explanation of keil C51 startup file
;------------------------------------------------------------------------------ ; STARTUP.A51: User power-on initialization routine ;------------------------------------------------------------------------------ ; ; User-defined memory space to be initialized at power-on ; ; Use the following EQU command to define the
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号