Assembly language uses mnemonic symbols to write programs, which are converted into binary code programs that can be recognized and processed by computers via a compiler.
1. Introduction to Programming Languages
Machine Language is represented in binary code, a set of machine instructions that can be directly recognized and executed by a computer. Its intuitiveness and universality are poor.
Assembly Language uses mnemonic symbols to write programs, which are converted into binary code programs that can be recognized and processed by computers via a compiler. Assembly language is still a machine-oriented language, programming is cumbersome and time-consuming, and it has poor universality.
High-Level Language is written in code that is close to natural language, which is then converted into binary code programs via a compiler. High-level languages are easy to learn and use, with good universality. C is a structured high-level programming language widely used in microcontroller system development.
1. Base and Weight of Number Systems
Base: The number of characters that can be used in each digit of a counting system.
Weight: The value represented by the digit “1” at different positions in the number.
Binary has a base of 2 (0 and 1), and its weight is a power of 2. Binary numbers are indicated with a B.
Decimal has a base of 10 (0-9), and its weight is a power of 10. Decimal numbers are indicated with D (or no indicator).
Hexadecimal has a base of 16 (0-9 and A-F), and its weight is a power of 16. Hexadecimal numbers are indicated with H.
Number base conversion is omitted here; there are many methods available, and everyone can choose one that suits them.
Example 5: Convert the decimal number 500.03125 to hexadecimal.
Solution:
Thus, we get: 500.03125 = 1F4.08H
Conversion Between Hexadecimal and Binary
The integer part is grouped every 4 bits to the left of the decimal point; if the highest group of the integer is less than 4 bits, add 0 to the left to make it 4 bits;
The fractional part is grouped every 4 bits to the right of the decimal point; if the lowest group of the fraction is less than 4 bits, add 0 to the right to make it 4 bits;
Replace each group of 4 binary numbers with their corresponding hexadecimal number to convert to hexadecimal.
When converting from hexadecimal to binary, the process is reversed.
Example 6: Convert the hexadecimal number 9F4.1H to binary.
Solution: Write each hexadecimal digit as a binary number.
Result: 9F4.1H = 100111110100.0001B
2. Encoding
Encoding is the process by which computers convert input information composed of letters, numbers, and symbols into binary code using input devices.
The character encoding currently used internationally is ASCII (American Standard Code for Information Interchange).
ASCII uses one byte to represent one character, employing a 7-bit binary code for encoding characters, with the highest bit generally used as a parity bit, thus totaling 128 characters, including 32 control characters, 10 Arabic numerals, 52 uppercase and lowercase English letters, and 34 special symbols.
3. Variables and Operators
The ASCII codes for the numbers 0-9 are 30H-39H, and for uppercase letters A-Z are 41H-5AH.
Data can be divided into constants and variables. Constants are values and characters that cannot change and can be used directly without declaration and definition, while variables are quantities that can change during program execution and must be defined in type before use.
The format for defining a variable in C51 is as follows:
Data Type [Storage Type] Variable Name List
Where the order of “Data Type” and “Storage Type” can be interchanged.
4. Variable Types
bit, sfr, sfr16, and sbit are new variable types in Keil C51.
(1) bit is used to define bit variables, which can only have values of 0 or 1. Bit variables are located in the internal RAM bit-addressable area of the 8051 microcontroller (20H to 2FH), totaling 16 bytes, and up to 128 bit variables can be defined.
(2) sfr is used to define special function register variables. This variable is stored in the internal special function register storage area for read and write operations on special function registers.
For example: In the 51 header file, there is a definition sfr P0=0x90, which defines the P0 port register in the chip, and the P0 can be used in the program to operate on that port register.
(3) sfr16 is also used to define special function registers, but it operates on special function registers that occupy two bytes.
For example: sfr16 DPTR=0x82 defines the 16-bit Data Pointer Register DPTR, with its low 8-bit byte address as 82H and high 8-bit byte address as 83H, allowing operations on DPTR within the program.
(4) sbit is used to define special function register bit variables, allowing read and write operations on bit-addressable bits of special function registers.
For example, sbit P0_0=P0^0 defines the 0th bit of the special function register P0, and subsequent operations on that bit can use P0_0 instead. The number after the ^ symbol defines the position of the bit in the register, which must be between 0-7.
5. Storage Types
Cx51 Storage Types
1. Internal Storage Area
(1) data stores variables in the directly addressable data storage area of the chip. The DATA area is located in the low 128 bytes of the internal RAM (0X00 to 0X7F).
Using the data storage mode allows the fastest access speed to variables in target code, placing frequently used variables in the DATA area can improve program execution speed.
If i is defined as an unsigned character type with storage type data, it can be declared as: unsigned char data i
(2) bdata is used to define variables in the bit-addressable BDATA area, allowing mixed access of bits and bytes. The uBDATA area is located in the internal RAM byte address of the microcontroller from 0x20 to 0x2F, totaling 16 bytes, each byte being 8 bits, thus 16×8=128 addressable bits. For example, after the following declaration, the bit variable value0 can access the 0th bit of the byte value:
unsigned char bdata value;
bit value0=value^0;
If the original value of value is 0x00 and you want to set the 0th bit of value to 1, you can achieve this either by byte access method value=0x01 or by bit addressing method value0=1.
(3) idata stores variables in the internal indirectly addressable data storage area IDATA. IDATA uses pointers for addressing and access.
The 51 core microcontroller RAM has only 128 bytes, so there is no indirect addressing data storage area, and idata is indistinguishable from data. The 52 core microcontroller RAM has 256 bytes, and when the 128-byte directly addressable data storage area is insufficient, the 128-byte indirectly addressable data storage area can be used, albeit with slower access speed than data.
2. External Storage Area
xdata stores variables in external data storage, using a 16-bit address to access any address within 64KB of external data storage.
pdata stores variables in the first page of external data storage (address 00H to FFH), with a storage space of 256 bytes. Addressing the PDATA area requires loading an 8-bit address, while addressing the XDATA area requires loading a 16-bit address, thus addressing the PDATA area is faster than addressing the XDATA area.
Variables of pdata and xdata storage types require access to external memory, making access speed the slowest, and should minimize access times. These two types are suitable for storing raw data or final results, while intermediate results that require frequent access should be minimized or avoided.
3. Program Storage ROM
code stores variables in program storage, which can only be read but not written, thus suitable for storing constants or array data for lookups, and cannot be used for variables that need to be modified during program execution. If you want to change the variable value, you must modify it in the program and re-burn the program into ROM.

END