How to use binary numbers directly in Keil C51

Publisher:RadiantGlowLatest update time:2012-05-05 Source: 21ic Keywords:Keil 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 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.

Keywords:Keil Reference address:How to use binary numbers directly in Keil C51

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

Error appears in keil: Flash Download failed: - "Cortex-M3" Problem Solving
Sometimes when the compilation method is switched, such as from ARMCC to GCC compiler, it will appear when compiling and downloading. Error: Flash Download failed: - "Cortex-M3" This error. Currently, the way to test and solve it yourself is: delete the files in the same directory "DebugConfig", "Listings", "Objec
[Microcontroller]
How to set non-zero initialization variables under Keil MDK
Some industrial control products may require to keep the data in RAM before reset after the system is reset (not power-on reset) to quickly restore the site or avoid restarting the site equipment due to an instant reset. However, by default, any form of reset in Keil MDK will clear the non-initialized variable data in
[Microcontroller]
C51 program for simulating RS232 serial communication with microcontroller IO port
This program has been used in one of my projects and is very stable. During the writing process, I referred to    the article http://www.51hei.com/mcu/1541.html on the 51hei website. Some subroutines are excerpted here. #include "reg52.h" #include "intrins.h"   #include "math.h"      #include "stdio.h" sbit BT_SND
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
  A gas sensor is a device that can convert information about the type of gas and its concentration into electrical signals. Based on the strength of these electrical signals, information about the presence of the gas to be tested in the environment can be obtained, so that detection, monitoring, and alarm can be perf
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
c51 74ls164--165--595
164 Marquee, #include reg51.h #include intrins.h #define uint unsigned int #define uchar unsigned char sbit dat=P2^6; sbit clk=P2^7; uchar ii; uchar code seg7code ={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0xff};//0-9,-,全灭 void sendbyte(uchar seg) {   uchar num,c;   num=seg7code ;  for(c=0;c 8;c++)  {  dat=num&
[Microcontroller]
c51 74ls164--165--595
What is the difference between sbit and bit in C51 programming
First of all, bit and sbit are both variable types extended by C51. sbit is generally used to define bit variables of special function registers to facilitate operations on a certain bit of the register. For example: sbit TXD=P3^0; This defines TXD as a bit variable, and this sbit has a certain address. Bit is simil
[Microcontroller]
Solve the Chinese garbled problem in keil5
1. When inputting Chinese characters, you will find that the input text becomes a question mark 2. To solve this problem, you can do the following: Find the following interface in the menu bar 3. Select Chinese GB2312 (Simplified), click OK   4. This will display the input Chinese
[Microcontroller]
Solve the Chinese garbled problem in keil5
Porting uC/OS-II on LPC11C14 under Keil MDK
The porting of uCOS-II on ARM is very common, but the porting under KEIL MDK is not very common. Based on the porting examples under ARM and my own recent experience of using KEIL MDK, I will record the porting process of uCOS-II under KEIL MDK. In the process of porting, I also referred to other people's information.
[Microcontroller]
Porting uC/OS-II on LPC11C14 under Keil MDK
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号