c语言嵌入式系统编程技巧
![Tips for Embedded Development in C](https://i-blog.csdnimg.cn/blog_migrate/8a92f0ef18ccaebe1e79aef476c7e227.png)
Let’s understand what’s an Embedded Development?
让我们了解什么是嵌入式开发?
Development of Softwares, Device Drivers for Electronics Devices to control the Hardware/Machine is known as Embedded Development.
用于控制硬件/机器的电子设备的软件,设备驱动程序的开发被称为嵌入式开发 。
C programming language is the most popular programming language that is used to develop embedded systems, the version of the C for Embedded Development is known as Embedded C programming.
C编程语言是用于开发嵌入式系统的最流行的编程语言,用于嵌入式开发的C版本称为嵌入式 C编程。
Quick links:
快速链接:
Initialize arrays (integers, floats, characters etc) with default values quickly
1) Assignment of Binary, Octal and Hexadecimal Numbers
1)二进制,八进制和十六进制数的分配
In C programming language we can assign binary, octal and hexadecimal values (Generally, we assign Decimal values), this way of initialization/assignment is helpful to the Embedded programmers.
在C编程语言中,我们可以分配二进制,八进制和十六进制值(通常,我们分配十进制值),这种初始化/分配方式对嵌入式程序员很有帮助。
An embedded programmer writes code which directly interact with the system/hardware/machine i.e. we can say he writes code to instruct the machine’s component.
嵌入式程序员编写直接与系统/硬件/机器交互的代码,即可以说他编写代码来指示机器的组件。
Examples of embedded codes are: writing drivers, memory management related codes, designing compilers, designing firmware etc.
嵌入式代码的示例包括:编写驱动程序,与内存管理相关的代码,设计编译器,设计固件等。
While writing a low level code, we need to use binary and hexadecimal values because machine level instruction are written in the form of binary, hexadecimal.
在编写低级代码时,我们需要使用二进制和十六进制值,因为机器级指令以二进制,十六进制的形式编写。
How to assign Binary, Octal and hexadecimal values?
如何分配二进制,八进制和十六进制值?
A decimal value can be used without any prefix character, but other types need prefix characters, like...
可以使用不带任何前缀字符的十进制值,但是其他类型则需要前缀字符,例如...
Binary value: A binary value can be assigned/representedby using "0b" (Zero, lowercase character ‘b’). Example: 0b00001010.
二进制值:可以使用“ 0b” (零,小写字符“ b”)分配/表示二进制值。 例如: 0b00001010 。
Octal value: An octal value can be assigned/representedby using "0" (zero). Example: 0753.
八进制值:可以使用“ 0” (零)来分配/表示一个八进制值。 示例: 0753 。
Hexadecimal value: A hexadecimal value can be assigned/represented by using "0x" or "0x" (zero, lowercase or uppercase character ‘x’). Example: 0x123ABC, 0x62726, 0Xaab345.
十六进制值:可以使用“ 0x”或“ 0x” (零,小写或大写字符“ x”)分配/表示一个十六进制值。 例如:0x123ABC,0x62726,0Xaab345。
Example:
例:
#include<stdio.h>
int main(void)
{
int value;
//assigning value in Decimal format
value = 3624;
//printing value in decimal, octal and Hexadecimal format
printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
//assigning value in binary format
value = 0b111000101000;
//printing value in Decimal, octal and Hexadecimal format
printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
//assigning value in Octal format
value = 07050;
//pringing value in Decimal, Octal and Hexadecimal format
printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
//assigning value in Hexadecimal format
value = 0xe28;
//printing value in Decimal, Octal and Hexadecimal format
printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
return 0;
}
Output
输出量
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
2) Printing the value of a variable in different format
2)以不同格式打印变量的值
Following format specifiers are used to print values in specified format, like...
以下格式说明符用于以指定格式打印值,例如...
Binary - There is no defined format specifier to print the value in Binary format.
二进制 -没有定义的格式说明符以二进制格式打印值。
Octal - "%o" is used to print the value in Octal format.
八进制 - “%o”用于以八进制格式打印值。
Decimal - "%d" is used to print the value in decimal format.
十进制 - “%d”用于以十进制格式打印值。
Hexadecimal - "%0x" or "%0x" is used to print the values in Hexadecimal format.
十六进制 - “%0x”或“%0x”用于以十六进制格式打印值。
3) signed and unsigned data types
3)有符号和无符号数据类型
A signed data stores negative and positive both type of values, while unsigned type can store only positive values.
有符号数据可存储负值和正值两种类型的值,而无符号类型只能存储正值。
How to calculate maximum value of a unsigned type?
如何计算无符号类型的最大值?
If a signed data type can store value from - MIN to MAX, then a unsigned data type can store 0 to (MAX*2)+1.
如果有符号数据类型可以存储从-MIN到MAX的值, 则无 符号数据类型可以存储0到(MAX * 2)+1 。
Consider an example of char data type: A singed char/char value from: – 128 to 127, while an unsigned char can store values from 0 to 255, which is 0 to (MAX*2)+1.
考虑一下char数据类型的示例: 单个char / char值从: – 128到127 , 而无符号char可以存储从0到255的值,即从0到(MAX * 2)+1 。
signed declaration:
签署的声明:
signed char var1;
char var2;
Here, var1 and var2 both are signed character type variables. Note that, variable declaration without using signed or unsigned is considered as signed type.
此处, var1和var2都是带符号的字符类型变量。 注意,不使用带符号或无符号的变量声明被视为带符号类型。
Why it is important for the Embedded Development?
为什么对嵌入式开发很重要?
While working on the boards/machines (with the device drivers) most of the values are 1 byte, 2 bytes or 4 bytes positive integers. So what’s the need to keep space for negative values? Most of the values in the embedded system take 1 bytes i.e. 8 bits.
在主板/机器上(带有设备驱动程序)工作时,大多数值是1字节,2字节或4字节的正整数。 那么,需要为负值保留空间是什么呢? 嵌入式系统中的大多数值占用1个字节,即8位。
So, in that case, you can use unsigned char, it will store value from 0 to 255 (in hexadecimal, 0x00 to 0xFF).
因此,在这种情况下,您可以使用unsigned char ,它将存储从0到255的值(十六进制,从0x00到0xFF )。
A new programmer declares an integer variable to store the integer type of value without considering the range of the value to be stored.
新的程序员声明一个整数变量来存储值的整数类型,而不考虑要存储的值的范围。
Remember that an integer variable is not a solution for integer type of values. Analyze the value range and declare variable according to them. There are many other data types like, char, unsigned char, short and unsigned short
请记住,整数变量不是整数类型值的解决方案。 分析值范围并根据它们声明变量。 还有许多其他数据类型,例如char,unsigned char,short和unsigned short
Data type (with signed and unsigned) | Required memory | Value rang |
---|---|---|
char | 1 byte (8 bits) | -128 to 127 |
unsigned char | 1 byte (8 bits) | 0 to 255 |
short | 2 bytes (16 bits) | -32, 768 to 32767 |
unsigned short | 2 bytes (16 bits) | 0 to 65,535 |
数据类型(带符号和无符号) | 所需的内存 | 价值范围 |
---|---|---|
烧焦 | 1个字节(8位) | -128至127 |
无符号的字符 | 1个字节(8位) | 0至255 |
短 | 2个字节(16位) | -32,768至32767 |
无符号短 | 2个字节(16位) | 0至65,535 |
4) Use of ‘for(;;)’ or ‘while(1)’ as infinite loop
4)使用'for(;;)'或'while(1)'作为无限循环
In Embedded system, we need a main loop which must be executed infinite times. The old method is to run an infinite loop is:
在嵌入式系统中,我们需要一个必须无限执行的主循环。 运行无限循环的旧方法是:
START:
System_init();
...;
...;
Other_statements();
...;
...;
goto START;
In C, C++ and even other programming languages, we should not use goto statement to reach at a particular statement, due to its behavior (more jumps from one statement to another may confuse the compiler or it will take more time to complete the task).
在C,C ++甚至其他编程语言中,由于其行为,我们不应该使用goto语句来到达特定的语句(从一个语句到另一个语句的更多跳转可能会使编译器感到困惑,否则将花费更多时间来完成任务) 。
There is a standard way to run infinite loop is: to use loop statements with TRUE (non-zero number) condition.
运行无限循环的标准方法是: 使用条件为TRUE(非零数字)的循环语句 。
Infinite for loop: (leave blank all three parts of loop statement)
无限for循环:(将循环语句的所有三个部分留空)
for(; ;)
{
System_init();
...;
...;
Other_statements();
...;
...;
}
Infinite while loop:
无限while循环:
while(1)
{
System_init();
...;
...;
Other_statements();
...;
...;
}
5) Quickly switch (updating variable with two values like 0 and 1) the value (flags) using Bitwise XOR (^) operator
5)使用按位XOR(^)运算符快速切换(更新变量,如0和1这样的两个值)值(标志)
Whenever we are using a single variable to maintain the flags, like after executing one part of code the value should be 1 and again it should be 0 and so on...
每当我们使用单个变量来维护标志时,例如在执行代码的一部分之后,该值应为1,再次应为0,依此类推...
In that case, we can use XOR (^) operator...
在这种情况下,我们可以使用XOR(^)运算符...
Example:
例:
#include <stdio.h>
int main()
{
unsigned char flag =0;
printf("Step 1: flag value is: %d\n",flag);
//XORing
flag ^= 1;
printf("Step 2: flag value is: %d\n",flag);
//XORing
flag ^= 1;
printf("Step 3: flag value is: %d\n",flag);
//XORing
flag ^= 1;
printf("Step 3: flag value is: %d\n",flag);
//XORing
flag ^= 1;
printf("Step 3: flag value is: %d\n",flag);
return 0;
}
Output
输出量
Step 1: flag value is: 0
Step 2: flag value is: 1
Step 3: flag value is: 0
Step 3: flag value is: 1
Step 3: flag value is: 0
Only single statement using XORing is updating values to 0 and 1, flag ^= 1;
只有使用XORing的单个语句会将值更新为0和1, 标志^ = 1;
6) Setting and Clearing bit of a number
6)设置和清除数字位
The quick way is to set or clear a bit is: [Read more: C program to set/clear (high/low) bits of a number]
设置或清除位的快速方法是:[阅读更多: C程序来设置/清除数字的(高/低)位 ]
//setting a bit
variable |= (1 << bit_index);
//clearing a bit
variable &= ~ (1 << bit_index);
Here, bit_index is the bit number (counting starts with 0)
此处, bit_index是位数(从0开始计数)
Example:
例:
#include <stdio.h>
int main()
{
unsigned char value = 16;
//binary of 16 is: 0001 0000
printf("value is: %d\n",value);
//not setting 0th bit
value |= (1<<0);
//value will be 0001 0001 = 17
printf("value is: %d\n",value);
//now clearing 4th bit
value &= ~(1<<4);
//value will be 0000 0001 = 1
printf("value is: %d\n",value);
return 0;
}
Output
输出量
value is: 16
value is: 17
value is: 1
7) Initialize arrays (integers, floats, characters etc) with default values quickly
7)使用默认值快速初始化数组(整数,浮点数,字符等)
If you are using any type of arrays, you can quickly initialize all elements while declaring them.
如果使用任何类型的数组,则可以在声明它们的同时快速初始化所有元素。
consider the example:
考虑这个例子:
#include <stdio.h>
#define MAX 10
int main()
{
int iarr[MAX] ={0};
float farr[MAX] ={0.0f};
char carr[MAX] ={' '}; //initializing with space
int i; //for loop counter
//printing values
printf("iarr: ");
for(i=0; i<MAX; i++)
printf("%d ",iarr[i]);
printf("\n");
printf("farr: ");
for(i=0; i<MAX; i++)
printf("%.02f ",farr[i]);
printf("\n");
printf("carr: ");
for(i=0; i<MAX; i++)
printf("%c ",carr[i]);
printf("\n");
return 0;
}
Output
输出量
iarr: 0 0 0 0 0 0 0 0 0 0
farr: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
carr:
Since, there may thousands of Tips/hacks for the embedded development in C programming language. If you know other tips & tricks, which may helpful for the embedded developers. Please share in the comment.
由于使用C编程语言进行嵌入式开发 ,可能有成千上万的提示/技巧 。 如果您知道其他技巧 ,可能对嵌入式开发人员有所帮助。 请分享评论。
翻译自: https://www.includehelp.com/c/c-programming-tips-for-embedded-development.aspx
c语言嵌入式系统编程技巧