Introduction: This section briefly introduces the use of proteus simulation software. The key lies in the codes of the components needed, such as resistors are resistors, capacitors are capacitors, etc. However, you don't need to remember them deliberately. You will remember them naturally after using them many times. When you encounter new components, you can search them online. The example in the above picture is the minimum system of the microcontroller and the LED light,......
This section briefly talks about the use of proteus simulation software. The key lies in the codes of the required components, such as resistor is resistor, capacitor is capacitor, etc., but you don’t need to remember them deliberately. You will remember them naturally after using them many times. If you encounter new components, just search them online.
The example in the figure above is the minimum system of a single-chip microcomputer and an LED light. The list of components in this schematic is shown in the left column. If you want to add a component, click "P" and a dialog box for selecting components will pop up, which includes all the components that come with Proteus. At this time, you need to enter the component code to search, or if you know the category of the component, you can search directly one level at a time.
In this section, we need to make the LED flash. As you can imagine, the flashing of the LED requires the level of P0.0 to change at a certain frequency. The faster the frequency, the faster the LED flashes. Knowing this, the corresponding C code is very easy.
#include
sbit led = P0^0;
void delayxms(unsigned int xms);
void main()
while(1)
led = 0; //LED lights up
delayxms(1000); //delay 1s
led =1; //LED turns off
delayxms(1000); //delay 1s
void delayxms(unsigned int xms) //delay function
unsigned int i,j;
for(i=xms;i>0;i--) //Through the nested for loop, realize the xms delay
for(j=110;j>0;j--);
Compared with the first section, this section of C code has an additional parameter function delayxms. Each time the value of xms is different, the delay time implemented by the function is also different. For example, in this example, to implement a 1s delay, xms is assigned a value of 1000. When writing a microcontroller program, you must develop good programming habits. In actual projects, there will be many functions, and it is common to have multiple header files and source files separated. Therefore, when writing a program, we can understand it this way, that is, the program has two parts, one is the declaration area of variables and functions, which tells the microcontroller what variables and functions I will use in this program; the second part is the function implementation, that is, the specific implementation code of the declared function. In this example, the C code, after including the header file, declares the led and delayxms functions, telling the microcontroller that the operation of the led in the following code is actually the operation of P0.0. The parameter type of the delayxms function is an unsigned integer and has no return value. Why can the delayxms function implement the delay of xms? Here we need to explain the clock cycle of the microcontroller.
Clock cycle
The clock cycle is the smallest unit in the MCU cycle, which is defined as the reciprocal of the clock frequency. For example, in this example, the clock frequency is 11.0592MHz, so the clock cycle is its reciprocal. One machine cycle is equal to 12 clock cycles, which is approximately equal to 1.09us. In one clock cycle, the MCU completes the most basic action, which is equivalent to the beating of a human heart. Obviously, the higher the clock frequency of the MCU, the faster the MCU works. Of course, this is limited to the hardware design of the MCU. Advanced MCUs such as Freescale and STM32 have a frequency multiplication function. The C code in the MCU takes time to execute each time. Through the breakpoint function in the KEIL software simulation, we can calculate the time required for code execution, which is why the delayxms function is written like this. Of course, it can be done more accurately, that is, using the built-in module timer of the MCU, which we will talk about in the third lesson.
In addition, the while loop and for loop in this C code are essentially the same. Both loops continue when certain conditions are met, and exit the loop when the conditions are not met. First, let's talk about the while loop in the main function. The while loop is very intuitive to understand. When the condition is true (non-zero), the loop will be executed. Because the code in the microcontroller is executed sequentially, if there is no while (1) dead loop, the effect achieved is that the LED lights up and turns off once, and it cannot light up a second time because the program has been executed. For the for loop in the delay function, the first part of the for loop is the initial value of the control variable, the second part is the condition that the control variable must meet, and the third part is the operation of the control variable after executing a loop. For example, in this example, first i=xms satisfies the condition i>0, then enter the for loop, and then reduce the i variable by 1. If the condition i>0 is still met, then the loop continues. It's that simple.
Previous article:Design of automobile anti-theft system based on time trigger mode
Next article:About the microcontroller pin read and write operations
Recommended ReadingLatest update time:2024-11-16 16:01
- Popular Resources
- Popular amplifiers
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
- GD32L233C-START Development Board Evaluation 3: GD32L233C Chip Low Power Performance Test (Continued)
- 【AT32F421 Review】+ COMP Comparator Usage
- Communication protocol conversion
- New live broadcast calendar
- LOTO Practice [Dry Goods] 2- Quick Application of Voltage Comparator
- GD32E230C Test 2: I2C and EEPROM
- 【XMC4800 Relax EtherCAT Kit Review】+DAVE integrated development environment construction and demo operation
- Design of data acquisition card based on FPGA.pdf
- Xunwei 4412 development board cross-compiles Qt application code
- Actual development and experience of the first phase