Application of the Commonwealth of the microcontroller

Application of the Commonwealth of the microcontroller
Often take place at the microcontroller often meet operational separation of high and low bytes, such as timed interrupt reset operation
(65535-200) / 256, (65535-200) 256% of such operation, and a divider consumes four machine cycles, we need to take some complicated column operation, if the number of times in a short time is required such operational procedures will undoubtedly bring a huge burden. In fact, when these operations we need only separate the high and low bytes of data only, so we can easily use the Commonwealth to reduce this part of the overhead.
Code:
union chufa {
int n; // n to be stored in separate data nibbles
char a [2]; // same Keil C in a plastic two bytes, char one byte, so the array with n representing a number of bytes
}test;
test.n = 65535-200; // End for all ok after the sentence, the following data is taken out of the high and low bytes of test data by accessing the array a
TH1 = test.a [0]; //test.a[0] is stored in the upper-bit data, which is due to the characteristics of keil, if other compiler also requested self-test
TL1 = test.a [1]; //test.a[1] stored in the lower order data test.n
! That's good, with only a subtraction instruction reached division operation remainder is taken, particularly useful during high frequency timing.

 

Guess you like

Origin www.cnblogs.com/linux-bfbdxj520/p/11609511.html