/********************************************************
* This program uses buttons to control pattern lights. *
* When K1 is pressed, the light runs from 0xfe to the left; *
* When K2 is pressed, the LED light runs from 0x7f to the right, reaches 0xfe and runs right back to the starting position; *
* When the K3 key is pressed, the LED light runs once from 0xfe in a running light pattern, and then flows back. *
* When the K4 key is pressed, the first four LED lights light up first, and then turn to light up the last four. *
* When the K5 key is pressed, any ongoing program ends and all LED lights go out. *
**********************************************************/
**************************************************
Connection method: P0 connects to independent button JP5; P2 connects to LED light interface JP1 *
***********************************************************/
#include //Header file, function declaration
#include
//Define the button position
sbit K1=P0^0;
sbit K2=P0^1;
sbit K3=P0^2;
sbit K4=P0^3;
sbit K5=P0^4;
unsigned char led;
unsigned char j;
void delayms(unsigned char ms) // 1ms standard delay
{
while(ms--)
{
for(j=0;j<110;j++); //It is still impossible to set a more standard delay, such as 1S; so the timer delay should be used for the most accurate timer
}
}
void main()
{ //P2=led;
unsigned int i;
while(1)
{
/*************************************************
* When K1 is pressed, the light runs from 0xfe to the left; *
*********************************************/
if(K1==0)
delayms(10); //Eliminate keyboard jitterif
(K1==0)
{
led=0xfe;
for(i=0;i<7;i++) //i can be a multiple of 8, indicating how many times this circuit is executed
{
//led=0x7f; // The initial definition of port P should be outside the loop, otherwise the effect will be messed up
P2=led; //Because a light has been initially set here, this
delayms(100) should be added to for; //Initially, 500MS is set here, so that the light color will move one each time the key is pressed, which cannot meet the requirements.
led=_crol_(led,1);
P2=led;
delayms(100);
}
while(!K1); //Here it is emphasized that if you release the button, the program will not be executed again.
P2=led; //Indicates the final state after executing the above program
. } //P2=led; // If this line is placed here, although it is not stated in the beginning that a light will be displayed after the program is burned, since this line is outside the if, it will still be displayed in the end.
/***************************************************************
*When K2 is pressed, the LED light runs from 0x7f to the right to 0xfe and then runs back to the starting position. *
***************************************************************/
if(K2==0)
delayms(10);
if(K2==0)
{
led=0x7f;
for(i=0;i<7;i++)
{
P2=led;
delayms(100);
led=_cror_(led,1);
P2=led;
delayms(100);
}
while(!K1);
P2=led;
delayms(100);
for(i=0;i<7;i++)
{
led=_crol_(led,1);
P2=led;
delayms(100); //How come it runs back so fast?
} //At this time led=0x7f
}
/************************************************************
*When the K3 key is pressed, the LED light starts from 0xfe and runs in a running light mode, and then flows back. *
*************************************************/
if(K3==0)
delayms(10); //eliminate jitterif
(K3==0)
{
led=0xfe;
delayms(200);
for(i=0;i<7;i++)
{
P2=led;
delayms(200);
led=led<<1; // Note led=_crol_(led,1)is equal to led<<1||led>>7;
P2=led;
delayms(200);
if(led==0x00)
break; //There must be a break here to jump out of the loop, and don't forget that the state at this time is 00, not 7f
}
while(!K1);
led=0x7f;
P2=led;
delayms(200 );
for(i=0;i<7;i++)
{
led=led>>1;
P2=led;
delayms(200);
}
}
/********************************************************
*When the K4 key is pressed, the first four LEDs light up first, and then turn to the last four. *
****************************************************/
if(K4==0)
delayms(10); //eliminate jitterif
(K4==0)
{
for(i=0;i<4;i++) //Execute four times
{ led=0xf0;
P2=led;
delayms(500);
led=0x0f;
P2=led;
delayms(500);
}
}
/************************************************************
*When the K5 key is pressed, end any ongoing program and turn off all LEDs. *
********************************************************/
if(K5==0)
delayms(10);
if(K5==0)
P2=0xff;
}
}
/**** */
Reference address:51 single chip microcomputer button control pattern light
* This program uses buttons to control pattern lights. *
* When K1 is pressed, the light runs from 0xfe to the left; *
* When K2 is pressed, the LED light runs from 0x7f to the right, reaches 0xfe and runs right back to the starting position; *
* When the K3 key is pressed, the LED light runs once from 0xfe in a running light pattern, and then flows back. *
* When the K4 key is pressed, the first four LED lights light up first, and then turn to light up the last four. *
* When the K5 key is pressed, any ongoing program ends and all LED lights go out. *
**********************************************************/
**************************************************
Connection method: P0 connects to independent button JP5; P2 connects to LED light interface JP1 *
***********************************************************/
#include
#include
//Define the button position
sbit K1=P0^0;
sbit K2=P0^1;
sbit K3=P0^2;
sbit K4=P0^3;
sbit K5=P0^4;
unsigned char led;
unsigned char j;
void delayms(unsigned char ms) // 1ms standard delay
{
while(ms--)
{
for(j=0;j<110;j++); //It is still impossible to set a more standard delay, such as 1S; so the timer delay should be used for the most accurate timer
}
}
void main()
{ //P2=led;
unsigned int i;
while(1)
{
/*************************************************
* When K1 is pressed, the light runs from 0xfe to the left; *
*********************************************/
if(K1==0)
delayms(10); //Eliminate keyboard jitterif
(K1==0)
{
led=0xfe;
for(i=0;i<7;i++) //i can be a multiple of 8, indicating how many times this circuit is executed
{
//led=0x7f; // The initial definition of port P should be outside the loop, otherwise the effect will be messed up
P2=led; //Because a light has been initially set here, this
delayms(100) should be added to for; //Initially, 500MS is set here, so that the light color will move one each time the key is pressed, which cannot meet the requirements.
led=_crol_(led,1);
P2=led;
delayms(100);
}
while(!K1); //Here it is emphasized that if you release the button, the program will not be executed again.
P2=led; //Indicates the final state after executing the above program
. } //P2=led; // If this line is placed here, although it is not stated in the beginning that a light will be displayed after the program is burned, since this line is outside the if, it will still be displayed in the end.
/***************************************************************
*When K2 is pressed, the LED light runs from 0x7f to the right to 0xfe and then runs back to the starting position. *
***************************************************************/
if(K2==0)
delayms(10);
if(K2==0)
{
led=0x7f;
for(i=0;i<7;i++)
{
P2=led;
delayms(100);
led=_cror_(led,1);
P2=led;
delayms(100);
}
while(!K1);
P2=led;
delayms(100);
for(i=0;i<7;i++)
{
led=_crol_(led,1);
P2=led;
delayms(100); //How come it runs back so fast?
} //At this time led=0x7f
}
/************************************************************
*When the K3 key is pressed, the LED light starts from 0xfe and runs in a running light mode, and then flows back. *
*************************************************/
if(K3==0)
delayms(10); //eliminate jitterif
(K3==0)
{
led=0xfe;
delayms(200);
for(i=0;i<7;i++)
{
P2=led;
delayms(200);
led=led<<1; // Note led=_crol_(led,1)is equal to led<<1||led>>7;
P2=led;
delayms(200);
if(led==0x00)
break; //There must be a break here to jump out of the loop, and don't forget that the state at this time is 00, not 7f
}
while(!K1);
led=0x7f;
P2=led;
delayms(200 );
for(i=0;i<7;i++)
{
led=led>>1;
P2=led;
delayms(200);
}
}
/********************************************************
*When the K4 key is pressed, the first four LEDs light up first, and then turn to the last four. *
****************************************************/
if(K4==0)
delayms(10); //eliminate jitterif
(K4==0)
{
for(i=0;i<4;i++) //Execute four times
{ led=0xf0;
P2=led;
delayms(500);
led=0x0f;
P2=led;
delayms(500);
}
}
/************************************************************
*When the K5 key is pressed, end any ongoing program and turn off all LEDs. *
********************************************************/
if(K5==0)
delayms(10);
if(K5==0)
P2=0xff;
}
}
/**** */
Previous article:LCD1602 control bus mode
Next article:Clock + stopwatch + running light assembly program
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- [TI High Precision Laboratory] In the course, when calculating the offset voltage error, is it not necessary to calculate the offset voltage error of the second-stage op amp?
- Does anyone know: Can one ZLL remote control control 3 ZLL lights at the same time?
- Does anyone have the Chinese version of the RL78F12 chip datasheet?
- Optimizing EMC and Efficiency in High Power DC/DC Converters Part 1
- Common Problems in RF Circuit Design
- Telink Microelectronics Matter Development Guide (I): The Past and Present of Matter
- Example of chip drain damage
- F280049C CAN_ex3 Issue
- [Factory Visit Live Replay] Arrived at the destination - TE Qingdao Factory
- Useful tutorial | How to quickly splice PCB data at unconventional angles?