Microcontroller C Language Tutorial (VII)

Publisher:740322lwjLatest update time:2015-12-23 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

   In the last two lessons, we talked about constants and variables. Let's first add a statement to redefine the data type. This statement is typedef.

This is a very useful statement, but I don't use it often. I usually use standard keywords when defining variable data types so that others can

It is very convenient to study your program. If you are a DELPHI programming enthusiast or programmer, you may be accustomed to the definition of variables in DELPHI.

For example, the int type is often defined with the keyword Integer. If you want to use this keyword again when using C51, you can write it like this:
    typedef int integer;
    integer a,b;

  When compiling, these two sentences actually define integer as int first. In the following statements, when you encounter integer, you replace it with int.

is equal to int, so a and b are also defined as int. typedef cannot be used to define variables directly, it just gives a name to an existing data type.

The replacement does not create a new data type. The following two sentences are examples of errors:
    typedef int integer;
    integer = 100;

Using typedef can facilitate program porting and simplify long data type definitions. You can also use typedef to define structure types, which will be discussed later.

We will explain this in detail later when we explain the structure type. The syntax of typedef is
    typedef existing data type new data type name

  Operators are symbols that perform a certain operation. Operators can be divided into unary operators and binary operators according to their relationship with the operator in the expression.

The unary operator requires one operand, the binary operator requires two operands, and the trinocular operator requires three operands.

An expression is a formula with a specific meaning, consisting of operations and their objects. C is an expression language.

becomes an expression statement.

Assignment operator:
You are familiar with the symbol "=". In C, its function is to assign values ​​to variables, which is called assignment operator. Its function is self-explanatory.

It is also clear that it is to assign data to variables. For example, x=10; From this we can see that the formula that uses the assignment operator to connect a variable with an expression

is an assignment expression. Adding ";" after the expression forms an assignment statement. The format of the assignment statement using "=" is as follows:
    variable = expression;
Examples are as follows
    a = 0xFF; //Assign the constant hexadecimal number FF to variable a
    b = c = 33; //Assign values ​​to variables b and c at the same time
    d = e; //Assign the value of variable e to variable d
    f = a+b; //Assign the value of variables a+b to variable f

  From the above examples, we can see that the meaning of the assignment statement is to first calculate the value of the expression on the right side of "=", and then assign the obtained value to the left side.

variables. And the expression on the right can be an assignment expression.
  In some friends' letters, there are error codes that confuse the two symbols "==" and "=". When you ask why the compilation fails, it is often because of the error in

In statements such as if (a=x), "=" is mistakenly used as "==". The "==" symbol is used to perform equality operations.

Arithmetic, increment and decrement operators
  are familiar to everyone. In C language, + and / are arithmetic operators. The arithmetic operators in C51 are

Among the following, only the positive and negative operators are unary operators, and the others are binary operators:
   + addition or positive operator
   - subtraction or negative operator
   * multiplication operator
   / division operator
   % remainder operator
The form of an arithmetic expression:
   expression 1 arithmetic operator expression 2

For example: a+b*(10-a), (x+9)/(ya)

  The division operator is different from the general arithmetic operation rules. If two floating-point numbers are divided, the result is a floating-point number, such as the value of 10.0/20.0

is 0.5, and when two integers are divided, the value obtained is an integer, such as 7/3, which is 2. Like other languages, C operators have precedence and associativity.

You can also use parentheses "()" to change the priority. These are almost the same as the mathematics we learned when we were young, so I don't need to explain them in detail.

:(
  There are so many operators! Let's stop for a moment and do an experiment. When learning operators and other knowledge, we

Let's add a serial interface to our experimental board. With the help of computer software, I can directly see the output results of the microcontroller. In the future, I will use some simple practical

This section explains the simple application and programming of the microcontroller and PC serial communication. If you are using a finished experimental board or simulator, you can skip this section.


  Before making the circuit, let's take a look at the MAX232 to be used. We will not discuss it in detail here, but just know that it is a TTL and RS232 level mutual

The conversion chip and basic pin wiring functions are enough. I usually use two low-power transistors and a small amount of circuitry to replace the MAX232, which can save

The effect is also good (if you are interested, you can check the relevant information on the website http://www.cdle.net ). The following figure shows the basic connection of MAX232

line graph.

Microcontroller C Language Tutorial (VII)
Figure 7-1 MAX232

  Based on the circuits in the last two lessons, add MAX232 according to Figure 7-3. Soldering with a soldering iron in this hot day is really hot.

Yes: The P serial port uses a DB9 female connector, so you can use the purchased PC serial port extension cable to connect to the computer, or you can directly connect it to the computer com port

superior.

Microcontroller C Language Tutorial (VII)
Figure 7-2 DB9 connector
Microcontroller C Language Tutorial (VII)
Figure 7-3 Experimental circuit with MAX232

  After that, we will use the "Hello World!" program from the first lesson to say hello to your computer! Burn the program to the chip.

Connect the serial port. Hey, now you need to open your serial port debugging software. If you don't have it, quickly download one online. You will use Windows Super

Mid-range is also OK, but I never use it. I use comdebug from http://emouze.com , it is a good software, I like it because of its function

It works well and also has a "line status" function, which is very useful when I make gadgets. The serial port number and baud rate are adjusted, the serial port is opened, and the microcontroller is powered on.

You can see "Hello World!" appearing continuously in the receiving area. Be sure to open the serial port of the software first, and then power on the microcontroller, otherwise the character

The characters are not aligned and garbled characters are seen.

Microcontroller C Language Tutorial (VII)
Figure 7-4 Debugging results
Keywords:MCU Reference address:Microcontroller C Language Tutorial (VII)

Previous article:Clock chip DS1302 program example
Next article:Microcontroller C Language Tutorial (VI)

Recommended ReadingLatest update time:2024-11-16 07:55

The construction of intelligent industrial sensor nodes requires sensors to share tasks with MCUs
As an important part of the perception layer in industrial control, industrial sensors not only improve industrial processes and provide asset monitoring and protection, but also make the visualization of industrial control clearer. In today's trend of industrial intelligent manufacturing, in order to create intelli
[Embedded]
Written based on AT89C52 microcontroller real-time clock program
#include'reg52.h' //Header file containing microcontroller registers #include'intrins.h' //Header file containing _nop_() sbit RS=P2^0; //LCD read and write selection bit sbit RW=P2^1; //LCD read and write selection bit sbit E=P2^2; //LCD enable terminal sbit BF=P0^7; //busy signal sbit SCLK=P1^0; //1302 clock out
[Microcontroller]
Written based on AT89C52 microcontroller real-time clock program
Audi motor driver (MCU) system block diagram, chip, function disassembly
New energy Motor driver ( MCU ) system block diagram, chip , function Disassembly The E-tron inverter has a voltage range of 150V-460V, a maximum current of 530A for 10 seconds, a maximum continuous current of 260A, a power density of 30kVA/L, and unlike Tesla
[Automotive Electronics]
Audi motor driver (MCU) system block diagram, chip, function disassembly
Based on 51 microcontroller, understand the principle of SG90 steering gear and control it with buttons
Specific function implementation When the switch is pressed, the angle of the servo moves to 0°, 45°, 90°, 135°, 180°, 0° (final stop position) respectively, with a time delay of 1s for each angle of rotation. Device: AT89C52, sg90 servo, three Dupont wires male to female, two buttons Rotation angle display: Tur
[Microcontroller]
Design and implementation of hydrographic cableway testing system based on AT89S8252 microcontroller
Early hydrological tests on small and medium-sized rivers generally used manually operated hydrographic cable test systems, which required long operation times, high labor intensity, and large subjective errors. To this end, we have transformed the original manual testing system and used microcontroller control to rep
[Microcontroller]
Design and implementation of hydrographic cableway testing system based on AT89S8252 microcontroller
[MCU framework][bsp layer][esp32s3][bsp_pwm] Use of PWM
LED PWM Controllers Overview LED controller (LEDC) is mainly used to control LEDs, and can also generate PWM signals for controlling other devices. The controller has 8 channels and can generate independent waveforms to drive devices such as RGB LEDs. The LED PWM controller can automatically change the duty cycle wi
[Microcontroller]
[MCU framework][bsp layer][esp32s3][bsp_pwm] Use of PWM
[STC MCU Learning] Lesson 14: SPI Communication - Real-time Clock DS1302
1.14.1. Background knowledge about RTC 1.14.1.1 What is a Real Time Clock (RTC)? (1) Distinguishing between time points and time periods Time period: timer/counter; water boiling; not unique Time point: RTC; Alarm, calendar; Unique (2) Why does the microcontroller need a time point? Sometimes the microcontroller n
[Microcontroller]
[STC MCU Learning] Lesson 14: SPI Communication - Real-time Clock DS1302
Design of anti-corrosion power supply monitoring node based on 80C51 microcontroller and AD526 operational amplifier
The phenomenon of metal corrosion can be seen everywhere. The direct and indirect losses caused by corrosion to metal materials are huge, even causing catastrophic damage accidents and serious environmental pollution. Research shows that losses caused by corrosion generally account for 3% to 4% of the gross national p
[Microcontroller]
Design of anti-corrosion power supply monitoring node based on 80C51 microcontroller and AD526 operational amplifier
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号