"I. conventional methods
All kinds of teaching materials or programming application reference materials of AVR single chip microcomputer. The port bit operation method introduced is nothing more than macro definition and overall and a constant phase or, phase and to realize the change of a single bit state. For example:
PORT&=-(1《1); Equivalent to: portb & = oxfd; The function is to clear PB1 and keep the other bits unchanged. Similar to this are:
PORTBl=(1《4); PB4 is set, and other bits remain unchanged.
PORTB=(k《4); PB4 flips and the remaining bits remain unchanged.
The above methods, whether constant value participation or shift operation, are always troublesome and not intuitive, and the specific constant value needs to be calculated manually, which is easy to make mistakes. The object code generated by shift operation is too large, which occupies system memory or Flash space and has low execution efficiency.
In terms of practical application effect, macro definition is better. Also prefer to use macro definitions. The C compiler will replace macros before compiling. Therefore, if you pay attention to some skills in macro definition, the code execution efficiency will be greatly improved and the operation will be handy during programming.
For general reference manuals or materials, it is recommended to use the following macro definitions:
After such a macro is defined, it can form a header file and then add it to the new project file (ICC 6.31a for compiler). However, the header file of the corresponding MCU shall be included before it. For example, if the MCU is at-mega48, the iom48v. H shall be included first, and then the self-made header file can be used in the program:
When using this method, the shift operation is still used, just for the convenience of program operation, which is only used in small-scale programs.
Next, we will make full use of the strong large bit operation ability of C language and the combination of pointers to build a bit definition header file under the icc6.31a platform, hoping to give you some enlightenment.
Firstly, several basic concepts are defined: bit field, address binding, and volatile qualified keyword.
In the installation directory of icc6.31a, there is an in clude folder, in which a large number of compiler developers have reserved header files for various AVR microcontrollers we have developed. The following is the iom48v. H header file.
Note: each port has three eight bit registers: pin, DDR and port.
Briefly summarized as follows:
The volatile - word is used to specify that the C compiler does not allow optimization of its qualified variables. For example:
This macro definition should be combined with the iom48v. H header file. In the front, there is the definition of each port register in the header file. In the above macro statement, ox25 is forcibly converted into a pointer constant. In fact, the meaning of the above macro definition is that portb is forcibly defined on the address ox25, that is, an unsigned character variable portb is defined and forcibly bound to the ox25 address. Other statements, and so on.
With this concept, let's take a look at the definition of bit field in C language. In standard C language, a special structure, bit field, can be defined, which allows the operation of a single bit in the defined structure. The basic composition is as follows:
In the header file of bit field definition given below, a bit field byte is defined in the custom bit operation header file_ BIT。
Custom AVT_ Bit. H header file (excerpt, taking Atmega48 as an example, define its port B)
Each port can be defined according to the same law.
Combined with the relevant definitions of header file, re bind its specific address, combined with the concept of bit field, and then divide each register from a byte into 8 controllable bits step by step. In the above documents, only port B is stated. In addition, the ports of Atmega48 are incomplete. There are only ports B, C and D, and there is no Port A. port D is 8 bits and port C is 6 bits. Port B is 7 bits. If an external crystal is connected, pb6 and pb7 cannot be used as ports. Therefore, the port is incomplete, but in order to maintain bit domain integrity and consistency and facilitate understanding, ports B, C and D are treated as 8bit. In actual operation, be careful not to operate on bits that do not actually exist. Of course, if you are interested, you can change the relevant definitions of the header file to improve it.
To verify this header file, a test program beep. E is quickly written, as follows:
In the circuit topology, a triode (connected to pole b) is connected to the PbO pin (14) of Atmega48, and a buzzer is controlled by the triode. The procedure is simple. In order to be more intuitive, LEDs are connected to other unused pins of the portb port. At this time, in the test program, change the port initialization function statement to:
DDRB=oxff; PORTB=ox00;( Or portb = oxff; Depending on the diode connection method), the diode cooperates with the buzzer to observe whether the PbO position can act alone. Of course, using this bit definition header file can realize the bit operation of any bit of any port of MCU.
2、 Extended application
Based on the above principles, combined with the address taking operator & of C language, it is not difficult to implement a general bit definition header file, which is suitable for all AVR microcontrollers, but the code is more complex. You might as well try it yourself.
Note: the above header files are defined by referring to the include folder of icc6.31a compiler. Before use, please first include the MCU header files provided by the system, and copy the custom header files to the built project, and then they can be used freely.
In terms of current usage, there are many MCU using at-mega48 / 16 / 128, so it is possible to define three corresponding bit operation header files. General purpose files are often bulky, cumbersome to compile and poorly readable, and are not suitable for ordinary enthusiasts., Read the full text, technical section
Three development directions of single chip microcomputer chip
Analysis of scon register and C program of 51 single chip microcomputer
Analysis of port bit operation method of AVR single chip microcomputer
How to use the single chip microcomputer written in C language to control the running water lamp
STM32 MCU how to output PWM through the control of timer and download enthusiast app
Create an electronic circle of your contacts
Pay attention to wechat of electronic enthusiasts
Interesting and informative information and technology dry goods
Focus on enthusiast class
Lock in the live broadcast of the latest course activities and technologies and collect them
0 collections
Share:, comment
Lin Chaowen PCB Design: pads tutorial, pads video tutorial, Zheng Zhenyu teacher: Altium designer tutorial, Altium designer video tutorial, Zhang Fei actual combat electronic video tutorial, Zhu Youpeng teacher: Hisilicon hi3518e tutorial, hi3518e video tutorial, Li Zeng teacher: signal integrity tutorial, high-speed circuit simulation tutorial, Huawei Hongmeng system tutorial, harmonyos video tutorial, saisheng: EMC design tutorial, EMC video tutorial Mr. Du Yang: STM32 tutorial, STM32 video tutorial, Tang zuolin: basic C language tutorial, basic C language video tutorial, Zhang Fei: Buck Power tutorial, buck power video tutorial, punctual atom: FPGA tutorial, FPGA video tutorial, Mr. Wei Dongshan: embedded tutorial, Embedded video tutorial Zhang Xianfeng: C language basic video tutorial Xu Xiaogang: Modbus communication video tutorial Wang Zhentao: NB IOT development video tutorial mill: FPGA tutorial, zynq video tutorial c language video tutorial linux driver development video tutorial Zhu Youpeng: u-boot source code analysis video tutorial harmonyos,
Press and hold the slider and drag to the far right
Learn about new features
Published, relevant recommendations
Design of serial port query of AVR single chip microcomputer
* Code adapted from Atmel AVR ApplICation Note AVR...
Published on November 22, 2018 at 15:37
•
11 readings
Design of digital tube display clock based on AVR single chip microcomputer
//Nixie tube data port P0, nixie tube control port P2
#include
#include
#in...
Published at 15:23, November 21, 2018
•
28 readings
How to use AVR single chip microcomputer to realize AT24C256 data high
Recently, I bought an AT24C256 EEPROM with a capacity of 32K byte and a data address width of 16bit
Published at 15:03, November 21, 2018
•
28 readings
Realization of hardware multi computer communication function based on AVR single chip microcomputer
Communication rules:
1: Clock 7.3728mhz/baud rate 9600 / 9 data bits / odd check / 1 stop bit / multiple hardware
Published at 15:04, November 20, 2018
•
29 readings
Advantages, characteristics and future development prospect of AVR single chip microcomputer
First of all, for non professionals, the main reason for choosing AVR MCU is that the threshold for entering AVR MCU development is very low, as long as they can operate
Published at 15:49, November 12, 2018
•
87 readings
Analysis of sensing principle of RC capacitor touch in AVR single chip microcomputer
In touch sensing applications, capacitance (c) consists of two parts: fixed capacitance (electrode capacitance, CX) and when the hand contacts or approaches the electrode, it is composed of
Published at 16:21, November 9, 2018
•
115 readings
Our other product: