A preliminary understanding of 51 single-chip microcomputer-2.4 single-chip C language modular programming

Publisher:cangartLatest update time:2016-03-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
2.2 MCU C language modular programming

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.

Reference address:A preliminary understanding of 51 single-chip microcomputer-2.4 single-chip C language modular programming

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号