[Self-study 51 single-chip microcomputer] 4---base conversion, C language variable types and operators

Publisher:BlissfulJoyLatest update time:2021-10-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Conversion between different bases, C language variable types and operators, Keil Debug usage and application and the realization of running lights


1. Binary, decimal, hexadecimal conversion

1.1 Introduction to binary system

Decimal system: Every ten digits has ten values: 0~9.

Binary: Every binary digit has only two values: 0 and 1. It is the most basic theoretical basis for realizing computer systems. Computer (including single-chip microcomputer) chips are based on tens of millions of switch tubes. Each of them can only have two states: on and off, corresponding to the two values ​​of binary 1 and 0. When writing binary data, the prefix 0b is required. The value of each bit can only be 0 or 1. Eight bits of binary are called a byte.


Hexadecimal: 4 binary digits are combined into one digit, represented by 0 to 9 plus A to F (or a to f). Hexadecimal is an abbreviation of binary and is also a common form in our program writing. When writing hexadecimal data, you need to add the prefix 0x. Two hexadecimal digits are called a byte.


The following table shows the correspondence between the three systems:

Base conversion

Explanation of base: Base is just the representation of data, and the size of data will not be different due to different base representations. For example, binary 0b1, decimal 1, and hexadecimal 0x01 are essentially the same data with the same value. When we do C language programming, we only use decimal and hexadecimal.


1.2 Base conversion

Binary to Hexadecimal

There are 16 numbers in hexadecimal, 0-15. The way to represent 15 in binary is 1111. Therefore, it can be inferred that hexadecimal can be represented as 0000~1111 in binary. As the name suggests, every four digits are one digit. For example:

00111101 can be divided like this:

0011|1101 (the highest bit is not enough, so it can be replaced by zero), according to the binary table, 8 4 2 1 (generally, this many examples are enough. If there are decimals, continue to list to the right, such as 0.5 0.25 0.125 0.0625...)

8 4 2 1

0 0 1 1| 1 1 0 1

Left half = 2 + 1 = 3 Right half = 8 + 4 + 1 = 13 = D

As a result, 00111101 can be converted into hexadecimal 3D.


Convert binary to decimal

Starting from the lowest (rightmost) digit, multiply the digit by the weight of the digit. The weight is the digit of the digit in the order of 2 minus the first power. For example, the 2nd digit is 2 to the (2-1)th power, which is 2; the 8th digit is 2 to the (8-1)th power, which is 128. Add up all the values.

2 (1-1) represents 2 raised to the power of 0, which is 1; and so on.

For example, the binary number 1101, converted into decimal is: 12(1-1)+02(2-1)+12(3-1)+12(4-1)=1+0+4+8=13.


Hexadecimal to Binary

Since in the binary representation method, the maximum value of the number represented by each four digits corresponds to 15 in hexadecimal, that is, the maximum value of each digit in hexadecimal, we can derive a simple conversion method, converting each digit in hexadecimal to the corresponding four digits in binary, and we get what we want:

Example: 2AF5 converted to binary:

Bit 0: (5)16 = (0101) 2

Bit 1: (F)16 = (1111) 2

2nd digit: (A) 16 = (1010) 2

Bit 3: (2) 16 = (0010) 2

So: (2AF5)16=(0010|1010|1111|0101)


Convert decimal to binary:

For example, divide by 2 continuously.

13 (decimal)

To convert to binary, all we need to do is

13 / 2 = 6 … 1

6 / 2 = 3…0

3 / 2 = 1…1

1 / 2 = 0… 1

Then write it from bottom to top: ob1101.


Hexadecimal to Decimal

The weight of the 0th digit of a hexadecimal number is 16 to the power of 0, the weight of the 1st digit is 16 to the power of 1, the weight of the 2nd digit is 16 to the power of 2, and so on.

Therefore, at the Nth position (N starts from 0), if it is a number X (X is greater than or equal to 0, and X is less than or equal to 15, that is: F), the size represented is X * 16 to the power of N.

Example: 2AF5 converted to decimal:

Calculate using vertical form:

Bit 0: 5 * 16^0 = 5

1st digit: F * 16^1 = 240

2nd digit: A * 16^2 = 2560

3rd digit: 2 * 16^3 = 8192

The direct calculation is:

5 * 16^0 + F * 16^1 + A * 16^2 + 2 * 16^3 = 10997 (decimal)


Decimal to Hexadecimal

Hexadecimal conversion has 16 numbers in each digit from small to large, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. That is, every 16th digit is incremented by 1. The six letters A, B, C, D, E, F (letters are not case sensitive) are used to represent 10, 11, 12, 13, 14, 15 respectively.

For example: (1) 1 = 1;

(2) 2 = 2;

(3) 30 = 30/16 = 1, leaving 14, which corresponds to E. So 30 = 1E

(4) 500 = 500/16 = 31, which leaves 4, so the unit digit is 4.

The second step is to convert 31 into hexadecimal, 31=31/16=1, the remainder is 15, find out the number corresponding to 15 is F

So 500=1F4

(5) 321 = 321/16 = 20 with a remainder of 1, that is, each digit is 1

Convert 20 to hexadecimal, 20=20/16=1, the remainder is 4

So 321=141

(6) 1024 = 1024/16 = 64, remainder 0

Convert 64 to hexadecimal, 64=64/16=4, the remainder is 0

So 1024=400

(7) 2000 = 2000/16 = 125, remainder 0

Convert 125 to hexadecimal, 125=125/16=7, the remainder is 13, the number corresponding to 13 is D

So 2000=7D0

To convert decimal to hexadecimal, when the number is greater than 16, keep dividing by 16 and write from the lowest digit to the highest digit.


2. C language basics

2.1 C language variable types and their scope in C51

Variable type: The basic data types of C language are divided into character type, integer type, long integer type and floating point type. Each basic type contains two types. Character type, integer type and long integer type can only express integers except for the different ranges of numerical values ​​that can be expressed. To express decimals, floating point type must be used.


The scope of variables in C51: see the table below

(Note: C51 refers to the C language standard in the 51 single-chip microcomputer)


Programming principle: Use smaller variable types when possible instead of larger ones. If a 1-byte char can solve a problem, do not define it as an int. On the one hand, it saves RAM space, and on the other hand, the program takes up less space and the operation speed is faster.


2.2 C language operators

Left shift << right shift >> (Note: all shifts are binary shifts. Left shift, the lowest bit is filled with 0; right shift, the highest bit is filled with 0). For example, a = 0x01 << 2; that is, 0x01 is converted to binary data 0b0000 0001, shifted left by 2, and then two 0s are added to the right, that is (in binary) 0b0000 0100 (in hexadecimal) 0x04

Bitwise inversion symbol ~: After inversion, 1 becomes 0, and 0 becomes 1 (Note: bitwise inversion also applies to binary). For example, a = ~(0x01); the binary of 0x01 is 0b00000001, and after bitwise inversion, it becomes 0b11111110, so the value of a is 0xFE.


3. Debug tutorial of Keil software

3.1 Delayed introduction

In the last section, we know that for(i=0; i<30000; i++); in the program is used to delay the LED light. In addition to this delay method, there are 3 other common delay methods. See the figure below:

for(i=0; i<30000; i++); Use a loop to delay the light. The delay time varies with the change of i. So how to ensure that the delay time is long enough to make the light flash? At this time, you can debug the program to observe the delay time of the light.


3.2 Application of Program Debug

Observe the inexact extension time

Steps: 1. Select Keil menu item Project–>Options forTarget 'Target1'…—>Open the Target tab, find the Xtal(MHz) position inside, this is to fill in the crystal oscillator option for our simulation time, fill in the crystal oscillator used by your own microcontroller (the blogger's STC89C52 is 11.0592MHz)–>Find the Debug tab, select Use Simulator on the left, and then click OK at the bottom. As shown below:

Debug Tutorial

Debug steps

2. Click the menu item Debug–>Start/Stop Debug Session, as shown in the figure:

debug

Window introduction: The Register window on the far left displays the current values ​​of some registers of the microcontroller and system information. Through this window, the sec option can be used to observe the running time of the C language code. The Disassembly window on the upper side is the code that keil converts C language into assembly language.


Introduction to the three buttons: The three buttons are shown in the figure below

The first one is the reset button. After clicking it, the program will run to the starting position; the second one is the full speed run button. After clicking it, the program will run at full speed; the third one is the stop button.


3. Click the Reset button, and then double-click the lines before and after the code where you need to observe the time to set breakpoints, as shown in the following figure:

4. Click the full-speed run button and observe the program running time 1, then click the full-speed run button again and observe the program running time 2. The program running time 2 minus the program running time 1 is the delay time of for(i=0; i<30000; i++);. As shown in the figure below:

By calculating for(i=0; i<30000; i++); the delay time is about 163ms.

(Note: The inaccurate delay time is affected by the Xtal analog microcontroller crystal oscillator and program optimization level)


Observe the value changes of registers and variables

Click Watch Windows–>Watch 1 in the View menu, double-click or press F2, enter the name of the variable or register we want to observe, and its value will be displayed. As shown below:

3.3 Breakpoint Setting

Double-click to observe the lines before and after the code, but breakpoints cannot be set in some places. This is because the Keil software itself has a program optimization function. If you want to set breakpoints in all codes, select the Keil menu item Project–>Options forTarget 'Target1'…—>Open the C51 option and set the level optimization level to 0, as shown in the following figure:

4. Implementation of Flowing Lights

Introduction: In the third section, we know that pin P0.0 controls DB0 through 74HC245, P0.1 controls DB1...P0.7 controls DB7. A byte has eight bits, so a P0 represents all 8 bits from P0.0 to P0.7. We write P0 = 0xFE; converted to binary, it is 0b11111110, which can light up LED2. The following 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F represent lighting up LED2 to LED9 respectively.

[1] [2]
Reference address:[Self-study 51 single-chip microcomputer] 4---base conversion, C language variable types and operators

Previous article:[Self-study 51 single chip microcomputer] 5 --- timer, digital tube, logical operation,
Next article:[Self-study 51 MCU] 3 -- Introduction to basic hardware knowledge and flashing LED lights

Latest Microcontroller 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号