/*-----------------------------------------------
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.
-------------------------------------------------- --------*/
Previous article:MCU C language tutorial: Building your first KeilC51 project
Next article:LED Color Light Controller Based on Single Chip Microcomputer
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
- [AIoT Smart Smoke Detection System] Product Design Background
- Simulation research on high frequency antenna protection design
- Please give some advice on the working mode of the boost circuit
- i.MX6ULL Embedded Linux Development 6-System Burning to eMMC and the Pitfalls Encountered!
- Gowin GW1N development board logic analyzer speed test
- Which MSP430FR6972 development board is suitable?
- Please recommend some easy-to-understand books or web links about phase detectors and frequency detectors. Thank you.
- PS2 Interface.pdf
- DIY a CNC switching power supply, you need to master these knowledge
- General architecture of the hardware system of the C6000 series DSP