The crystal oscillator frequency used by C51 is 11.0592MHz. An LED light is connected to P0.0 of C51. Now it is required to design a program to make the LED light flash at an interval of 1s.
We not only design the program, but also analyze this microcontroller project.
By the way, let’s take a look at the difficulty of this question~~~~~~~~~~~
By the way, this lesson will use the microcontroller timer》》See my previous article for a detailed introduction to the timer
Design a circuit diagram. The microcontroller uses 80C52RC, the crystal oscillator is 11.0592MHz, and an LED is connected to P0.0
Let me explain here, the IO of C51 only supports weak pull-up, which means that the IO of the microcontroller can only provide a very weak current when it is pulled up. This current may not be enough to light an LED light (or the LED light). The brightness is very low), in order to let the LED light up normally, we use the pull-up method. Here is a brief introduction to how to use it.
When P0.0 is at low level, the LED light is on and lights up normally. When P0.0 is at high level, the LED is not on and goes out.
Considering the resources at hand, I used a ready-made development board to complete this program design. This development board uses a pull-up LED, and LED1 is connected to P1.0. The actual circuit diagram is modified as follows:
Now let’s analyze the program
Block diagram
First of all, we need to understand that the LED we use is in pull-up mode. When P1.0 = 1, the LED is turned off, and when P1.0 = 0, the LED is turned on.
Since we want to control LEDs, here’s how to do it
Three special instructions
sfr P0 = 0x80;
This instruction is stored in reg52.h, and the address of the IO group register has been defined. We can use keywords like P0, P1, and P2 directly. It is convenient for us to operate the IO registers. It is worth mentioning that the IO registers are bit addressable.
sbit LED = P1^0
The sbit instruction defines a name for a port on the IO. For example, define a name called LED for P1.0 (written as P1^0). Use the following method the next time you operate this IO.
sbit LED = P1^0;//This is the previous definition
LED= 1; //Write high level to P1.0IO
LED= 0; //Write low level to P1.0IO
In addition, you can also directly operate the entire IO group, the method
So, the next step is the procedure
【1】Software delay implementation
The so-called software delay is to achieve the delay effect by executing empty code. The delay achieved by this delay has a characteristic that the time is not accurate, because the time for each operation in the C language is not fixed in the microcontroller.
The procedure is as follows
#include
sbit LED = P1^0;
void delay(unsigned int xms) //The actual value of parameter xms needs to be provided when calling.
{
unsigned int i,j; //Define unsigned integer variables i, j
for(i=0; i< xms; i++) //Without ";" at the end, the next statement is the loop body. Parameter xms delay x milliseconds.
for(j=0; j< 110; j++); //With ";" behind it, it means that the loop body of this for statement is an empty statement.
}//This is worth learning, the code here is delay code
void main()
{
LED = 1; //Turn off the LED light first
while(1)
{
LED = !LED; //LED status inversion
/*
Here is a description of the inversion
Use "!" to negate it.
Before inversion, LED = 1, after inversion, LED = 0
*/
delay(1000);//Software delay 1000ms = 1s
}
}
This is the effect, so what, it’s okay, but...
The question requires us to delay by 1s. The precise mention of 1s must mean that we are expected to use a timer to complete this project.
I think that if we use software delay, the teacher will give a very low score, and it will not reflect our ability.
Next we use a timer to complete this question
About the usage of timer
#include
void main()
{//Note that setting the initial value of the timer must be set in the main function
TMOD = 0x01;
/*Set the timer mode register TMOD to 0000 0001
GATE = 0
C/T = 0
M1 =0
M0=1
Standard 16-bit timer
*/
//Set the initial value of the timer first
TL0 = 0x3A;//Set the low bit of the timer initial value
TH0 = 0x8E;//Set the high bit of the timer initial value
TR0 = 0; //Remember to set the timer to start
while(1)
{
if (TF0 == 1)
{//Here you can put down the code to be executed after your timer is full.
//After overflow, you need to set the initial value for your timer again.
TL0 = 0x3A;//Set the low bit of the timer initial value
TH0 = 0x8E;//Set the high bit of the timer initial value
TF0 = 0;//Set the initial value and then reset the timer
}
}
}
We need to set up a standard 16-bit timer. For the setting method and process, please refer to the article I wrote. The above is the setting process for the 16-bit timer.
The following is the program block diagram
Below is the code
#include
sbit LED = P1^0;
unsigned char counter = 0;
void main()
{//Note that setting the initial value of the timer must be set in the main function
TMOD = 0x01;
/*Set the timer mode register TMOD to 0000 0001
GATE = 0
C/T = 0
M1 =0
M0=1
Standard 16-bit timer
*/
//Set the initial value of the timer first
TL0 = 0x4B; //Set the low bit of the timer initial value
TH0 = 0xF5;//Set the high bit of the timer initial value
TR0 = 1; //Remember to set the timer to start
while(1)
{
if (TF0 == 1)
{
counter++;
if(counter >200)
{
counter = 0;
LED = !LED;
}
TL0 = 0x4B; //Set the low bit of the timer initial value
TH0 = 0xF5;//Set the high bit of the timer initial value
TF0 = 0;//Set the initial value and then reset the timer
}
}
}
picture
Previous article:51 microcontroller minimum system production steps
Next article:MCS-51 pin function description
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How to view the complex technical documentation of TI DSP
- Please help analyze this circuit.
- kicad-eda_6-layer-pcb_A64-OlinuXino_Rev_D
- Battery Test Equipment --- Signal Chain
- iTOP-4418 development board TF card burning-boot uboot
- Introduce the factors to be considered in selecting DSP chips with examples
- Tektronix Industrial Application Standard and High-Speed Interface Test Solutions
- Ora Good Cat “chip replacement incident”: How big is the gap between the two chips?
- 【NUCLEO-L552ZE Review】+ Bluetooth communication experiment (1)
- Explanation of the stack pointer register SP of msp430