Directly using binary number method in Keil C51

Publisher:知识阁楼Latest update time:2011-02-14 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In Keil C51, numbers cannot be assigned directly in binary form, although it is possible in 8051 assembly. Although binary numbers are long to write and prone to errors, because they are written one bit at a time, programmers can clearly see the status of each bit, which is more intuitive. Therefore, many people miss 8051 assembly and want to use binary in C51. If I am not mistaken, the person who searched this blog is probably looking for this solution. The following two methods are both good choices.

Method 1:

Create a header file to list all binary number macro definitions. Use the macro definitions directly when needed. The header file definition is: binary (right click >> "Save Target As" >> Download).

Method 2:

Make a macro definition with parameters to convert the input binary-like number into the corresponding hexadecimal number. The entire definition and usage example are shown below. You can use it directly. If you want to study the principle but don’t understand the macro definition with parameters, please refer to the chapter on preprocessing commands in Tan Haoqiang’s "C Programming" (the third edition is on P204-P211).

#define LongToBin(n)

(

((n >> 21) & 0x80) |

((n >> 18) & 0x40) |

((n >> 15) & 0x20) |

((n >> 12) & 0x10) |

((n >> 9) & 0x08) |

((n >> 6) & 0x04) |

((n >> 3) & 0x02) |

((n ) & 0x01)

)

#define Bin(n) LongToBin(0x##n##l)

void main(void)

{

unisigned char c;

c = Bin(10101001); //equivalent to c = 0xA9

}

Backslash is used in the above preprocessing instructions. Here is an explanation: each prepared statement always starts with the "#" character and cannot exceed one line. Once a newline is encountered, the statement is considered to have ended. The only way to extend a prepared statement to multiple lines is to add a backslash ('') before the newline.

Reference address:Directly using binary number method in Keil C51

Previous article:Design of Taxi Meter System Based on AT89S51 Single Chip Microcomputer
Next article:Design of MP3 Solution Based on Single Chip Microcomputer AT89C51SND1C

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号