Transplantation and Optimization of Embedded C Language Bit Operation

Publisher:annye_chengLatest update time:2012-03-29 Source: 单片机与嵌入式系统应用 Keywords:Embedded Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction
The application of single-chip microcomputers is becoming more and more extensive, and there are more and more types of them. Since the embedded C language has strong readability and good portability, it greatly reduces the labor intensity of software engineers compared with assembly language, so more and more single-chip microcomputer engineers have begun to use C language programming. However, the portability of C language is limited to subroutines that are not related to hardware, while subroutines related to specific hardware cannot be ported. In single-chip microcomputer applications, bit operations (especially bit operations on pins) are very common, such as reading and writing EEPROM data and IC card data, field-type LCD display, etc. Many integrated circuits with serial destinations require single-chip microcomputers to use software to do I/O port reading and writing programs. How to make these subroutines have good versatility and high efficiency in code generation is a problem that many software engineers are considering. Here are two porting methods for C language bit operations.

1 Use logical operations to implement bit operations
Please see the following subroutine:


This is a subroutine to read a byte from the 88SC102 card through the microcontroller pin. The program uses the writing style in μC/OS-II, that is, variables and functions are written in "camel case", and constants and inline functions defined by define are written in all uppercase and underlined.
This program drives a pin to output CARD_CLK high and low signals, and reads CARD_SDA data from another pin one bit at a time.
1.1 For MSP430 series microcontrollers
This program is applied to MSP430 microcontrollers (this article uses MSP430F413 microcontrollers), and the following definitions should be included in the header file:

The compilation results are as follows:


This is almost the same result as programming by hand in assembly. The code is very efficient. [page]

1.2 For 51 series MCU
To apply this program in 51 series MCU, the following definition should be added to the header file:


From the assembly results, we can see that the direct clearing and setting of bits has reached the simplest level, but the reading of bit values ​​is not ideal.
1.3 Used in 196/296 series microcontrollers
In microcontrollers such as 80C196MC and 80C296SA, the on-chip I/O ports can be window-mapped to low-end addresses. In this way, the I/O ports can be directly addressed, so the program code is the shortest and the execution speed is the fastest, but this makes the C program unportable. If window technology is not used, the on-chip I/O ports are memory-mapped and operate in the same way as ordinary memory addresses. Add the following definition to the header file to use the original program:


The assembled code is 56 bytes, and the code efficiency is also very high.
Using logical operations to implement bit operations, C programs are simple and clear, easy to transplant, and more readable. However, the 96 series microcontrollers cannot use JBC and JBS bit operation instructions, and the 51 series microcontrollers cannot use their unique bit operation instructions such as JB and JNB to improve code efficiency. Using the bit segment structure to implement bit operations can make up for this deficiency. [page]

2 Use the bit segment structure to implement bit operations
Rewrite the original program as follows:


2.1 Application in 51 series microcontrollers
To use ACC in C51, it is not necessary to define it in each subroutine, so add #define C51_ASM at the beginning of the file. In this way, the fourth, fifth, and sixth sentences will be ignored. Add the following definition in the header file:


The rest of the definitions are as described in the first part of this article. As a result, the assembly of sentence ⑧ becomes "MOV C, CardSDA" and "MOV ACC_0, C". In sentence ⑩, the function returns the parameter through R7, and the program has been simplified.


You can also define a bit segment structure like 196/296, using the JB instruction. Interested readers can try it themselves.

2.2 Application in 196/296 series microcontrollers
To apply this program in 196/296, you need to add a definition of a local variable ACCImg, which is the fourth, fifth, and sixth sentences in the previous program. Then add the following bit segment structure definition in the header file:


The port address variable must be defined as the following data type: bdata PIN; [page]
At the same time, add the macro definition in the header file:


In this way, ACCImg is defined as a low-end register, and ACC is its byte access form. The eighth sentence in the source program reads the pin, and the result of the assembly uses the JBC instruction. The whole program reduces the bytes compared to not using the bit segment, achieving the purpose of optimizing the code.

]

2.3 Application in MSP430 series microcontrollers
MSP430 series microcontrollers do not have bit operation instructions, so there is no need to define a bit segment structure. Just define ACC as an unsigned 8-bit number. The header file is defined as follows:


The result of the assembly is exactly the same as that of the bit operation using logical operations.


Conclusion
There are three types of bit operations on pins: directly setting or clearing, inputting data from the port, and outputting data from the port. The first two have been introduced above. The C program to output data from the port is as follows:


Among them: the first sentence OUT_SIO_DA(), the 5l series can be defined as a bit operation SIO_SDA=ACC_7; the 196/296 and 430 series can be defined as an if statement as above.
The name ACC is used as a local variable in the bit segment operation program. In C51, this happens to be the main accumulator, which is very practical for programs of half-duplex devices such as 240l and IC cards, but it is not so convenient when the SPI bus input/output is operated simultaneously.
There is no transplantation obstacle to using logical operations to implement bit operations. The bit operations in μC/OS-II are all implemented using logical operations. The bit segment definition may have different allocation orders for different compilers, but considering that 32-bit high-speed CPUs will not use software to simulate this serial port operation, such programs will only be used in medium- and low-speed microcontrollers without on-chip cache such as 5l, 196/296, MSP430, etc., so the method of using bit segments to operate pins still makes sense. Whether to use logical operations or bit segments for bit operations depends entirely on personal preference. The compilers used in this paper are KeiI C51 V7.03, IARC430 V2.10A and Tasking C96 V5.0.

Keywords:Embedded Reference address:Transplantation and Optimization of Embedded C Language Bit Operation

Previous article:Building an embedded system development environment under RTW
Next article:Design of isolated word speech recognition algorithm based on system on chip SoC

Recommended ReadingLatest update time:2024-11-17 00:50

How to implement stack protection in IAR Embedded Workbench development tools to improve code security
As more and more embedded products are connected to external networks, people are paying more and more attention to the information security of embedded products. This includes direct connection to external networks, such as through Wi-Fi connection, and indirect connection to external networks, such as the ECU in t
[Industrial Control]
How to implement stack protection in IAR Embedded Workbench development tools to improve code security
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号