2007-12-25 10:54:05
0 Introduction
In the program design of real-time control system, the problem of fractional arithmetic is often involved. The method of binary representation of decimal in computer system has the fixed-point number representation method and floating-point representation method. The decimal range represented by floating-point number notation is large and the precision is high, but the program code is long and the operation speed is slow. The fixed-point number indicates that the decimal range is small, the precision is low, but the program code is short and the operation speed is fast.
The use of C language design program has the advantages of strong program readability, programming convenience, but in accordance with the conventional method of designing programs, real-time than the use of assembly language design program, which is related to the performance of fractional operations more prominent. This limits the application of C language. If the appropriate calculation method is used, programming with C language can get the same real-time as the assembly language programming.
The raw data collected by the forward channel in the real-time control system are mostly fixed-point integers, such as the conversion result of A/D converter of the forward analog channel, the counting result of timer/counter, etc., are fixed-point integers. And the system of the back channel can accept the input amount is an integer, that is, the finite word error caused by quantization is unavoidable, the precision of the control of the decimal place is not accepted by the executing agency. Therefore, although the method of using fixed-point number to represent decimal is low, in most cases, the control precision of real-time control system can still be satisfied.
The internal program memory of MCS-51 MCU is only 4 K, and the operation speed is slow. For the real-time, code length limit requirements of the control system, the use of MCS-51 single-chip microcomputer control, it is not appropriate to use a large number of floating point operations. This paper introduces a multiplication program of 16-bit fixed-point decimals under Keil C51.
1 fixed-point fractional arithmetic algorithm
1.1 The characteristics of the control algorithm.
In the computer real-time control system, the control algorithm can usually be expressed by the difference equation below.
Y[n] is the output of the nth sample period, usually a binary integer, and x [n] is the input of the nth sampling period, usually a binary integer; ai, bi is a real factor. When calculating the difference equation, the coefficients AI and bi are converted to integer or fixed-point decimals, which greatly improves the computation speed and greatly reduces the code length. It is important to realize the calculation of fast control algorithm in embedded controller with limited program memory capacity or operation speed.
1.2 Fixed-point decimals
Decimals can be divided into integers divided into 0 decimal fraction and fractional numbers. Decimal fraction can be directly used with fixed-point decimals, when the use of 16-bit fixed-point decimal, the resolution can reach 2-16, can be obtained enough precision.
1.3 Fixed-point algorithm
Set X as the decimal decimal fraction and M as a 16-bit binary integer. If the program needs to calculate y= (x M), you can first convert the X into a 16-bit binary to make a decimal point.
X = (x 65536) rounding (2)
Since the decimal point of X is before the highest of X, 2 16-bit binary numbers are multiplied by 32-bit binary numbers, the decimal point is between 16 and 16 bits high, and the high 16 bits after multiplication are the integer portion of the computed result, and the lower 16 bits are the fractional parts of the result. That
(x M) rounding = (x M) Take high 16 bits (3)
This processing can greatly improve the operation speed, and greatly reduce the code length.
In assembly language programming, the rounding operation is easy to realize, and the implementation of the rounding operation in C language can use the union, the method is as follows.
Define 2 unions first.
union{
unsigned char a_byte[4];
Long A_long;
}r;
union{
unigned Char b_byte[2];
int b_int;
}p;
The first is a combination of a long integer variable and a 4-byte variable, a long integer variable to hold the result of the calculation, and the second is a union of integer and 2 byte variables for rounding operations. In Keil C51, a long integer occupies 4 bytes, stored in RAM in order from high to Low, r.a_byte[0], r.a_byte[1] storing the integer part of the calculation result, r.a_byte[2], r.a_byte[3] The fractional part of the calculation result.
Use the following procedure to achieve the rounding operation.
P.B_BYTE[0]=R.A_BYTE[0];
P.B_BYTE[1]=R.A_BYTE[1];
This p.b_int is the integer portion of the computed result. The above program is compiled with only 2 data transfer instructions, requiring 4 machine cycle execution time. It has a faster execution speed than using a division or shift operation to achieve a rounding operation.
2 procedures
The program needs to calculate a 0.12345-by-16-bit binary number after rounding, using floating-point numbers when the program is as follows.
Main ()
{
int b;
b=20000;
A=0.12345*b;
}
The running result of this program is a=2527, the program compiles 513 bytes, it takes 602 machine cycles to do floating-point operation.
Main ()
{
int A, B;
union{
Char c[4];
Long D;
}U1;
union{
Char e[2];
int F;
}U2;
b=20000;
u1.d= (long) 8090*b;
U2.E[0]=U1.C[0];
U2.E[1]=U1.C[2];
}
The running result of this program is u2.f=2527, the program compiles after 129 bytes, only 134 machine cycles are required for integer operation.
3 Conclusion
using the method described in this article, the C language design MCS-51 microcontroller control algorithm program can be obtained with the use of assembly language design control algorithm program the same effect. Give full play to the advantages of the
C language design program, the author in the design of a control system, the use of this method has achieved very good results.