A complete guide to MCU C language knowledge points (Part 3)

Publisher:风暴使者Latest update time:2012-11-13 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Lesson 8, Operators and Expressions (Bitwise Operators)

Friends who have learned assembly know that assembly has a strong ability to process bits, but the microcontroller C language can also perform bitwise operations on the operation objects, so that the microcontroller C language can also have a certain ability to directly operate on the hardware. The role of the bit operator is to operate on the variable bit by bit, but it does not change the value of the variable involved in the operation. If you need to change the value of the variable bit by bit, you must use the corresponding assignment operation. In addition, the bit operator cannot be used to operate on floating-point data. There are 6 bit operators in the microcontroller C language. The general expression of bit operations is as follows:

Variable 1 Bitwise Operator Variable 2 Bitwise operators also have priorities, from high to low: "~" (bitwise inversion) → "《《" (left shift) → "》》" (right shift)

Shift) → "&" (bitwise AND) → "^" (bitwise XOR) → "|" (bitwise OR)

Table 8-1 is the truth table of bitwise logic operators. X represents variable 1 and Y represents variable 2.

Table 8-1 Logical truth table for bitwise inversion, AND, OR, and XOR

Using the experimental board built before, we will do an experiment to verify whether the bit operation really does not change the value of the variables involved, and learn the expression of bit operations. The program is very simple. Use P1 port as the operation variable. P1.0-P1.7 corresponds to the lowest bit to the highest bit of the P1 variable. Through the LED connected to the P1 port, we can intuitively see whether the variable has changed or how it has changed after each bit operation. The program is as follows:

#include 《at89x51.h》

void main(void)

{

unsigned int a;

unsigned int b;

unsigned char temp; //temporary variable

P1 = 0xAA; //Light up D1, D3, D5, D7 The binary value of port P1 is 10101010, and the LED is lit when it is 0

for (a=0;a<1000;a++)

for (b=0;b<1000;b++); //delay

temp = P1 & 0x7; //Simply writing P1|0x7 is meaningless, because no variable is affected and it will not be compiled

//After executing P1|0x7, the result is stored in temp. At this time, it is temp that is changed, but P1 will not be affected.

//At this time, the LEDs do not change, and D1, D3, D5, and D7 are still on.

for (a=0;a<1000;a++)

for (b=0;b<1000;b++); //delay P1 = 0xFF; //turn off LED

for (a=0;a<1000;a++)

for (b=0;b<1000;b++); //delay

P1 = 0xAA; //Light up D1, D3, D5, D7 The binary value of port P1 is 10101010, and the LED is lit when it is 0

for (a=0;a<1000;a++)

for (b=0;b<1000;b++); //delay

P1 = P1 & 0x7; //At this time, only D2 will be off.

//Because before P1=0xAA=10101010

// and 0x7 0x7=00000111

//Store the result in P1 P1=00000010 //When the bit is 0, turn on the LED. For the circuit, see Lesson 3

for (a=0;a<1000;a++)

for (b=0;b<1000;b++); //delay P1 = 0xFF; //turn off LED

while(1);

//You can use the above program to do bitwise OR, left shift, negation, etc.

}

Compound assignment operators

Compound assignment operators are simply assignment operators with other operators added before the "=" operator. The following are compound assignment operators in C:

%= modulo assignment -= logical negation assignment

The general form of the left shift assignment compound operation is:

The meaning of the variable compound assignment operator expression is that the variable and expression are first operated as required by the operator, and then the result of the operation is assigned to the variables involved in the operation.

Variables. In fact, this is a method of simplifying programs in C language. All binary operations can be simplified by using compound assignment operators. For example:

a+=56 is equivalent to a=a+56

y/=x+9 is equivalent to y=y/(x+9) Obviously, using compound assignment operators will reduce the readability of the program, but it can simplify the program code and

It can improve the efficiency of compilation. For those who are just starting to learn C language, it is better to use the program expression method according to their own understanding and habits when programming, and do not blindly pursue the shortness of program code

.

If you have programming experience, you will be familiar with the role of commas. For example, in VB, the comma in "Dim a, b, c" is used to define multiple variables as variables of the same type. The same is true in C, such as "int a, b, c". These examples show that commas are used to separate expressions. But in C language, commas are also a special operator, that is, the comma operator, which can be used to connect two or more expressions to form a comma expression. The general form of a comma expression is:

expression1, expression2, expression3...expressionn

When the program is running, the expression composed of the comma operator calculates the value of each expression from left to right, and the value of the entire expression composed of the comma operator is equal to the value of the rightmost expression, which is the value of "expression n". In practical applications, in most cases, the purpose of using comma expressions is just to get the value of each expression separately, and it is not necessary to get and use the value of the entire comma expression. It should also be noted that not all commas appearing in any position of the program can be considered as comma operators. For example, the commas in the parameters of the function and the definitions of the same type of variables are only used for separation and not as comma operators.

Conditional Operators

As we said above, there is a ternary operator in the MCU C language, which is the "?:" conditional operator, which requires three operands. It can connect three expressions to form a conditional expression. The general form of the conditional expression is as follows:

Logical expression? Expression 1: Expression 2 The function of the conditional operator is to select the value of the expression to be used based on the value of the logical expression.

When the value of the logical expression is true (non-zero), the value of the entire expression is the value of expression1; when the value of the logical expression is false (non-zero), the value of the entire expression is the value of expression1;

is 0), the value of the entire expression is the value of expression 2. It should be noted that the type of the logical expression in the conditional expression can be different from the type of expression 1 and expression 2. The following is an example of a logical expression.

If a=1, b=2, then we need to take the smaller value of ab and put it into the min variable. You might write it like this:

if (a < b)

min = a;

else

min = b; //This paragraph means that when a < b, the value of min is the value of a, otherwise it is the value of b.

Using conditional operators to form conditional expressions becomes simple and clear:

min = (a < b)? a : b Obviously, its result and meaning are the same as the above program, but the code is much less than the previous program, and the compilation efficiency is relatively high. However, it has the same disadvantage as compound assignment expressions, which is that the readability is relatively poor. In actual application, you should use it according to your own habits. For me, I like to use a more readable method and add appropriate annotations, which can help debug and write the program, and also facilitate future modification and reading.

Keywords:MCU Reference address:A complete guide to MCU C language knowledge points (Part 3)

Previous article:A complete guide to MCU C language knowledge points (Part 1)
Next article:A collection of typical MCU essay competition questions (Part 1)

Recommended ReadingLatest update time:2024-11-16 15:50

51 MCU + CPLD makes the system more efficient
1. Introduction With the development of digital electronic technology, a new device is being widely used, that is CPLD (Complex Programmable Logic Device). In layman's terms, it is an IC that can be redeveloped and can incorporate all the above-mentioned discrete devices. Based on this, this article introduces
[Microcontroller]
51 MCU + CPLD makes the system more efficient
Analysis of MCU Clock
Clock cycle        The clock cycle is also called the oscillation period, which is defined as the reciprocal of the clock pulse (the clock cycle is the reciprocal of the crystal oscillator external to the microcontroller, for example, the clock cycle of a 12M crystal oscillator is 1/12us). It is the most basic and sma
[Microcontroller]
Analysis of MCU Clock
Detailed explanation of 51 single chip microcomputer development steps
If you want to engage in high-end embedded development, you must first be able to use ARM and Linux. If you want to be able to use ARM and Linux, you must first be able to use microcontrollers. If you want to learn microcontrollers from scratch, you must start with the 51 microcontroller. If you want to learn M
[Microcontroller]
51 MCU Series Knowledge 9--Timer, Counter (2)
two 1. When we use a single-chip microcomputer to count a pulse, how do we determine the upper limit of the frequency allowed for the pulse? When set to counting mode, the external pulse signal is counted through pin T0 (P3, 4) or T1 (P3-5). When the input pulse signal has a negative jump (or positive jump, the sp
[Microcontroller]
at89s52 microcontroller pin diagram, at89s52 pin description
  This article introduces you to the at89s52 microcontroller pin diagram and pin description, at89S52 features and functions. at89S52, a high-performance 8-bit microcontroller.   AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller. It contains 8k Bytes ISP (In-system programmable) Flash read-only pro
[Microcontroller]
at89s52 microcontroller pin diagram, at89s52 pin description
MCU Beginners' Learning Path - Experience
What I want to say to microcontroller beginners I have been in contact with microcontrollers for more than two years without realizing it. From the initial MCS-51 to the later AVR and MSP430, I still have some understanding of microcontrollers. Of course, I am only a rookie who has just started. I am still a be
[Microcontroller]
Application of MPC555 microcontroller in automotive electronics
With the rapid development of the automobile industry, the requirements for automobile control, communication and networking are becoming more and more complex. The new generation of electronic control units (ECUs) with 32-bit microcontrollers and embedded real-time operating systems as basic technical features have b
[Automotive Electronics]
Design of three-phase combined inverter based on SA4828 and single chip microcomputer
1 Introduction Power supply equipment is a kind of electronic product with large quantity, wide application and strong versatility. Power supply is used in almost all departments such as modern communication, electronic instruments, computers, industrial automation, power engineering, national defense, etc. It is al
[Microcontroller]
Design of three-phase combined inverter based on SA4828 and single chip microcomputer
Latest Analog Electronics Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号