This tutorial is about placing data in a specific ram(random access memory) location/address using xc8 compiler and mplabx ide.Β I am using microchip pic16f877 8-bit microcontroller in this tutorial. It has 8KB of rom (read access memory) and 368 Bytes of ram(random access memory). Its generally not recommended to place data in data memory(ram). Since data memory(ram) size is limited, few Kbβs. Our processor runsΒ sequential routines to store and access data from data memory. Placing data in specific ram location/address disturbs theseΒ sequential routines and processor has to jump to specific address for storing and retiring our data. On the other hand it is useful in case if we want our data to be accessed and processed faster by processor. Since data in placedΒ in ram, we do not need it to load from program memory. Thus time to fetch the data from program memory(rom) is reduced.
Β
Do not reserve large chunks of addresses/locations for data in data memory(ram). If the data memory(ram) is flooded with the reserved addresses/locations, their will be no space left for variables/data that needs to be loaded in the memory while ourΒ program is running. I recommend to only place the data in memory which is continuously required by the system or data that isΒ highly fetched by the processor for manipulation and generating output, and this output is directly related to the system performance. Β
We can use β@β qualifier to access the address of microchip pic microcontroller ram using xc8 compiler. With β@β qualifier we not only can access ram addresses, we can also accessΒ program memory(rom) address. In this project i am also using β@β qualifier to access the ram location/address. Their are other two methods through which we can access the ram and rom but they are little difficult to manage. These two methods utilizes the compiler special directives.
- __section()
- __at()
I am going to allocate address in ram of pic16f877 microcontroller. I am using mplabx with xc8 compiler for writing and compiling code. You can see the code syntax in the picture on right side.
Addresses 0x70, 0x71 and 0x72 are allocated for integer variables ramloc1, ramloc2 and ramloc3. Please see the syntax of the code its
- int ramloc @ 0x70;Β
In the main function variables are initialized with data. I initialized variables with hex data 0x45 =69(decimal), 0x22=34(decimal)Β and 0x34=52(decimal).Β Initializing with hex data is easy to see in the ram file registers of mplabx ide.
/* | |
Β | * File: code.c |
Β | * Author: Usman Ali Butt |
Β | * Property off: www.microcontroller-project.com |
Β | * Created on 19 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. |
Β | // CONFIG |
Β | #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 |
Β | //(RB3/PGM pin has PGM function; low-voltage programming enabled) |
Β | #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 ramloc1 @ 0x70; //Reserve ram address for variable ramloc1 |
Β | int ramloc2 @ 0x71; //Reserve ram address for variable ramloc2 |
Β | int ramloc3 @ 0x72; //Reserve ram address for variable ramloc3 |
Β | Β |
Β | Β |
Β | int main(int argc, char** argv) {//Program memory Address specified |
Β | Β |
Β | ramloc1=0x45; //Store hex value all ram address 0x70 |
Β | ramloc2=0x22; //Store hex value all ram address 0x71 |
Β | ramloc3=0x34; //Store hex value all ram address 0x72 |
Β | for(int i=0;i<1000;i++){ |
Β | for(int j=0;j<10000;j++); |
Β | } |
Β | Β |
Β | return (EXIT_SUCCESS); |
Β | } |
Download the project code written in mplabx ide. Folder includes all the mblabx files. Please provide us your feed back on the project. If you have any queries please write them below in the comments section.