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.Β
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. Β Β
int main(int argc, char** argv) @ 0x200 {}
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: www.microcontroller-project.com
* 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);
}
void main() @ 0x100 {}