The program process is very simple. After the system is powered on, LED0-LED3 will light up and LED4-LED7 will go out. When SW8 is pressed for the first time, LED4-LED7 will light up and LED0-LED3 will go out. When SW8 is pressed again, LED0-LED3 will light up and LED4-LED7 will go out.
There will be some jitter when the button is pressed and released, which sometimes affects the application process, so I thought of a way to use switch to judge the state of the button.
#include
#include
char B=0,choose1=1;
void SURE_key(int Key_Num); //Define key status judgment function
//=============================
void delay_ms(int k)
{
int i,j;
for(i=0;i { for(j=0;j<850;j++) ; } } //============================= int main() { unsigned char i=0; DDRA=0xff; //Initialize port A to output PORTA=~(0xf); //Initialize port A 0-3 output low level; 4-7 output high level DDRD=0x0; //Initialize port D to all input PORTD|=0xff; //Turn on the pull-up resistor while(1) { SURE_key(~PIND&(1<<4)); //Judge the key status, press and release B++ if(B%2==1) PORTA=(0xf); else PORTA=~(0xf); } return 0; } void SURE_key(int Key_Num) { switch(choose1) { case 1: if(Key_Num!=0) choose1=2; //Is it pressed? else choose1=1; break; case 2: if(Key_Num!=0) choose1=3; //Is it pressed? else choose1=1; break; case 3: if(Key_Num!=0) choose1=3; //Pressed! Released? else choose1=4; break; case 4: //Released B++; choose1=1; break; } }
Previous article:AVR microcontroller drives LCD1602
Next article:Atmega16 timer time0
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
- EEWORLD University ---- Live Replay: Microchip Security Series 10 - Automotive Cybersecurity
- Appearance characteristics of chip resistors
- Has anyone used a 15v to 3.3v converter?
- When low EMI power supplies meet crowded circuit boards, what do you do?
- EEWORLD University ---- OpenCV 3 with Python 3 Tutorial
- Siemens 230RC opens for 3 seconds and closes for 3 seconds
- Stack Overflow Technology from Entry to Mastery
- Design of Automatic AC Voltage Stabilizer
- Competition control competition materials and excellent works sharing
- NUCLEO_G431RB Review (1) Compilation Environment Settings