Various patterns of running lights c51 program based on 51 single chip microcomputer

Publisher:SparkleMagicLatest update time:2012-09-24 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*-----------------------------------------------
Function: The running light moves and flashes symmetrically (double flashing)

----------------------------------------*/

#include
#define uint unsigned int
void delay(uint);
main()
{
uint comp1=0xfe;
uint comp2=0x80;
P1=0x7e;
delay(30000);
while(1)
{
P1=0xff;
comp1<<=1;
comp1|=0x01;
comp2>>=1;
P1&=comp1;
P1^=comp2;
delay(30000);
if(P1==0xe7)
{
comp1<<=1;
comp1|=0x01;
comp2>>=1;
}

if(comp1==0x7f)
{
comp1=0xfe;
comp2=0x80;
}
}
}
void delay(uint cnt)
{
while(cnt--);
}


/*------------------------------------------------ ------------------

It only loops once, but not all the time. The error is:

By adding a test statement:

if(comp1==0x7f)
{
comp1=0xfe; comp2=0x80;
P1=0x00; delay(30000);

}

Finding that the if statement is not executed, it naturally continues to move left and right:

1111 1111&1111 1111^0000 0000==11111 1111

So it looks like the code in the while is executed once.

It's not clear why it doesn't work...

After correcting the following code, the function can be realized.

if(P1==0x7e)
{
comp1=0xfe;
comp2=0x80;
}

or:

if(comp2==0x01)
{
comp1=0xfe;
comp2=0x80;
}

-------------------------------------------------- ----------*/[page]

*********************************************

/*-----------------------------------------------
Function: running light (one-way single flash)

----------------------------------------*/

#include
#define uint unsigned int
void delay(uint);
main()
{
//uint fre=0x03;
//uint comp1=0xfe,comp2=0x80;
P1=0xfe;
while(1)
{
/ *------------------------------------------------ ------------------

Module 1: Cyclic unidirectional flashing, only one light is on and off
Execute 3 times, then switch to the next flashing

-------------------------------------------------- ------------------*/
while(1)
{
delay(30000);
P1<<=1;
P1|=0x01;
if(P1=0x7f)
{
delay( 30000);
P1=0xfe;

}
}

}
}
void delay(uint cnt){while(cnt--);}

/*-----------------------------------------

The program running results jump to the left and right ends. The reason is:

The equal sign in if(P1=0x7f); has also become an assignment sign, so it should be corrected to if(P1==0x7f);

Be especially careful not to mistake the equal sign in a judgment statement for an assignment sign.

--------------------------------------------------*/

[page]

***************************************

/*-----------------------------------------------
Function: pattern light (one-way single flash + one-way double flash)

----------------------------------------*/

#include
#define uint unsigned int
void delay(uint);
main()
{
uint fre=0x04;
uint comp1=0xfe,comp2=0x80;
while(1)
{
/*------ -------------------------------------------------- ----------

Module 1: Cyclic unidirectional flashing, only one light is on and off
Execute 3 times, then switch to the next flashing

-------------------------------------------------- ------------------*/
P1=0xfe;
while(1!=fre--)
{
delay(30000);
P1<<=1;
P1|=0x01;
if(P1==0x7f)
{
delay(30000);
P1=0xfe;

}
}
/*------------------------------------------------- --------------------------

Module 2: Cyclic unidirectional flashing, only two lights are on or off
Execute 3 times and switch to the next flashing

-------------------------------------------------- ------------------*/
P1=0xfc;
while(3!=fre++)
{
delay(30000);
P1<<=2;
P1|=0x03;
if( P1==0x3f)
{
delay(30000);
P1=0xfc;
}
}
}
}
void delay(uint cnt)
{
while(cnt--);
}

/*------------------------------------------------ ----

There are no problems with the two modules, but putting them together does not produce the desired results.

The first cycle was not completed, and the number of cycles of both cycles did not meet the requirements.

The error is that the loop control of module 1 and module 2 is only one time on and off, corrected as:

#include
#define uint unsigned int
void delay(uint);
main()
{
uint fre=0x04;
uint fre1,fre2;
uint comp1=0xfe,comp2=0x80;
while(1)
{
/*-- -------------------------------------------------- ----------------

Module 1: Cyclic unidirectional flashing, only one light is on and off
Execute 3 times, then switch to the next flashing

-------------------------------------------------- ------------------------*/
P1=0xfe;
while(1!=fre--)
{
fre1=0x08;
while(1!=fre1 --)
{
delay(3000000);
P1<<=1;
P1|=0x01;
if(P1==0x7f)
{
delay(3000000);
P1=0xfe;
}
}
}
/*-------- -------------------------------------------------- ----------

Module 2: Cyclic unidirectional flashing, only two lights are on or off
Execute 3 times and switch to the next flashing

-------------------------------------------------- ------------------*/
P1=0xfc;
while(3!=fre++)
{
fre2=0x04;
while(1!=fre2--)
{
delay(3000000) ;
P1<<=2;
P1|=0x03;
if(P1==0x3f)
{
delay(3000000);
P1=0xfc;
}
}
}
}
}
void delay(uint cnt)
{
while(cnt--);
}

Pay attention to the fre++ and fre-- in the control statements; and the initialization of fre1 and fre2 must be correct.

Due to the presence of the if() statement, the values ​​of fre1 and fre2 are one less than expected.

-------------------------------------------------- --------*/

Reference address:Various patterns of running lights c51 program based on 51 single chip microcomputer

Previous article:MCU C language tutorial: Building your first KeilC51 project
Next article:LED Color Light Controller Based on Single Chip Microcomputer

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号