1. Background
In the process of learning MCU, it is very necessary to master some assembly language. As a low-level language, assembly language plays an irreplaceable role in MCU development. For example, each instruction can accurately determine the delay time, which is easy to understand and very suitable for hardware engineers to learn. However, to improve MCU skills, you must master C language programming, because C language has a powerful modular management concept. I think that in the process of learning, many people have learned both assembly language and C language, so a problem arises. If C language and assembly language are combined to achieve better programming effects, this explanation is based on this problem.
2. Operation steps
first step:
Create a new project and file in KEIL and copy the following program and save it as a C file:
/*
Function: Nesting assembly language in C language, which serves as a starting point for discussion
Author: Yang Rongguo of Sanwei Electronics
Date: 2010-5-16
*/
#include
delay()
{
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<120;j++); //12M crystal oscillator delay 1MS
}
main()
{
P2=0x00; //Control 8 LEDs to light up
delay(); //delay 1S
#pragma ASM // assembly nesting starts
MOV P2,#0FFH //End of assembly nesting
#pragma ENDASM // assembly nesting starts
while(1); //C program ends
}
Step 2:
Select the C file in the Project window, such as CX1.C, and right-click, select "Options for ...", click "Generate Assembler SRC File" and "Assemble SRC File" on the right, and change the checkbox from gray to black (double-click);
Step 3:
Add the corresponding library file (e.g. Keil\C51\Lib\C51S.Lib in Small mode) to the project. This file must be the last file of the project (load it into the project like a header file);
Step 4:
Compile and generate HEX file in KEIL, and design the simulation diagram as shown below, and check the effect in simulation.
3. Demonstration Effect
As shown in the figure below, if you can see the 8 LEDs controlled by the P2 port of the microcontroller light up first and then automatically turn off after a delay of about 1S on your own simulation software PROTEUS, it means that you have fully mastered the method of nesting assembly language in C language.
Homework: Implement C language nested in assembly language. Use C language to control the red LED of P2.0 to flash 5 times at a time interval of 0.1S. Use assembly language to control the green LED of P2.1 to flash 3 times at a time interval of 0.1S and then stop.
Previous article:Matrix keyboard of single chip microcomputer
Next article:STC12C2052AD MCU AD conversion C language program
Recommended ReadingLatest update time:2024-11-16 20:53
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
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
- Analysis of the Causes of Operational Amplifier Oscillation and Self-excitation
- (Bonus 14) GD32L233 Review - Driving Segment Code LCD
- China's chip self-sufficiency rate was 30% last year, and the goal is to reach 70% by 2025 (attached with representative companies in 70 sub-sectors of domestic chips)
- 【New Year's Taste Competition】New Year's Eve Reunion Dinner and My Wedding
- Getting Started with the TI AWR1642BOOST-ODS EVM Development Board
- [STM32WB55 Review] A brief analysis of the execution process of the BLE demo program
- Positioning, positioning, positioning: the road to ultra-wideband
- 35-year-old programmer trades Luna and loses tens of millions of assets in three days
- Design of Pulse Oximeter Based on TI MSP430
- BMS solutions for electric bicycles and electric motorcycles under the new national standard for electric vehicles