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
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
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.
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);
}
No comments:
Post a Comment