PIC C language programming_pointers in PICC

Publisher:勾剑寒Latest update time:2020-02-24 Source: eefocusKeywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:PIC Reference address:PIC C language programming_pointers in PICC

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

Component selection for serial communication module between PIC microcontroller and PC
    The MAX232 product is a chip compatible with the RS-232 standard launched by Texas Instruments (TI). The device contains two drivers, two receivers and T voltage generators, providing TIA/EIA-232-F levels. The MAX232 has the following features:   Single 5V power supply and four 1,0μF charge pump capacitors;   Me
[Microcontroller]
Component selection for serial communication module between PIC microcontroller and PC
Design of LCD module driven by PIC microcontroller
The bias voltage is generated by using an external resistor ladder network (see the circuit diagram below). Because the resistor ladder network is connected between VDD and Vss, there will be a current flowing through the resistor ladder network, and the current is inversely proportional to the resistance. In other wo
[Microcontroller]
Design of LCD module driven by PIC microcontroller
PIC Microcontroller Getting Started Tutorial (Part 2) - Installing the Integrated Development Environment
The computer used in this tutorial runs Windows 10 Professional 64-bit 1. Download MPLAB X IDE v4.05 Historical versions: http://www.microchip.com/development-tools/pic-and-dspic-downloads-archive  Note: As of 2018-06-10, the latest version of MPLAB X IDE is v4.15. When using v4.15 offline, every time you manage
[Microcontroller]
PIC Microcontroller Getting Started Tutorial (Part 2) - Installing the Integrated Development Environment
Summary of PIC microcontroller interrupts
Compared with 51 or other series of single-chip microcomputers, the interrupt mechanism of PIC single-chip microcomputer has its own special features. In view of some problems and doubts of some of our beginners, I will make a personal summary here. If there are any inappropriate parts, please correct me. sO100
[Microcontroller]
pic16 multi-channel adc sampling
#define FUEL 0X23 //Oil sensor 2 fuel an8 00100011     #define TEMP 0x1f //Oil sensor 2 temperature an7 00011111     #define POWER 0X11 //Oil sensor 2 voltage an6 00011011  Related ports are initialized as input unsigned int get_ad(unsigned char n) {     unsigned int value;        ADCON0 = n ;         ADCON1=0XE0;//
[Microcontroller]
PIC16F877A microcontroller (external interrupt)
1 Basic principles 2 Implementation Code /*----------------Function function:     External interrupt RB0/INT Press the button (that is, generate an interrupt), and the LED lights up --------------------------*/ #include pic.h // Call the header file of PIC16f87XA microcontroller //#include"delay.h"//Heade
[Microcontroller]
PIC16F877A microcontroller (external interrupt)
What is a PIC microcontroller and what are its characteristics?
What is a PIC microcontroller? PIC (Peripheral Interface Controller) is an integrated circuit (IC) used to control peripheral devices. It is a CPU with decentralized (multi-tasking) functions. Compared with humans, the brain is the CPU, and the shared part of PIC is equivalent to the human nervous system. PIC microc
[Microcontroller]
What is a PIC microcontroller and what are its characteristics?
A Novel Bootloader Design Based on PIC18 Microcontroller
     Bootloader is a small program that runs before the kernel of the operating system runs. Its main function is to complete the initialization of hardware and software devices, establish memory space mapping, and bring the system's hardware and software environment to a suitable state, or load the operating system i
[Microcontroller]
A Novel Bootloader Design Based on PIC18 Microcontroller
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号