Placing code in a specific Rom/Flash/Program Memory Address of Microchip Pic Microcontroller

Recently i was working with 8-bit pic16f877 microcontroller and i want to place program code at a specific rom(read only memory) location. I was working with xc8 compiler and mplabx ide.  Previously i did this same thing many times while using c18 c compiler. C18 compiler uses #pragma code directives to accomplish this task. But when i started with xc8 compiler and put the same #pragma code directive in my code the compiler starts giving errors. I found that the xc8 didn’t support #pragma code directives. So now i have to locate the xc8 macros for accomplishing this task. I searched the internet but did not found any major help regarding it. Then i decided to read the xc8 compiler datasheet. After lot of reading i found some valuable information and now i want to share it with the microcontroller projects community.

I found three methods to store data at a particular location in program memory of pic microcontroller, when working with xc8 compiler. The three macros/directives that i found were

  • __section()
  • __at()
  • ‘@’ qualifier

The first two methods are little complex and difficult to manage, while the third one the ‘@’ qualifier is easy to manage and use. Before using the ‘@’ qualifier one should first check the program memory banks addresses range of the pic microcontroller that he is using. I am using pic16f877 microcontroller, its program memory address range is from 0x0000 to 0x1FFF. The word size is 14 bits and memory size is 8 KB. 

Placing code in a specific Rom Flash Program Memory Address of Microchip Pic Microcontroller

I want my code to start at program memory location/address 0x200. To do so i placed the ‘@’ qualifier at the end of the main function. See the above main function initialization. At the end of the closing brackets i placed the @ qualifier with the program memory address where i want my code to be placed in flash/rom. In the main function i have two for loop functions, generating a random delay.    

After writing code i compiled it with xc8 compiler in mplabx ide. For verifying that the code is placed at the right address, I started mplabx simulator and opened the program memory window to see the code placement. See the result on the left hand side. The code starts at the address 0x200 as specified in the c code. Program memory window also shows the assembly equivalent code of the c code.

int main(int argc, char** argv) @ 0x200 {}

Note: The syntax of the @ qualifier is very important. Place the qualifier and address with one digit void gap. I placed them together, compiled the code successfully with no errors but the code did not starts at the desired address. So be sure to put the gap between qualifier and address.  

I recommend to chose the program memory last/bottom address for placing your code. Do not pick the address from in between or starting memory address. Make the code stream line. Jumping at different address will create mess and increase latency.    

* File: code.c
* Author: Usman Ali Butt
* Property off:
* Created on 15 March, 2017, 3:38 PM

// PIC16F877 Configuration Bit Settings
// ‘C’ source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIGURATION bits of pic16f877 microcontroller
#pragma config FOSC = EXTRC // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
// FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)
#pragma config WRT = ON

#include <stdio.h>
#include <stdlib.h>
#include <pic16f877.h>

int main(int argc, char** argv) @ 0x200 {//Program memory Address specified

for(int i=0;i<1000;i++){
for(int j=0;j<10000;j++);
return (EXIT_SUCCESS);

The upper code is just a demo. The @ qualifier is not limited for main function. You can use it in your custom functions. Like if you have a custom delay function in your code and you want to place it at location 0x100 the initialization code will be 

void main() @ 0x100 {}

More tutorial on placing data at specific location/address in ram of pic microcontroller

Download the project code. Folder contains the mplabx project with all files. Please don’t forget to give us your feed back on the project. If you have any queries and suggestions regarding project. Feel free to discuss them below.

Current Project / Post can also be found using:

  • pic16 usb to i2c

Leave a Comment

= 4 + 7

Read previous post:

The internet as we knew it, a few years back, was being run by humans; the majority of everything that...

Scroll to top