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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Apple Store is going to remove WeChat. If you want to use WeChat, you can’t buy an Apple phone?
- How to measure analog voltage using the ADC of MSP430?
- Analysis and application of key technologies of BLE low-power Bluetooth
- 求助Cortex_M4_IPU1_C0:Error connecting to the target:Device is held in reset.
- Power-saving Mode of ADI AD5933 Impedance Converter
- "New Concept Analog Circuit" - Signal Processing Circuit, Yang Jianguo's new book
- How to use the IP of Video On-Screen Display in zynq
- Review Weekly Report 20220516: Mil Allwinner Automotive-Grade CPU Development Board MYC-YT507, Award-winning Review is here
- What is the state of Industry 4.0? Check out the results of this global survey
- Unlocking the Potential of Full-Duplex DOCSIS 3.1 Architectures with DPD and 75Ω Cable TV Switches