Last time we lit up a running light LED1. Actually, lighting up just one running light is a bit monotonous.
Let's try something different this time.
1. Lighting up multiple running lights
a Open keil and close the original c file
Delete the one on the left, because I don't want to recreate the project.
Create a c file according to yesterday's method and add it to the project (you will do the same in the future, so please have your own naming rules, otherwise you will not be able to tell them apart. I named it LED1.1 here)
b Write the code. (If you want to light up the 1357 running lights)
#include
sbit LED1 = P1^0;
sbit LED3 = P1^2;
sbit LED5 = P1^4;
sbit LED7 = P1^6;
void main()
{
LED1 = 0;
LED3 = 0;
LED5 = 0;
LED7 = 0;
}
The above is the code,
c. As last time, connect USB and burn the program (remember to cold boot)
Here is an explanation. Since it is in the same project, the name of the hex file remains unchanged.
Now you can see the 1 3 5 7 running lights lit up.
The pixel of the mobile phone is not very good, but you should be able to see it clearly on the board.
2 Turning the running lights on and off
It's a bit boring to keep the running lights on all the time, let's make them move
We already know that LED1 = 0; set it to low level to light up,
LED1 = 1; set to high level to turn off
So how do you make a running lamp light up for a period of time and then turn it off for a period of time?
This can be done with a delay function. In the delay function, do nothing.
Create a new c file, pay attention to your naming, it is best not to use Chinese characters, and form your own naming rules
a Write the code
Explanation:
#define uint unsingned int is a macro definition, which we have learned in C language.
// is a single line comment
/* is a multi-line comment */
for(i=1000; i>0; i--) ;
for(j=110; j>0; j--);
1 Why use two for loops?
Because the delay time of a for loop is not enough for people to notice, two for loops are nested. This way, it is obvious to the human eye.
2 Why is there a semicolon directly after the second for loop?
A direct semicolon means that no code will be executed and it is simply used for delay.
b Burning program
c Observe the running lights
Observe the running lights carefully. The first running light will light up for a certain period of time, then turn off for a certain period of time, and then light up again in this cycle.
Because there is a while(1) loop, where is 1, the statements in the while loop will be executed continuously.
This is also a very important point. Many MCU programs have a while (1) loop.
This is determined by each microcontroller. If there is no loop in the program of some microcontrollers, it will execute from the beginning to the end, and then return to the beginning to continue executing. Repeat. Some microcontrollers will stop after executing it once more. Some may enter a disordered running state after execution, and you just don’t know where the program has run! Therefore, when writing a microcontroller program, it is best to add a while loop. Even if your program only runs once, you should add while(1); at the end to make it always point to this statement without any misoperation.
Also, this is an imprecise delay, because for some reasons it is not possible to achieve a very precise delay here.
3 The running lights light up back and forth
We have lit up the flowing lights, but how do we light up these flowing lights one by one?
a Some students may think of this method
In fact, it is also possible to write it like this, and you will see the running lights light up from 1 to 4 in sequence
Here P1 = 0xff is written. This means that when all 4 running lights are lit, all 8 bits of P1 port are set to 1.
That is, all of them are off. In this way, you can see the running lights light up one by one, all go out, and then light up one by one.
In fact, there is another way.
b Function usage of header file intrinsics
intrins is a header file that contains many functions. Here are two of them.
_crol_ Characters rotate left
_cror_ Characters rotate right
Take _cror_ as an example
We all know that the P1 port controls the water lamp. P1 has eight
By default, P1 = 0xff // 1111 1111
If you write the following code
First, to use the _cror_ function, you first need the header file intrinsics
The statement is #include
Here a temp=0x7f // 0111 1111 is defined
P1 = temp, so the eighth running light is on at first.
The for loop means repeating this operation 8 times
temp = _cror_(temp,1);
The above statement means that port P1 is circularly shifted right by one position.
So the seventh running light is on, and the others are off.
There is another point here. A delay function is defined. Delay is a function with no return value.
There are two definition methods, which are consistent with those in C language.
Then you will see that the eighth running lamp lights up first, and then lights up one by one, while the others are indeed extinguished.
Well, the usage of _crol_ is the same. In addition to lighting up one by one, you can also light up one at a time.
temp = _cror_(temp,2); just change 1 to 2
Previous article:51 MCU Learning Part 6 - 1.4 Display of Digital Tube
Next article:51 MCU Learning Part 6 - 1.2 The first C51 program (lighting up the running light)
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- 【Repost】Design of the amplifier circuit of pyroelectric infrared sensor
- Design of Chinese Input Method for Embedded System
- EMU100A MCU Emulator User Manual Version 0.2
- FAQ_How to solve the problem of drastic jumps in the ADC sampling of BlueNRG-12
- Ask a question about ZigBee point-to-point communication
- Matrix keyboard program problem
- [GD32L233C-START Review] (4) LPTIMER wake-up trigger ADC in Sleep low power mode
- How to improve this PCB?
- Review of China's RFID Industry in 2005
- New progress in process research and development of mainstream packaging manufacturers