The mbed program is written in C + + and adds some custom functions and constants on top of it, so we need to get a simple look at the corresponding language base here.
Constants
Constants are constant in the process of the program, constants often appear directly in the program, such as the number 1768, character ' m '; string "mbed", etc., at this time only they are required to conform to the corresponding type of data representation. corresponding to various data types, there are integer constants, floating-point constants, character constants, and string constants.
Sometimes for the convenience of code writing, we use an identifier to represent a constant, implemented by a macro-defined preprocessing directive, which is the constant definition, in the following format:
#define Identifier constant is a user-named identifier that is the symbolic constant name.
As a symbolic constant name, general capitalization, once defined, in the program where the constant occurrence of the constants can be replaced by the symbolic constant name, the use of symbolic constants of the program before compiling with the actual constant substitution of symbolic constants. Examples of constants defined in Mbed are as follows:
#define Device_analogin 1//Whether ADC acquisition is supported
#define Device_analogout 1//Whether DAC output is supported
variables and their data types
As long as there is no memory limit, mbed can freely define variables in C + + syntax, but the data type of the variable must be one of the following types:
bool : boolean type, Only true and false two values, respectively, represent values 1 and 0, which occupy 1 bytes.
Char : The character type, which is used to represent a character but is stored as a character ASCII value, with a value range of 128 to 127, which takes up 1 bytes.
byte : The byte type, with a value range of 0 to 255, which occupies 1 bytes.
Short : shorter integer with a value range of 32768 to 32767, which occupies 2 bytes.
unsigned short : The unsigned stub, with a value range of 0 to 65535, which occupies 2 bytes.
int : integer with a value range of -2^31 to 2^31-1, which takes up to 4 bytes.
unsigned int : unsigned short integer with a value range of 0 to 2^32-1 and 4 bytes.
Long : length integer with a value range of –2^63 to 2^63-1, which takes up to 8 bytes.
unsigned long : unsigned long integer with a value range of 0 to 2^63 and 8 bytes.
float : floating point, which is used to represent decimals and occupies 4 bytes.
Double : high-precision floating-point type, used to represent decimals and occupy 8 bytes.
Array : array type, as defined below:
int light[6] = {0, +,----100,150};
Enumeration
In practical problems, the values of some variables are confined to a limited range. For example, the LPC1768 UART can only be UART0, UART1, UART2, UART3,I2C can only be i2c0, i2c1, I2C2 and so on. If these quantities are described as integers, the character type or other types are obviously inappropriate. For this reason, the C + + language also provides a "Enumeration"Type. All possible values are enumerated in the definition of the "enum" type, and the variable value that is described as the "enum" type cannot exceed the defined range.
Defining a variable is an enumeration type, you can first define an enumeration type name, and then explain whether the variable is that enum type, or you can directly define the enumeration type variable, which is generally used in mbed, as in the following example:
typedefenum {
SPI_0 = (int)LPC_SSP0_BASE,
SPI_1 = (int)LPC_SSP1_BASE
} SPIName;
typedefenum {
I2C_0 = (int)LPC_I2C0_BASE,
I2C_1 = (int)LPC_I2C1_BASE,
I2C_2 = (int)LPC_I2C2_BASE
} I2CName;
For the use of enumeration types, we need to understand the following points:
An enumeration element is not a variable, but a constant, so an enumeration element is also known as an enumeration constant, because it is a constant, so an enumeration element cannot be assigned a value.
The enumeration elements are constants, they are valued, and the C + + language is compiled at compile time so that their values are 0,1,2,... If you specify the value of an element when defining an enumeration type, you can also change the value of an enumeration element, such as the following definition, which starts with 1, and it is important to note that the value of the enumeration element can be the same.
typedefenum {
PWM_1 = 1,
PWM_2,
PWM_3,
PWM_4,
PWM_5,
PWM_6
} PWMName;
typedefenum {
// LPC Pin Names
P0_0 = LPC_GPIO0_BASE,
P0_1, P0_2, …
// mbed DIP Pin Names
p5 = P0_9,
p6 = P0_8,
p7 = P0_7,
p8 = P0_6,
p9 = P0_0,
p10 = P0_1,
p11 = P0_18,
p12 = P0_17,
…
// Not connected
NC = (int)0xFFFFFFFF
} PinName;
The L enumeration value can be used for judgment. The comparison rule for enumeration values is to compare them by their sequential number in the description, such as PWM5>PWM3.
L An integer cannot be assigned directly to an enumeration variable, and a type conversion must be forced to assign a value. For example: pwm= (enum pwmname) 2; This assignment means that the enumeration element with sequence number 2 is assigned to PWM, which is equivalent to PWM =pwm_2;
In the mbed code, there are a large number of enumeration types and are tightly bound to the constant definition, as in the following code, followed by constant values are enumerated elements:
// Default peripherals
#define MBED_SPI0 p5, p6, p7, p8
#define MBED_SPI1 p11, p12, p13, p14
#define MBED_UART0 p9, p10
#define MBED_UART1 p13, p14
#define MBED_UART2 p28, p27
#define MBED_UARTUSB USBTX, USBRX
operator
The mbed uses C + + operators, including mathematical operations, logical operators, and binary operators in three parts.
mathematical operators: support plus (+), minus (-), multiply (*), except (/), remainder (%) of the five basic operations, it should be noted that the different data types involved in the operation will result in different results, such as the case of the integer type 10/3=3, and the case of floating-point participation 10/3.0 =3.3, that is, the data type of the result of the operation is related to the data type involved in the operation, but also to consider the overflow of the results of the operation, such as the following code, the ideal result is 1000, the actual result is 232, is the result of 1000 overflow:
byte i=10;
i=i*100;
logical operators: support for non-(!), with (&&), or (| |) Three types of logical operators.
Binary operators: supports bitwise-NON (!), supports bitwise-and (&), supports bitwise-OR (|), supports bitwise XOR (^), shifts left (>>), and right-shifts (<<) 6 in binary operators.
Control Structure
The control structure of the mbed using C + + is the following four types:
If...else:Conditional judgment structure, the following code only val=1 when the LED pin height:
if (val == 1) {
led=1;
}
Else{
led =0;
}
For :Loop structure, the following code will place the pin 10 times higher:
for (int i = 0; i < 10; i++) {
led=1;
wait(1);
}
Switch case:Branching structure, the following code will place different pins high according to Val values:
switch (val) {
case 1:
led1=1;
break;
case 2:
led2=1;
break;
default:
}
while/do while:Loop structure, the following code will put the pin height 512 times, the difference between the while/do while is that the loop body is not executed at a time, and does while the loop body executes at least 1 times:
int val = 0;
while (sensorValue < 512) {
led1=1;
wait(1);
val++;
}
do {
led1=1;
wait(1);
val++;
} while (val < 512);
In the loop structure we need to make reasonable use of the break and continue keyword, the front means to jump out of the loop directly and continue to skip this cycle, such as the following code, when the x value is not between 140 to 200, the program will skip the subsequent led1=1;wait (1); Then start the next round: But if you break, the program will jump out of the for loop and execute code led1=1.
for (light = 0; light < 255; light++)
{
if ((x > 140) && (x < 200))
continue;
led1=1;
wait(1);
}
led2=1;
Time wait function
Considering that all applications use time-waiting functions, such as wait in the previous example, we also put the time-wait function on the basis of the program design, mbed altogether provides three time-wait functions, which are implemented as follows:
void wait(float s) {
wait_us(s * 1000000.0f);
}
void wait_ms(int ms) {
wait_us(ms * 1000);
}
void wait_us(int us) {
uint32_t start = us_ticker_read();
while ((us_ticker_read() - start) < (uint32_t)us);
}
It can be seen that the essence of these three functions are wait_us, it should be noted that the wait function input is a floating-point type, the unit is the second, Wait_ms and Wait_us input is the shape, the units are milliseconds and subtle, and here the Us_ticker_read () is also a very common time function, which indicates how long the system has been running since reset.
Now we can use some of the above functions to complete the new HelloWorld program, we create a new project called HelloWorld2, HelloWorld2.cpp code as follows, after the program upload you can see through the serial tool, the effect of its operation, It will periodically output the time the system is running:
#define DELAY_MS 500
DigitalOut led(LED1);
Serial pc(USBTX,USBRX);
int main() {
pc.baud(115200);
while (1)
{
led=1;
wait_ms(DELAY_MS);
pc.printf("hello world,system had passed %d ms.\n",us_ticker_read());
led=0;
wait_ms(DELAY_MS);
}
}
http://blog.tianya.cn/post-7182283-114135201-1.shtml
http://blog.tianya.cn/post-7182283-114135350-1.shtml
http://blog.tianya.cn/post-7182283-114135844-1.shtml
http://blog.sina.com.cn/s/blog_15e2eae990102w9n9.html
http://blog.sina.com.cn/s/blog_15e2eae990102w9nb.html
http://blog.sina.com.cn/s/blog_15e2eae990102w9nd.html
http://blog.tianya.cn/post-7200700-114138258-1.shtml
http://blog.tianya.cn/post-7200700-114138509-1.shtml
http://blog.tianya.cn/post-7200700-114138881-1.shtml
http://blog.tianya.cn/post-7200700-114139103-1.shtml
http://blog.tianya.cn/post-7200700-114139506-1.shtml
http://blog.tianya.cn/post-7200700-114140033-1.shtml
http://blog.tianya.cn/post-7200700-114140618-1.shtml
http://blog.tianya.cn/post-7200700-114140869-1.shtml
http://blog.tianya.cn/post-7200700-114141082-1.shtml
http://blog.tianya.cn/post-7200700-114141292-1.shtml
http://www.cnblogs.com/SA-Jim/p/5367348.html
http://www.cnblogs.com/SA-Jim/p/5367356.html
http://www.cnblogs.com/SA-Jim/p/5367371.html
http://www.cnblogs.com/SA-Jim/p/5367388.html
http://www.cnblogs.com/SA-Jim/p/5367397.html
http://www.cnblogs.com/SA-Jim/p/5367407.html
http://www.cnblogs.com/SA-Jim/p/5367415.html
http://www.cnblogs.com/SA-Jim/p/5367422.html
http://tieba.baidu.com/p/4465149167
http://www.biyinjishi.com/kdoc/89456152/
http://www.biyinjishi.com/kdoc/89456149/
http://www.biyinjishi.com/kdoc/89456154/
http://www.biyinjishi.com/kdoc/89456155/
http://www.biyinjishi.com/kdoc/89456157/
http://www.biyinjishi.com/kdoc/89456146/
http://www.biyinjishi.com/kdoc/89456151/
http://www.biyinjishi.com/kdoc/89456147/
http://www.biyinjishi.com/kdoc/89456148/
http://www.biyinjishi.com/kdoc/89456144/
http://www.biyinjishi.com/kdoc/89456145/
http://www.biyinjishi.com/kdoc/89456150/
http://www.biyinjishi.com/kdoc/89456153/
http://www.biyinjishi.com/kdoc/89456156/
Language basis for mbed programs