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 bit by bit, 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:Realization of Multifunctional Power Fault Controller Based on 8051 Single Chip Microcomputer
Next article:Design of portable farmland basic information collection terminal based on C8051F single chip microcomputer
Recommended ReadingLatest update time:2024-11-16 16:21
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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
- Introduction to TI C2000 Chopper Module
- TI - MCU - MSP430 User Guide 31 -> BSL
- NMOS tube on-state voltage test problem
- EEWORLD University - How to use independent measurement ADC to design high-precision CT phase meter
- A collection of selected PCB design resources, free points download for a limited time
- MCU model confirmation and decryption
- [Zero-knowledge ESP8266 tutorial] Quick start 25 Blynk control RGB LED
- Need a 10uh inductor, ESR < 0.2Ω, saturation current > 0.8A
- Ti DSP optimization basic strategy
- Bluetooth Protocol Stack-01