Here is a section to talk about modular programming of C language for microcontrollers. What is modular programming? All the codes we wrote before are in the led.c file. Imagine how to read if there are too many codes. In addition, how can people who work with you understand your code? Simply put, there is no readability and portability. Modular programming is to solve this problem. When a project is large, we will divide the project into sections of tasks, and each person completes different sections of tasks. At the same time, each section is connected. Suppose developer A needs to use a certain functional module of B, then A only needs to pay attention to the function of the module and how to call it, and as for how this function is implemented, it can be ignored. In addition, for a single person who has developed a project, the next project may need the same functional module. Similarly, you only need to know how to call the module, and there is no need to study the process of implementing the function of the code. Another project is too large and errors occur, which is not easy to troubleshoot. However, if you troubleshoot according to the idea of one module at a time, the idea will appear clearer.
Next, let's take a look at how to establish C language modular programming for microcontrollers
(1) Open Keil software, Project->New uVersion Project
Open the led folder, enter led in the file name and save it.
(2) Select the MCU model. I have already explained it before.
(3) A dialog box pops up, select yes
(4) Create a new blank file or press ctrl+N, then save (ctrl+S) and name it src
Then double-click the src folder, enter main.c in the file name and save it.
Enter the following program in main.c:
- #include
- void main(void)
- {
- }
- Then save
(5)点击project->options for Target ‘target 1’…
(6) In target, select crystal oscillator as 11.0592
(7) Then click output, check Create HEX File, and then click Select Folder for Objects…
(8) A dialog box will pop up. Create a new folder and name it output. Double-click the output folder and click OK.
(9) Then click Listing, then click Select Folder for listings
(10) Double-click output in the pop-up dialog box, click OK, and click OK again.
(11) Return to the main interface, right-click the source group, and select add files to group "sourcegroup1"...
(12) In the pop-up dialog box, double-click src, click main.c, click Add, and then click close.
(13) The source code is stored in the src folder. When we click compile, the output-related files will be stored in the output folder.
(14) Now we create four new files, led.c, led.h, delay.c, and delay.h. We divide the previously written program into three modules, main.c, led.c, and delay.c. To create these files, click Create Blank File (ctrl+N), then Save (ctrl+S), and select the save path in the src folder. If you want to create led.c, the file name is led.c, and if you want to create led.h, the file name is led.h
(15) After building these four files, enter the following content in led.c:
- #include
- sbit LED = P1^0;
- void ledON(void)
- {
- LED =0;
- }
- void ledOFF(void)
- {
- LED=1;
- }
Copy code
Then click Save.
Type in led.h
- #ifndef _LED_H_
- #define _LED_H_
- extern void ledON(void);
- extern void ledOFF(void);
- #endif
Copy code
In delay.c, enter,
- unsigned int i;
- void delay(void)
- {
- for(i=0;i<30000;i++);
- }
Copy code
In delay.h, enter:
- #ifndef _DELAY_H_
- #define _DELAY_H_
- extern void delay(void);
- #endif
Copy code
In main.c, enter:
- #include
- #include "delay.h"
- #include "led.h"
- void main(void)
- {
- while(1)
- {
- ledON();
- delay();
- ledOFF();
- delay();
- }
- }
Copy code
(16) Add led.c, delay.c files
(17) Click Compile, check for errors, and complete.
Previous article:51 MCU Registers-3.1 MCU Timer
Next article:Preliminary understanding of 51 single-chip microcomputer-2.3 single-chip microcomputer control DS18B20 temperature sensor module
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
- About Negative Feedback Amplifier Circuit
- GD32E230C development platform is finally built
- Proteus MSP430 MCU simulation example 16 - timer timing 1 second
- Building ESP32-C3 development environment based on window Visual Studio Code: ESP-IDF
- [AT-START-F403A Review] + Unsuccessful W25Q128 Read and Write
- Cadence Allegro beginners must read
- EEWORLD University ---- Introduction to CC2650MODAPluetooth? Low-energy RF module
- After the microcontroller is powered on, it runs a display program and encounters a very strange problem.
- A brief discussion on LTE technology and practical application solutions
- What is the drilling process in the Siliton ceramic substrate PCB?