51 single chip microcomputer button control pattern light

Publisher:JoyfulHarmonyLatest update time:2012-09-08 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/********************************************************
* 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

Previous article:LCD1602 control bus mode
Next article:Clock + stopwatch + running light assembly program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号