5021 views|11 replies

40

Posts

0

Resources
The OP
 

Some Misuses and Summary of MCU C Language [Copy link]

 
Only when learning about single-chip microcomputers did I really know what the C language is and what it is used for~ However, the use of C language in embedded systems is only a small part of it. There are many other places for its application. Haha, we will not discuss this here. When we write programs, do we make a lot of mistakes and even if the program is compiled, it still cannot achieve the expected results? Then it is difficult for us to find the mistakes, right? I think the reason why a language can be called a language is that it is a tool, a tool for mutual communication and conveying each other's intentions. As a language, it must have its own grammar. If you want to communicate with each other, you must first learn its grammar well. (For example, expressions, functions, loops, pointers) I call it the grammar of the C language. Although C++ is very powerful, it also has many pitfalls. So I have two purposes for this blog post. One is to summarize some of the misuse and mistakes of C++, and the other is to summarize some basic grammar of C++. The first time: 1. About self-increment and self-decrement (that is, ++i, i++) To add or subtract one to a number, we can: i += 1;j -= 1;The C language also allows the ++ and -- operators. This is actually misleading because ++ and -- can be used as prefixes and suffixes, which may change the value of the operands. Let's take a look:i = 1;printf("i is %d\n",++i); /* prints i is 2 */printf("i is %d\n",i++); /* prints i is 2 */The result of the calculation expression i++ is i, but it will cause i to be incremented: i = 1; printf("i is %d\n",i++); /* prints i is 1/ */printf("i is %d\n",i); /* prints i is 2 */
The firstprintf showsi's original value before the increment, the secondprintf showsi's new value after the change;Of course-- I won't give examples for similar ones~but using ++ and -- multiple times in the same expression is often difficult to understand. Let's look at the following example: i = 1; j = 2; k = ++i + j++; the final values of i, j, k are 2, 3,4and++iis2 j++is2;
In summary: whether it is ++i or i++, after executing this statement, the value ofi is increased by 1, but the value of(++i) is increased by 1, while(i++) #define
2.1.typedef
In addition to directly using standard type names (such as int, char, float, double) and self-declared structures, unions, pointers, and enumeration types, the C language can also use typedef to declare new type names to replace existing type names.
typedef unsigned char u8;
typedef unsigned int u16;
u8 count;
u16 time;
typedef struct
{
u8 month;
u8 day;
u16 year;
}DATE;
DATE brithday;
To summarize, the method of declaring a new type name is:
1.First write the definition body according to the method of defining variables(For exampleunsigned int i)
2.Replace the variable name with the new variable name(For example, replacei withu16)
3.Addtypedef (typedef unsigned int u16)
at the very beginning4.Then define the variable with the new type name
2.2 #define
2.1.1Macro definition without parameters
#define Identifier string
#define PI 3.1415926
Note:
1.Its function is to use the specified identifierPI to replace3.1415926
2.The macro definition is to use the macro to replace the string, that is, to do a simple replacement, without correctness check. If written as
#define PI 3.l4l6926
That is,1 is written as the letterl, but the preprocessor is substituted as usual without any syntax check!!
2.1.2Macro definition with parameters
#define Macro name(Parameter) String
#define S(a,b) a*b
area = S(a,b);
#define MAX(x,y) (x)>(y) ? (x)y)3.The difference between typedef and #defineGenerally speaking, typedef because it can handle pointer types correctlytypedef char *String1;#define String2 char *String1 s1,s2;String2 s3,s4;s1,s2,s3 are defined as char*, but s4 is defined as char type 3. static variables static variables can be roughly divided into three usages 1. Used in local variables, becoming static local variables. Static local variables have two uses,memory function and global lifetime.
2. Used for global variables,The main function is to limit this global variable from being called by other files.
3. Used for members in a class.Indicates that this member belongs to this class but not to any specific object in the class
1. Static local variables Static local variables belong to the static storage mode, which has the following characteristics: (1) The lifetime of a static local variable defined in a function is the entire source program, but its scope is the same as that of an automatic variable. The variable can only be used in the function that defines it. After exiting the function, although the variable continues to exist, it cannot be used. (2) It is allowed to assign initial values to static local variables of a constructed class, such as arrays. If no initial value is assigned, the system automatically assigns a value of 0. (3) If no initial value is assigned to a static local variable of a basic type when it is declared, the system automatically assigns a value of 0. If no initial value is assigned to an automatic variable, its value is indeterminate. According to the characteristics of static local variables, it can be seen that it is a quantity with a lifetime of the entire source program. Although it cannot be used after leaving the function that defines it, it can continue to be used when the function that defines it is called again, and the value left after the previous call is saved. Therefore, when a function is called multiple times and the values of certain variables are required to be retained between calls, static local variables can be considered. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is still better to use local static variables. For example: void fun(){static int a = 1;a++;}When entering this function for the first time, the variable a is initialized to 1! and then incremented by 1. Each time you enter this function, a will not be initialized again, and only the operation of self-increment by 1 will be performed; Before the invention of static, to achieve the same function, only global variables can be used: int a = 1; void fun() { a++; } 2.Adding static before static global variables (external variables) constitutes a static global variable. Global variables are static storage methods, and static global variables are also static storage methods. There is no difference between the two in terms of storage methods. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid in the source file that defines the variable, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file, they can only be used by functions in the source file, so errors in other source files can be avoided. From the above analysis, we can see that changing a local variable to a static variable changes its storage method, that is, changes its lifetime. Changing a global variable to a static variable changes its scope, limiting its scope of use. Therefore, the static specifier plays different roles in different places.
3.staticclass member variables
The static keyword has two meanings,You should judge based on the context
1.Indicates that the variable is a static storage variable, indicating that the variable is stored in the static storage area.
2.
indicates that the variable is an internal link ( This situation means that the variable is not within any {}, just like a global variable , at this time add static), that is to say in other .cpp files , the variable is not visible ( you can't use ).
2. static functions——internal functions and external functions
When a source program consists of multiple source files, Clanguage divides functions into internal functions and external functions according to whether they can be called by functions in other source files.
1 [/font=宋体]Internal functions (also called static functions )
If a function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program, this function is called an internal function. To define an internal function, just add a “static” keyword before the function type, as shown below: static Function type Function name (Function parameter list) {…} The keyword “static” means “static” in Chinese, so internal functions are also called static functions. But here "static" does not refer to the storage method, but means that the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, they don't have to worry about whether the functions they define have the same name as functions in other files, because it doesn't matter if they have the same name. 2 External functions Definition of external functions: When defining a function, if you do not add the keyword "static", or prefix it with the keyword "extern", it means that this function is an external function: extern] Function typeFunction name(Function parameter table)
{……}
When calling an external function, you need to explain it:
[extern] Function typeFunction name(Parameter type table)[, Function name2(Parameter type table2)……];

This post is from MCU

Latest reply

Good post by the host, please leave your name  Details Published on 2018-1-11 17:00
 

40

Posts

0

Resources
From 3
 
More MCU learning: http://www.superedu.com.cn/xuexi/jdsp/?ee321
This post is from MCU
 
 

40

Posts

0

Resources
2
 
For the current market, there are more high-end embedded talents, and the low-end talents are becoming saturated, and the salary will definitely drop, so improving your own knowledge is the key. If you have C basics and introductory knowledge of embedded systems, it is recommended to start with stm32F103. You can also go to address where there is a lot of information, a lot of development boards, and they are also widely used. Of course, C language is used to learn microcontrollers. If you are good at it, C++ is also okay.
This post is from MCU
 
 
 

10

Posts

0

Resources
4
 
The old iron is awesome
This post is from MCU
 
Personal signature119消防平台
 
 

40

Posts

0

Resources
5
 
First of all, you need to learn the basics of C language, which is equivalent to 80% knowledge of microcontrollers, because now all 8/16/32-bit (51 series, MSP430 series, ARM series) use C language.
This post is from MCU
 
 
 

40

Posts

0

Resources
6
 
If you really want to learn, it is recommended to start with stm32F103. There is a lot of information, many development boards, and it is widely used. Of course, the C language is the first language to learn microcontrollers. If you are good at it, you can also play with C++.
This post is from MCU
 
 
 

40

Posts

0

Resources
7
 
As another system besides PC, embedded system has a wide range of applications, including single chip microcomputer, ARM, FPGA, DSP, IC design and microprocessor architecture.
This post is from MCU
 
 
 

525

Posts

235

Resources
8
 
The ones that impressed me the most are: a=1;b=2;c=3; a=b==c;
This post is from MCU
 
 
 

40

Posts

0

Resources
9
 
The main contents of embedded system include Linux system, C language development, database, JAVA part of Android development, etc. After graduation, you can develop application software, kernel development, driver development and other work, and do projects.
This post is from MCU
 
 
 

3

Posts

0

Resources
10
 
Good post by the host, please leave your name
This post is from MCU
 
 
 

40

Posts

0

Resources
11
 
The single-chip microcomputer is now considered a universal electronic device, and the single-chip microcomputer itself is the main body. The embedded system is subordinate in the physical structure relationship, and the embedded system is embedded and installed in the target application system.
This post is from MCU
 
 
 

40

Posts

0

Resources
12
 
A single-chip microcomputer is an integrated circuit chip that uses ultra-large-scale integrated circuit technology to integrate a central processing unit (CPU) with data processing capabilities, random access memory (RAM), read-only memory (ROM), multiple I/O ports and interrupt systems, timers/counters and other functions (may also include display driver circuits, pulse width modulation circuits, analog multiplexers, A/D converters and other circuits) on a silicon chip to form a small and complete microcomputer system. It is widely used in the field of industrial control. Since the 1980s, the 4-bit and 8-bit single-chip microcomputers at that time have developed into the current 32-bit 300M high-speed single-chip microcomputers.
This post is from MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list