The basic concept of pointers in PICC is not much different from the standard C syntax. However, in the specific architecture of PIC microcontroller, there are still a few points that need special attention when defining pointers.
1) Pointer to RAM
If it is assembly language programming, the method to implement pointer addressing is definitely to use FSR registers, and PICC is no exception. In order to generate efficient code, PICC will use FSR to implement indirect addressing for pointer operations pointing to RAM when compiling the original C program. This is bound to cause a problem: the range that FSR can directly and continuously address is 256 bytes (bank0/1 or bank2/3). How to define the pointer to cover the maximum 512 bytes of internal data storage space? PICC still leaves this problem to the programmer to solve: when defining a pointer, the addressing area to which the pointer applies must be clearly specified, for example:
unsignedchar*ptr0; //①Define the pointer that covers bank0/1
bank2unsignedchar*ptr1; //②Define the pointer that covers bank2/3
bank3unsignedchar*ptr2; //③Define the pointer that covers bank2/3
Three pointer variables are defined above, of which ① pointer has no bank restrictions and points to bank0 and bank1 by default; ② and ③ point to bank2 and bank3 respectively, but they are actually the same because a pointer can cover the storage area of two banks at the same time. In addition, the above three pointer variables are stored in bank0. We will introduce how to store pointer variables in other banks later.
Since the defined pointer has a clear bank applicable area, type matching must be implemented when assigning values to pointer variables. The following pointer assignment will generate a fatal error:
unsignedchar*ptr0;
bank2unsignedcharbuff[8];
Program statements:
//Define a pointer to bank0/1
//Define a buffer in bank2
ptr0 = buff; // Error! Trying to assign the address of a variable in bank2 to a pointer to bank0/1
If this type of incorrect pointer operation occurs, the PICC will notify you of a message similar to the following when it is finally connected:
Fixupoverflowinexpression(...)
Similarly, if a pointer is used as a parameter when calling a function, the bank scope must also be matched, which is often overlooked. Suppose the following function implements the function of sending a string:
void SendMessage(unsignedchar*);
Then the string to be sent must be in bank0 or bank1. If you also want to send a string in bank2 or bank3, you must write a separate function:
void SendMessage_2(bank2unsignedchar*);
These two functions can be exactly the same in terms of internal code implementation, but the types of parameters passed are different.
According to my application experience, if you see the error indication "Fixupoverflow", it is almost certainly caused by the assignment of pointer types that do not match. Please focus on checking the pointer operations in the program.
2) Pointer to ROM constant
If a set of variables is a constant that has been defined in the ROM area, then the pointer to it can be defined like this:
constunsignedcharcompany[]=”Microchip”;
constunsignedchar*romPtr;
The program can assign values to the above pointer variables and implement data retrieval operations:
romPtr=company; // pointer initialization
data=*romPtr++; //Get the number pointed to by the pointer, and then add 1 to the pointer
//Define constants in ROM
//Define a pointer to ROM
Conversely, the following operation will be an error because the pointer points to a constant variable and cannot be assigned a value.
*romPtr = data; //Write a number to the address pointed to by the pointer
3) Pointer to function
Function pointers are rarely used in microcontroller programming, but as part of the standard C syntax, PICC also supports function pointer calls. If you have a certain understanding of compiler principles, you should understand that the efficiency of implementing function pointer calls on the specific architecture of PIC microcontrollers is not high: PICC will establish a call return table in RAM, and the actual call and return process is achieved by directly modifying the PC pointer. Therefore, unless required by special algorithms, it is recommended that you try not to use function pointers.
4) Pointer type modification
The pointer definitions introduced above are the most basic forms. Like ordinary variables, pointer definitions can also be preceded by special type modifier keywords, such as "persistent", "volatile", etc. Considering that the pointer itself must also limit its scope, the pointer definition in PICC may seem a bit complicated at first glance, but as long as you understand the specific meaning of each part, it becomes very straightforward to understand the actual use of a pointer.
㈠The positional meaning of the modifier bank
Some of the pointers introduced above work on bank0/1, and some work on bank2/3, but they are all stored in bank0. Obviously, in a program design, pointer variables may be located in any available address space. At this time, the location where the bank modifier appears is a key. See the following example:
//Define a pointer to bank0/1. The pointer variable is in bank0
unsignedchar*ptr0;
//Define a pointer to bank2/3, the pointer variable is in bank0
bank2unsignedchar*ptr0;
//Define a pointer to bank2/3, the pointer variable is in bank1
bank2unsignedchar*bank1ptr0;
From this, we can see the rule: the bank modifier in front indicates the scope of this pointer; the bank modifier in the back defines the storage location of this pointer variable itself. Once you master this rule, you can define pointers of any scope and put pointer variables in any bank.
㈡The positional meaning of the volatile, persistent, and const modifiers
If you can understand the positional meaning of the bank modifier introduced above, in fact, the meaning of the keywords volatile, persistent, and const appearing in different positions is consistent with the word bank. For example:
//Define a pointer to a volatile character variable in bank0/1. The pointer variable is located in bank0 and is non-volatile.
volatileunsignedchar*ptr0;
//Define a pointer to a non-volatile character variable in bank2/3. The pointer variable is located in bank1 and is volatile.
bank2unsignedchar*volatilebank1ptr0;
//Define a pointer to the ROM area. The pointer variable itself is also a constant stored in the ROM area.
constunsignedchar*constptr0;
That is, the modifier that appears in front acts on the variable pointed to by the pointer; the modifier that appears in the back acts on the pointer variable itself.
Previous article:PIC C language programming_Variable definition in PICC
Next article:PIC C language programming_Implementation of PICC interrupt function
Recommended ReadingLatest update time:2024-11-16 15:24
- 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
- TI full range of component libraries (component library, package library, 3D library) collection
- [NXP Rapid IoT Review] Week 2: Get familiar with the online GUI Rapid IoT Studio
- National College Student Electronic Design Competition Quadcopter UAV Data Album
- Today at 10:00 AM, there will be a prize live broadcast: [ADI will meet you for TOF (Time Of Flight) technology introduction and product applications]
- Second-order filter, how to ensure that the data does not overflow when performing fixed-point operations?
- GD32E231 DIY Competition (7) - Human infrared sensor module and music IC driver completed
- About the problem of data transmission between AD7768 evaluation board and sdp-h1 board
- Free e-book download | "ADI System Solutions Selection Volume 6"
- Hello everyone
- CC2541 Mini Development Kit and CC2540 by Original You